lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto Ierusalimschy:

It is there because 'luaH_realasize' can be
a little expensive to compute, so the redundancy avoids that call
in a quite common case.
Wouldn't it be better to use a more efficient test such as the following instead?

!isrealasize(t) && ((l_castS2U(key)-1u) & ~(((lua_Unsigned)t->alimit)-1u)) < t->alimit

This test does some bit hacks to test that key-1 is smaller than the next power of two of the limit. This is done by masking of bit p from (key-1) through ~(t->alimit-1) as p is chosen such that it is the integer for which 2^(p+1) >= t->alimit > 2^p holds. This results in (key-1) & ~(t->alimit-1)  staying larger or equal to 2^(p+1) when key-1 >= 2^(p+1) and getting smaller than 2^p when key-1 < 2^(p+1) as bit p is masked off through ~(t->alimit-1) allowing t->alimit being used as the compare value.

Regards,
Xmilia