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. |
robinbb
added a commit
to robinbb/dune
that referenced
this pull request
May 27, 2026
Extract the multiplicative `(acc * 31) + x` accumulator into a new
`Stdune.Hash` module with the API requested in review:
type t
val create : unit -> t
val feed : t -> int -> t
val hash : t -> int
Route the structural hash combinators through it:
- `Stdune.Tuple.T2.hash`, `Stdune.Tuple.T3.hash`, `Stdune.List.hash`
drop the allocations (a fresh inner tuple / a fresh `int list`)
that they previously paid before handing off to `Hashtbl.hash`.
- `Stdune.Option.hash` stops allocating via `Hashtbl.hash` and gains
an explicit discriminator byte. The `None` vs `Some 0` collision
fixed in ocaml#14543 is preserved by construction.
- `Stdune.Result.hash` likewise stops allocating and gains explicit
discriminator bytes. This is a drive-by bugfix: the previous
implementation collided `Ok x` with `Error y` whenever the inner
hashes were equal (e.g. `Ok 0` and `Error 0`), the same shape as
the bug `Stdune.Option.hash` had pre-ocaml#14543.
- `Resolve.error_hash` (`src/dune_rules/resolve.ml`) drops its 2-tuple
allocation and its per-call `List.map ~f:Lazy.force` (which builds
a fresh `int list` of length |stack_frames|), folding the forced
frames into the accumulator instead.
Same `'a -> 'a -> int` callsite API; callers don't change. Hash
values shift, but these functions feed `Hashtbl` / `Memo` bucket
selectors only — no persistent serialization, no cross-process
invariants, so the shift is invisible to consumers.
Signed-off-by: Robin Bate Boerop <me@robinbb.com>
robinbb
added a commit
to robinbb/dune
that referenced
this pull request
May 28, 2026
Extract the multiplicative `(acc * 31) + x` accumulator into a new
`Stdune.Hash` module with the API requested in review:
type t
val create : unit -> t
val feed : t -> int -> t
val hash : t -> int
Route the structural hash combinators through it:
- `Stdune.Tuple.T2.hash`, `Stdune.Tuple.T3.hash`, `Stdune.List.hash`
drop the allocations (a fresh inner tuple / a fresh `int list`)
that they previously paid before handing off to `Hashtbl.hash`.
- `Stdune.Result.hash` likewise stops allocating and gains explicit
discriminator bytes. This is a drive-by bugfix: the previous
implementation collided `Ok x` with `Error y` whenever the inner
hashes were equal (e.g. `Ok 0` and `Error 0`), the same shape as
the bug `Stdune.Option.hash` had pre-ocaml#14543.
- `Resolve.error_hash` (`src/dune_rules/resolve.ml`) drops its 2-tuple
allocation and its per-call `List.map ~f:Lazy.force` (which builds
a fresh `int list` of length |stack_frames|), folding the forced
frames into the accumulator instead.
`Stdune.Option.hash` is already optimal on main as of ocaml#14543 and is
left unchanged.
Same `'a -> 'a -> int` callsite API; callers don't change. Hash
values shift, but these functions feed `Hashtbl` / `Memo` bucket
selectors only — no persistent serialization, no cross-process
invariants, so the shift is invisible to consumers.
Signed-off-by: Robin Bate Boerop <me@robinbb.com>
robinbb
added a commit
to robinbb/dune
that referenced
this pull request
May 29, 2026
Extract the multiplicative `(acc * 31) + x` accumulator into a new
`Stdune.Hash` module with the API requested in review:
type t
val create : unit -> t
val feed : t -> int -> t
val hash : t -> int
Route the structural hash combinators through it:
- `Stdune.Tuple.T2.hash`, `Stdune.Tuple.T3.hash`, `Stdune.List.hash`
drop the allocations (a fresh inner tuple / a fresh `int list`)
that they previously paid before handing off to `Hashtbl.hash`.
- `Stdune.Result.hash` likewise stops allocating and gains explicit
discriminator bytes. This is a drive-by bugfix: the previous
implementation collided `Ok x` with `Error y` whenever the inner
hashes were equal (e.g. `Ok 0` and `Error 0`), the same shape as
the bug `Stdune.Option.hash` had pre-ocaml#14543.
- `Resolve.error_hash` (`src/dune_rules/resolve.ml`) drops its 2-tuple
allocation and its per-call `List.map ~f:Lazy.force` (which builds
a fresh `int list` of length |stack_frames|), folding the forced
frames into the accumulator instead.
`Stdune.Option.hash` is already optimal on main as of ocaml#14543 and is
left unchanged.
Direct consumption of ocaml#14605: `src/source/opam_switch.ml` already
included `Repr.Poly`, but a redundant manual `let hash` shadowed the
inherited one. That definition is now dropped. This is a single
demonstration site for the elimination-by-Repr.Poly direction
referenced in the review; a broader sweep is out of scope here
because `Repr.Poly`'s `validate_compare` rejects `Repr.view`, which
pervades dune's wrapped-string newtypes (paths, lib-names, opam
URLs, checksums, package names, ...).
Same `'a -> 'a -> int` callsite API; callers don't change. Hash
values shift, but these functions feed `Hashtbl` / `Memo` bucket
selectors only — no persistent serialization, no cross-process
invariants, so the shift is invisible to consumers.
Signed-off-by: Robin Bate Boerop <me@robinbb.com>
robinbb
added a commit
to robinbb/dune
that referenced
this pull request
May 29, 2026
Extract the multiplicative `(acc * 31) + x` accumulator into a new
`Stdune.Hash` module with the API requested in review:
type t
val create : unit -> t
val feed : t -> int -> t
val hash : t -> int
Route the structural hash combinators through it:
- `Stdune.Tuple.T2.hash`, `Stdune.Tuple.T3.hash`, `Stdune.List.hash`
drop the allocations (a fresh inner tuple / a fresh `int list`)
that they previously paid before handing off to `Hashtbl.hash`.
- `Stdune.Result.hash` likewise stops allocating and gains explicit
discriminator bytes. This is a drive-by bugfix: the previous
implementation collided `Ok x` with `Error y` whenever the inner
hashes were equal (e.g. `Ok 0` and `Error 0`), the same shape as
the bug `Stdune.Option.hash` had pre-ocaml#14543.
- `Resolve.error_hash` (`src/dune_rules/resolve.ml`) drops its 2-tuple
allocation and its per-call `List.map ~f:Lazy.force` (which builds
a fresh `int list` of length |stack_frames|), folding the forced
frames into the accumulator instead.
`Stdune.Option.hash` is already optimal on main as of ocaml#14543 and is
left unchanged.
Direct consumption of ocaml#14605: `src/source/opam_switch.ml` already
included `Repr.Poly`, but a redundant manual `let hash` shadowed the
inherited one. That definition is now dropped. This is a single
demonstration site for the elimination-by-Repr.Poly direction
referenced in the review; a broader sweep is out of scope here
because `Repr.Poly`'s `validate_compare` rejects `Repr.view`, which
pervades dune's wrapped-string newtypes (paths, lib-names, opam
URLs, checksums, package names, ...).
Same `'a -> 'a -> int` callsite API; callers don't change. Hash
values shift, but these functions feed `Hashtbl` / `Memo` bucket
selectors only — no persistent serialization, no cross-process
invariants, so the shift is invisible to consumers.
Signed-off-by: Robin Bate Boerop <me@robinbb.com>
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.