Skip to content

refactor: add hash to Repr.Poly - #14605

Merged
rgrinberg merged 1 commit into
ocaml:mainfrom
rgrinberg:push-mlynqtrxmuwo
May 19, 2026
Merged

rgrinberg merged 1 commit into
ocaml:mainfrom
rgrinberg:push-mlynqtrxmuwo

Conversation

@rgrinberg

Copy link
Copy Markdown
Member

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.

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]>
@rgrinberg
rgrinberg merged commit e50794d into ocaml:main May 19, 2026
29 checks passed
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]>
@Alizter Alizter added this to the 3.24.0 milestone Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants