Skip to content

fix: correct operator precedence in DDict hashset load factor check - #4760

Open
VirajMishra1 wants to merge 1 commit into
facebook:devfrom
VirajMishra1:fix-ddict-hashset-load-factor
Open

fix: correct operator precedence in DDict hashset load factor check#4760
VirajMishra1 wants to merge 1 commit into
facebook:devfrom
VirajMishra1:fix-ddict-hashset-load-factor

Conversation

@VirajMishra1

Copy link
Copy Markdown

The DDict hashset load factor check in ZSTD_DDictHashSet_expand has an operator precedence bug. The intent (documented in the comment at lines 83-87) is to check whether count * COUNT_MULT / (size * SIZE_MULT) != 0, i.e. whether the load factor exceeds 0.75.

But the expression lacks parentheses around the denominator:

hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT

Due to left-to-right associativity of * and /, this evaluates as ((count * 4) / size) * 3 instead of count * 4 / (size * 3). This triggers expansion at ~25% load instead of the intended 75%, causing the hashset to grow ~3x faster than necessary.

Fix: add parentheses around the denominator to match the documented formula.

The load factor expansion check evaluates as ((count * 4) / size) * 3
due to left-to-right associativity, triggering expansion at ~25% load
instead of the intended 75% (count * 4 / (size * 3)). This causes the
hashset to grow ~3x faster than necessary.

Add parentheses to match the formula documented in the comment.
@meta-cla meta-cla Bot added the CLA Signed label Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant