fix: Option.hash distinguishes None from Some x when (f x = 0)#14543
Merged
Conversation
Previously `Option.hash f None` and `Option.hash f (Some x)` both went through `Stdlib.Hashtbl.hash`, which gives equal results whenever `f x = 0` because `None` shares OCaml's tag-0 runtime representation with the integer 0. This collapses, for example, `Some false` and `None` under `Bool.hash`, and `Some ()` and `None` under `Unit.hash`. Switch to a non-structural recurrence that tags the `Some` branch with `+ 1`, so the two cases occupy disjoint buckets. Signed-off-by: Robin Bate Boerop <me@robinbb.com>
There was a problem hiding this comment.
Pull request overview
This PR updates Stdune.Option.hash so None no longer collides with Some x when the supplied element hasher returns 0, and adds regression coverage for the previously affected Bool and Unit cases.
Changes:
- Replaces structural hashing for options with a tagged integer recurrence.
- Adds an expect test covering
Option.hash Bool.hash (Some false)andOption.hash Unit.hash (Some ()).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
otherlibs/stdune/src/option.ml |
Updates Option.hash implementation to distinguish None from Some values whose inner hash is 0. |
otherlibs/stdune/test/option_tests.ml |
Adds expect-test coverage for the fixed Bool and Unit hash collision cases. |
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.
Summary
Option.hash f NoneandOption.hash f (Some x)previously both went throughStdlib.Hashtbl.hash, which gave equal results wheneverf x = 0becauseNoneshares OCaml's tag-0 runtime representation with the integer 0. So under stdune's existing hashers:Option.hash Bool.hash (Some false)collided withOption.hash Bool.hash None(Bool.hash false = 0).Option.hash Unit.hash (Some ())collided withOption.hash Unit.hash None(Unit.hash () = 0).This PR replaces the structural hash with a tagged recurrence —
None -> 0,Some s -> (f s * 31) + 1— so the two cases land in disjoint buckets. Added an expect test inotherlibs/stdune/test/option_tests.mlcovering theBoolandUnitcases that previously collided.Discovered while surveying allocation-heavy hash sites for a follow-up to #14542.