Skip to content

cache: avoid copying serialized entries on L1 reads - #4396

Merged
MustafaSaber merged 2 commits into
zalando:masterfrom
perfloop-oss:perfloop-pr-open-4ry0jfmdv6
Nov 3, 2026
Merged

MustafaSaber merged 2 commits into
zalando:masterfrom
perfloop-oss:perfloop-pr-open-4ry0jfmdv6

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Warm L1 HTTP cache reads no longer clone the serialized response before decoding it. The decoder reads privately owned bytes after releasing the shard lock.

4 KiB body: B/op 30.5 kB → 24.34 kB (−20.2%), ns/op 39.7 µs → 36.59 µs; medians of 9 paired runs.

64 KiB body: B/op 237.8 kB → 147.4 kB (−38%), ns/op 256.7 µs → 219.1 µs (−15.2%); medians of 11 paired runs.

Per-metric results
Workload Measure Metric Before median After median Paired change Outcome / requirement Paired runs
warm L1 HTTP cache hit (4 KiB) allocated bytes B/op 30.5 kB 24.34 kB −20.2% improvement passed 9
warm L1 HTTP cache hit (4 KiB) time ns/op 39.7 µs 36.59 µs — non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| ns 9
warm L1 HTTP cache hit (64 KiB) allocated bytes B/op 237.8 kB 147.4 kB −38% improvement passed 11
warm L1 HTTP cache hit (64 KiB) time ns/op 256.7 µs 219.1 µs −15.2% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| ns 11

Changes

  • Reuse the shard lookup and LRU promotion, borrowing bytes owned by the entry store.
  • Keep the defensive copy on the public byte-store API and decode fresh payloads, headers and Vary metadata for each storage read.

The stored JSON representation, byte-based eviction, freshness windows and response selection are unchanged. Misses and L2 fetches are outside the optimization.

Testing

BenchmarkCacheHit measures complete serial warm-handler GETs with empty, 4 KiB and 64 KiB text bodies. It includes routing, decoding, default tracing, response writing and access-log formatting into a discard sink. Reproduce with go test ./filters/cache -run '^$' -bench '^BenchmarkCacheHit$' -benchmem.

These are in-process warm L1 measurements, not network latency or deployed traffic savings.

TestShardedByteLRU_GetIsolation checks defensive byte copies. TestLRUStorage_GetIsolation checks nested mutable results. TestLRUStorage_SnapshotLifetime checks retained bytes across overwrite, deletion, eviction and expiry; TestLRUStorage_ConcurrentReads exercises readers and writers under the race detector. Existing cache filter tests cover GET, HEAD, conditional 304 and Vary results.

The Docker-backed filters/cache.TestValkeyStorage_GetSetDelete and filters/cache.TestValkeyStorage_FallsBackToL1OnValkeyUnavailable require a Docker daemon, which is unavailable here; they remain for target CI.

5 recorded checks pass with this change.

  • HTTP cache tests
  • HTTP cache race tests
  • HTTP cache vet
  • HTTP cache staticcheck
  • Go formatting

Not run locally: TestValkeyStorage_GetSetDelete, TestValkeyStorage_FallsBackToL1OnValkeyUnavailable. These tests start Valkey using testcontainers and require an unavailable Docker daemon.

Related work: #4395 also touches the L1 store for cache metrics.

Case and evidence


Generated by Perfloop. Human sponsor: Tomás Senart.

@perfloop-agent
perfloop-agent marked this pull request as ready for review November 1, 2026 21:21
@szuecs szuecs added optimization minor no risk changes, for example new filters labels Nov 2, 2026
@szuecs
szuecs requested a review from larry-dalmeida November 2, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor no risk changes, for example new filters optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants