perf: use zero-alloc xxh3.HashString on the local counter hot path#60
Merged
Conversation
The local counter hashed keys via the streaming xxh3.New() API, which benchmarks at ~17-20 ns/op. The one-shot xxh3.HashString produces a bit-identical result for the same input and measures ~1.5-3.6 ns/op (verified equivalent across empty, short and long keys), so this is a ~10x speedup on the per-request hot path with no change in hash output. The exported LimitCounterKey is intentionally left on the streaming API: making it zero-alloc would require concatenating the window string, which would change its output value and break key stability for external backends such as httprateredis. This addresses the performance angle raised by @lrstanley in #57: xxh3 was simply being called sub-optimally. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Benchmark Results |
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.
While looking into #57 (raised by @lrstanley), the benchmarks surfaced that we were calling xxh3 sub-optimally: the local counter hashed keys via the streaming
xxh3.New()API.On realistic rate-limiter keys (IP, IP+method+path, IPv6):
xxh3.New()streaming (current)xxh3.HashString(this PR)xxh3.HashString(key)returns a bit-identical result to the streaming call for the same input (verified across empty, short, and long keys), so this is a ~10x speedup on the per-request hot path with no change in hash output.Scope / what this intentionally does not change
The exported
LimitCounterKey(key, window)is deliberately left on the streaming API. Making it zero-alloc would require concatenating the window string, which would change its output value and break key stability for external backends such ashttprateredis. Only the unexported, in-memorylimitCounterKeyis changed (its values reset anyway, so there's no compatibility surface).Credit to @lrstanley in #57 — the performance discussion there is what prompted this; the dependency itself stays.
go vet ./...clean,go test ./...passes🤖 Prepared with the help of Claude (AI), reviewed before opening.