perf: generic LRU cache with struct key for prepared statement cache#824
Merged
dkropachev merged 1 commit intoscylladb:masterfrom Apr 5, 2026
Merged
perf: generic LRU cache with struct key for prepared statement cache#824dkropachev merged 1 commit intoscylladb:masterfrom
dkropachev merged 1 commit intoscylladb:masterfrom
Conversation
…atement cache Make internal/lru.Cache generic (Cache[K comparable]) to support typed keys. Replace the prepared statement cache's string-concatenation key with a stmtCacheKey struct, eliminating ~100-500 bytes of allocation per query from fmt.Sprintf and fixing a theoretical key collision when host IDs or keyspace names contain the separator character. Also update routingKeyInfoLRU to use the typed Cache[string].
Author
|
CI failure seems to be #822 again. |
dkropachev
approved these changes
Apr 5, 2026
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
internal/lru.Cachegeneric (Cache[K comparable]) to support typed keysstmtCacheKeystruct, eliminating per-query allocation fromfmt.Sprintfand fixing a theoretical key collision when host IDs or keyspace names contain the separator characterroutingKeyInfoLRUto use the typedCache[string]Extracted from #779.
Benchmark results
Before (string-concatenation key) vs after (struct key) — 5 runs, Intel i7-1270P.
Lookup (hot path — cache hit)
Struct key lookup is ~3ns slower due to comparing 3 fields vs one pre-built string. Both are zero-allocation. This is the steady-state path for repeated queries — the regression is negligible in absolute terms (~3ns).
Insert (cold path — cache miss, key construction + insertion)
This is the path that matters for new queries: every new (host, keyspace, statement) tuple hits this on first use. The old approach allocated a concatenated string via
fmt.Sprintfon every call. The struct key references the original strings without copying.Net effect
The insert path (where the allocation overhead lived) improves by 32% across the board. The lookup regression of ~3ns is dwarfed by the real-world query execution time. Additionally, the struct key fixes a correctness issue: the old concatenation
hostID + keyspace + statementcould collide when field boundaries overlap (e.g., hostID="ab", keyspace="cd" vs hostID="a", keyspace="bcd").Signed-off-by: Yaniv Kaul yaniv.kaul@scylladb.com