Eliminate hot-path allocations in Map.Get, Map.Without, Set.Has - #88
Merged
Conversation
The generics migration introduced 6 allocs/call on Map.Get and 4 on Set.Has due to Go's GCShape stenciling boxing struct types through any(x). This change eliminates all allocations on read paths for concrete key types (string, int, etc.) and reduces interface key types to at most 1 alloc. Key changes: - Cache DefaultNPEqArgs per type (was allocating fresh EqArgs per call) - Add string specializations in resolveHashFunc and EqualFuncFor to avoid boxing through any(x) - Add GetSeededHashFunc for non-boxing seeded hashing of map keys - Rewrite mapEntryHashFunc to hash keys directly instead of boxing the mapEntry struct through the Hashable interface - Add Tree.GetWith/WithoutWith accepting pre-built EqArgs - Map.Get/Without use cached specialized EqArgs with GetWith/WithoutWith Benchmarks (arrai, Apple M4 Max): Map[string,Value].Get: 6 allocs ~120ns → 0 allocs ~26ns (4.6x) TupleGet: 6 allocs ~120ns → 0 allocs ~27ns (4.5x) Set[string].Has: 4 allocs → 0 allocs ~20ns Set[Value].Has: 4 allocs → 1 alloc ~45ns Co-Authored-By: Claude Opus 4.6 <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.
Summary
DefaultNPEqArgsper type to avoid fresh*EqArgsallocation per callstringspecializations inresolveHashFuncandEqualFuncForto avoid boxing throughany(x)GetSeededHashFuncfor non-boxing seeded hashing of map keysmapEntryHashFuncto hash keys directly instead of boxing the mapEntry struct through the Hashable interfaceTree.GetWith/WithoutWithaccepting pre-built EqArgsMap.Get/Withoutuse cached specialized EqArgs withGetWith/WithoutWithBenchmarks (arrai, Apple M4 Max)
Map[string,Value].GetTupleGetSet[string].HasSet[Value].HasThe remaining 1 alloc on
Set[Value].Hasis theequalEqualerboxing path for interface types — deferred as it's low-impact (interface-to-any boxing is cheap).Test plan
go test ./...)go test ./...)-count=5🤖 Generated with Claude Code