BUG: length-aware string hashtable keys, fixing embedded-NUL collapse in unique/factorize/groupby - #66512
Draft
jbrockmendel wants to merge 1 commit into
Draft
BUG: length-aware string hashtable keys, fixing embedded-NUL collapse in unique/factorize/groupby#66512jbrockmendel wants to merge 1 commit into
jbrockmendel wants to merge 1 commit into
Conversation
pandas keys several khash tables on a bare `const char *`, which hashes to the first NUL and compares with strcmp, so any string containing an embedded NUL is truncated for both hashing and comparison. Distinct values that share a prefix up to their first NUL therefore collapse into one. Key those tables on an explicit (pointer, length) pair instead. The hash is X31 over exactly `len` bytes -- bit-identical to `__ac_X31_hash_string` for NUL-free input, so bucket distribution is unchanged -- and equality is a length check plus a byte comparison. `PyUnicode_AsUTF8AndSize` and `_token_len` supply the length, so no path pays an extra strlen. This fixes `unique`, `factorize`, `Series.unique`, `Index.unique` and `groupby` on object dtype, plus `read_csv`'s `dtype=object` interning and `dtype="category"` conversion. The na_values/true_values/false_values hashsets change key type but still derive their length with strlen, so NA handling is unchanged here; giving them an explicit length changes user-visible NA semantics and is left to a follow-up. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
doc/source/whatsnew/v3.1.0.rstfilepandas keys several khash tables on
kh_cstr_t, a bareconst char *, hashed with__ac_X31_hash_string(which stops at the first NUL) and compared withstrcmp. Any value containing an embedded NUL is therefore truncated for both hashing and comparison, so distinct values sharing a prefix up to their first NUL collapse into one.Keyed on an explicit
{const char *ptr; size_t len}pair instead. The hash is X31 over exactlylenbytes -- bit-identical to__ac_X31_hash_stringfor NUL-free input, so bucket distribution is unchanged -- and equality is a length check plus a byte comparison.PyUnicode_AsUTF8AndSizeand_token_lensupply the length, so no path pays an extrastrlen.Fixes
unique,factorize,Series.unique,Index.uniqueandgroupbyon object dtype, plusread_csv'sdtype=objectinterning anddtype="category"conversion (both of which silently boxed two distinct fields to one value).Note on the issue's snippet
GH-34551 is titled for
DataFrame.drop_duplicates, but that method routes throughPyObjectHashTableand already returns the right answer -- the issue's own snippet passes onmain. The confusion it reports is real, but it lives inunique/factorize/groupby. Worth knowing before comparing this diff against the issue title.Performance
The byte-by-byte equality is deliberate and load-bearing:
memcmpwith a runtime length does not inline, and one libc call per token cost -9.2% on a bool column, where a hashset lookup hits on nearly every token. Full 123-caseread_csvbenchmark against this branch's merge-base: median 1.0081, 55 significant wins, 2 regressions (bool @120.974,mixed @120.978 -- 12-thread only; the same fixtures are +7.6% and +2.4% serial). Long keys were the obvious risk for a byte loop and did not materialise (long fields (~150B)+3.6%), so no length threshold is needed.Scope
One of three PRs splitting the embedded-NUL work apart so the piece needing a semantics discussion doesn't block the rest:
na_values/true_values/false_valueshashsets change key type here but still derive their length withstrlen, so NA handling is unchanged by this PR.Draft until the set can be reviewed together.