Repository navigation
feat(ts): hold a crate's own BuildHasher in a hash map or set - #509
Merged
Merged
Conversation
Quest-Claim: 79aa32ce98cbf0be
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…BuildHasher Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er in from_iter Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Hasher in from_iter Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
marked this pull request as ready for review
November 3, 2026 14:49
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 37 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. CodeRabbit Verify
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
Problem
A std or hashbrown
HashMap/HashSetbuilt with a crate's ownBuildHasherdidn't translate. A key that is a JS object was refused with "hashing by the hasher of_". With a primitive key,with_hasher(s)translated but threwsaway, so itsDropnever ran andhasher()was refused.Approach
Hash around it, as the maintainer decided. The map keeps finding keys with the runtime's own
Mixer, through the key'sHashfor std'sDefaultHasher, and never calls the crate's hasher. What it does withSfollows hashbrown:rt.HashMapgainss, the map's ownBuildHasher(anything butRandomState).with_hasherandwith_capacity_and_hasherkeep it, andhasher()lends it.S::default()runs wherever std's makes one:Default,collect/from_iter,mem::take, and|,&,-,^of sets.cloneclonesSbefore the entries. Drop glue dropsSbefore the entries (hashbrown's field order).into_iter,into_keys, andinto_valuesdrop it immediately.from_itertakes the items and reads their size hint before makingS(rt.hashFromIter), unlike std's map and set and hashbrown's set.Differential tests against native wasm32:
lang/hashesmaps(std) andstockmaps::own_hasher/maps::own_from_iter(hashbrown). They log eachS's default, clone, and drop, interleaved with value clones/drops, iteratorsize_hint, andhasher().hash_one(..). Maps print sorted contents; only one-entry maps print drop and clone order, since std's iteration order is its own.Decision
Decided by the maintainer: hash around the crate's
BuildHasher. The runtime never calls its hasher, which extends the README's rule that how often the runtime hashes is its own. A hasher that logs its calls sees none from the map, and one that panics on misuse (nohash-hasher's debug check) does not panic as it does natively. Calling through it could not match native exactly anyway: native rehashes every key as it grows by hashbrown's bucket counts, which the runtime does not follow (decided 2026-11-01).Impact
rt.HashMaphas a new fields.rt.hashNew,hashWithCapacity,hashClone,hashDrop,hashIter,hashKeys, andhashValuestake an optional trailing state argument, andrt.hashFromIteris new. Output for maps without their ownBuildHasheris unchanged.Alternatives
build_hasher,Hash, andfinishper operation for every key, primitives too. It matches native except on growth, but it gives up the JSMapfast path for primitive keys and needs a larger runtime change. The maintainer chose against it.RandomStatetoo, sohasher()works on a default map: it's a separate gap, costs aRandomStateper map, and has no consumer.Follow-ups
BuildHasherDefault<H>is refused as a type from another crate, which blocksFxHashMap-style maps.==of two maps is refused for everyBuildHasher.Review
Codex
gpt-6.1-sol, medium effort,quest review, head00de759600eb2e9f7582b994ba0c62cbd18b0b37: round 3 reported no findings.from_itermust callinto_iterbeforeS::default(). Fixed, with a differential case.size_hintbeforeS::default(). Fixed withrt.hashFromIter, with a differential case using a loggingsize_hint.Head
742f7a70044068fe6f0eb908701edaca35b98606mergesorigin/mainafter that review. The PR's code diff is unchanged; the merges changed only the stock fixture's case list and thehashesandstocksnapshots (main's new case, and #520's parentheses), so no new review was needed.just cipassed on742f7a70044068fe6f0eb908701edaca35b98606.(written by Claude Opus 5.5)
🤖 Generated with Claude Code