Re: [PATCH v2 2/2] oidset: use khash
- From
Jeff King <peff@peff.net>
- Date
- Oct 4, 2018, 06:48 UTC
- Message-ID
- <20181004064833.GA9051@sigill.intra.peff.net>
- In-Reply-To
- <d17f104e-7cc2-5e73-78c9-6c268f9ec5b2@web.de>
On Thu, Oct 04, 2018 at 07:56:44AM +0200, René Scharfe wrote:
Show 9 quoted lines
> > As the comment above notes, I think we're really looking at the case > > where this gets populated on the first call, but not subsequent ones. It > > might be less hacky to use a "static int initialized" here. Or if we > > want to avoid hidden globals, put the logic into filter_refs() to decide > > when to populate. > > Right. I'd prefer the latter, but was unable to find a nice way that > still populates the oidset lazily. It's certainly worth another look, > and a separate series.
It's a little awkward because the lazy load happens in a conditional. You can fully encapsulate it like the patch below, but I actually don't think it's really helping readability.
Show 11 quoted lines
> >> +KHASH_INIT(oid, struct object_id, int, 0, oid_hash, oid_equal) > > > > This will declare these "static inline". Our other major "macros become > > inline functions" code is commit-slab.h, and there we found it necessary > > to add MAYBE_UNUSED. I wonder if we ought to be doing the same here (I > > don't get any warnings, but I suspect sparse might complain). > > I doubt it (but didn't check) because khash.h defines kh_clear_##name(), > which we don't use it anywhere and there have been no complaints so far. > And if we wanted to add MAYBE_UNUSED then the right place for that would > be in KHASH_INIT, no?
Right, that would be the correct spot. I'm OK to leave it until somebody complains. Looking at commit-slab again, its functions are straight statics, not static inline. That's probably the important difference.
Show 17 quoted lines
> > It might be nice if these functions could hide inside oidset.c (and just > > declare the struct here). It looks like we might be able to do that with > > __KHASH_TYPE(), but the double-underscore implies that we're not > > supposed to. ;) > > > > I guess we also use a few of them in our inlines here. I'm not 100% sure > > that oidset_* needs to be inlined either, but this is at least a pretty > > faithful conversion of the original. > > We could inline all of the oidset functions, following the spirit of > klib/khash.h. > > Or we could uninline all of them and then may be able to clean up > oidset.h by using KHASH_DECLARE. Perhaps we'd need to guard with an > "#ifndef THIS_IS_OIDSET_C" or similar to avoid a clash with KHASH_INIT. > > Not sure if any of that would be a worthwhile improvement..
Unless we know something is a performance win to inline, I'd generally prefer not to.
For a case like this with auto-generated functions, I'm mostly worried about bloating the compiled code. Either with a bunch of inlined non-trivial functions, or cases where the compiler says "this is too big to inline" and generates an anonymous file-scope function, but we end up with a bunch of duplicates, because we're generating the same functions in a bunch of C files.
I may be worried about nothing, though. I don't know how clever the compiler and linker can be there.
-Peff