Re: [PATCH 1/5] scripts: add bit count parameter back to host hash functions
From: Julian Braha
Date: Fri Nov 27 2026 - 07:50:22 EST
On 11/27/26 01:16, Linus Torvalds wrote:
> On Thu, 26 Nov 2026 at 16:00, Julian Braha <julianbraha@xxxxxxxxx> wrote:
>> Accordingly, adjust the callers of 'hash_32()' and 'hash_ptr()' to use
>> the additional parameter. This is a pure refactor (no functional changes),
>> so pass 32 to preserve the existing behavior.
> I think this is the wrong approach.
>
> I'd suggest hiding the size of the hash table in the table itself, sinc
> ethey are all constant sizes.
>
> You can do it with some typedef tricks - not exactly pretty macros, but
> the end result ends up pretty nice from a user standpoint.
>
> Something like the appended TOTALLY UNTESTED patch. It builds, but it's
> literally a quick hack.
>
> What it does is to turn each hashtable into a 'union' type that has the
> hashtable head array (as 'hlist[]'), and then a special 'hash_bits[]'
> array.
>
> And the 'hash_bits[]' array isn't actually used as an array, it's
> literally just used for its size: it's the number of bits the hash table
> has.
>
> So now you can take such a hashtable type, and always turn it into the
> number of bits with just
>
> sizeof(hashtable.hash_bits)
>
> so now the hash table itself inherently carries its size in bits with it
> - no need for 'ilog2()' or any other crap to calculate the bits.
>
> And at that point, you can now just use the high bits of the hased
> value, and you don't need to pass in any hash table sizes.
>
> You can actually even pass pointers to these things around, but you have
> to do it with that original named type, because the size is carried in
> the *type* itself, and it's not just an anonymous array. Nothing in the
> tree does that
>
> We can't use this trick in the kernel, because the kernel hash tables
> aren't all fixed sizes. But for the scripting code, this should make
> the hashing really syntactically simple because the size is always
> there.
It's a fair point - I see why it was simplified in the first place.
Will take a close look at your code. Thanks!
- Julian Braha
Attachment:
OpenPGP_signature.asc
Description: OpenPGP digital signature