Closed brianwitte closed 3 months ago
Good catch! I'll have to take a closer look at this because this is really tricky code. We maybe need to add some SELF_TEST
to this too to ensure it doesn't break in the future.
@lpereira -- sounds good, agreed.
I will to add some SELF_TEST
for the greater good 😄
Will keep this up and add them here!
closing for now since we are going back to another approach for this lookup. we can revisit this later if it makes sense. thanks for looking into it! :sunglasses:
Improved index tracking logic and added explicit handling of zero cases with n | 1 to avoid undefined behavior in __builtin_clz* functions.
I ran into an issue here when trying out lua handlers and then I stepped through (and added debug logging) through the various query parameter handling functions and found that
value_lookup
was unable to retrieve values from they kv structure correctly.I refactored using the same guidance in orlp's article that was in
value_lookup
as a comment.The primary difference in this refactoring lies in how the index is computed. The C++ template in the article uses
std::bit_floor(n)
to find the highest power of two less than or equal ton
, which we mimic using__builtin_clz
operations.The calculation
b | bit
directly correlates with how the bitwise OR operation is used in the C++ template to conditionally update the indexb
.Instead of iteratively adjusting the index as in the original, this approach directly influences
b
by setting bits when the conditionkey_value_compare(&k, &base[i - 1]) >= 0
holds true.I tried my best to ensure that the semantics are preserved by maintaining the key-value comparison logic and the final check to confirm the key match before returning the value.