Repository navigation
refactor: add hash to Repr.Poly - #14605
Merged
Merged
Conversation
Expose polymorphic hashing alongside equal and compare for reprs that are validated as safe for polymorphic operations. Drop redundant local hash definitions now provided by Repr.Poly. Signed-off-by: Rudi Grinberg <[email protected]>
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 <[email protected]>
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 <[email protected]>
robinbb
added a commit
to robinbb/dune
that referenced
this pull request
Jun 3, 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 <[email protected]>
robinbb
added a commit
that referenced
this pull request
Jun 4, 2026
## Summary Extract the `(acc * 31) + x` hash accumulator into a new `Stdune.Hash` module per [#14542 review](#14542 (comment)), and route `Tuple.T2.hash`, `Tuple.T3.hash`, `List.hash`, `Result.hash`, and `Resolve.error_hash` through it. None of these allocate per call any more. Drive-by fix: `Stdune.Result.hash` no longer collides `Ok x` with `Error y` whenever `h1 x = h2 y` (e.g. `Ok 0` vs `Error 0`) — same bug shape `Stdune.Option.hash` had before #14543. Related: #14605 (`Repr.Poly.hash`). Signed-off-by: Robin Bate Boerop <[email protected]>
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.
Expose polymorphic hashing alongside equal and compare for reprs that are validated as safe for polymorphic operations. Drop redundant local hash definitions now provided by Repr.Poly.