perf: use runtime maphash for sharded-cache key hashing#362
Merged
Conversation
StringKeyToShard is on every Get/Set/Delete against pokestopCache, gymCache, and stationCache — easily tens of thousands of calls per second under typical GMO load. Switch from hash/fnv to hash/maphash, which is ~5× faster on the hot path (~17 ns/op → ~3.4 ns/op, measured on Go 1.26 / Apple M3 Pro with 35-char fort IDs). Both implementations are alloc-free thanks to Go 1.26's escape analysis on the fnv struct and []byte cast, so the win is pure CPU. maphash.MakeSeed() is captured once at package init; a per-process random seed is fine because shard assignments are in-memory only and never persisted across restarts.
Contributor
|
Nice to have, but gain close to nothing :) |
Collaborator
Author
|
Called 100s of times per gmo so not nothing but it’s hardly going to be noticeable indeed |
Contributor
Collaborator
Author
|
If you can find a way to optimise the proto memory that would be great - but pooling won’t work given the shame of gmos. This broad section of allocations is outside of our control |
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
StringKeyToShardis on everyGet/Set/Delete/GetOrSetFunccall againstpokestopCache,gymCache, andstationCache— easily tens of thousands of calls per second under typical GMO ingest load.Switch from
hash/fnvtohash/maphash, which is ~5× faster on the hot path.Benchmark
Single-key and many-distinct-keys variants, 5 runs each, Go 1.26 on Apple M3 Pro, 35-char fort IDs (typical):
fnv.New64a()+Write([]byte(key))maphash.String(seed, key)Both are alloc-free under Go 1.26 — escape analysis keeps the
fnvhash struct and the[]byte(key)cast on the stack. The win is purely CPU.Safety notes
maphash.MakeSeed()is captured once at package init. Shard assignments are in-memory only and never persisted, so a random per-process seed is fine.map).Test plan
go build ./...go vet ./decoder/go test ./decoder/ -race -count=1🤖 Generated with Claude Code