-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
calculate_log2_keysize
in dictobject.c
incorrect
#133703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Uh, it is my bug.
|
Thanks @methane for catching this:
With that change, you can also simplify the return statement: - return _Py_bit_length(minsize | (PyDict_MINSIZE-1));
+ return _Py_bit_length(minsize); Suggestion: for clarity you might instead write minsize = PY_MAX(minsize, PyDict_MINSIZE) - 1;
return _Py_bit_length(minsize); This makes the lower bound explicit, reads more directly at a glance, and the performance difference (if any) should be negligible. The |
Sorry, I was wrong. Py_MAX() uses conditional move on both of arm64 and amd64. |
Well, you were correct to say I was wrong to assume the performance difference is necessarily negligible in this context. I’m going to look more closely into assembly-level costs. But yeah, |
(cherry picked from commit 92337f666e8a076a68305a8d6dc8bc9c095000e9) Co-authored-by: Inada Naoki <[email protected]>
(cherry picked from commit 92337f6) Co-authored-by: Inada Naoki <[email protected]>
Bug report
Bug description:
Originally reported by @ThomasBr0 in #132762
The
_Py_bit_length()
and_BitScanReverse64()
code paths are incorrect and don't always return the smallest log2 key size to fit the desired number of items. The bug is benign in the sense that the dictionaries are sometimes too big, but they're never too small.For example
calculate_log2_keysize(7) = 4
butcalculate_log2_keysize(8) = 3
.cpython/Objects/dictobject.c
Lines 545 to 566 in 9546eee
cc @methane @markshannon
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: