Skip to content

perf: stream large cache hits without pipe staging - #197

Merged
ovaistariq merged 3 commits into
mainfrom
perfloop-pr-open-mej8rnv47a
Sep 3, 2026
Merged

ovaistariq merged 3 commits into
mainfrom
perfloop-pr-open-mej8rnv47a

Conversation

@perfloop

@perfloop perfloop Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Stream large whole-object cache hits directly to the response writer instead of staging them through io.Pipe, a goroutine, and a second copy. A lazy writer commits cached headers and X-Cache: HIT on the first non-empty write, so empty and pre-commit failures still fall back safely.

After commitment, a cache stream failure still avoids appending an upstream response, but now records the request as an error so truncated transfers are visible in request metrics. Small-object, range, block, and miss paths remain unchanged.

Testing

  • make lint-ci
  • make test
  • go test -race -run '^(TestServeFromCache_LargeObject|TestServeFromCache_LargeObjectUncommittedBeforeFirstByte|TestHandleGetObject_LargeEmptyCacheHitFallsBackBeforeHeaders|TestHandleGetObject_LargeCacheHitPostCommitErrorDoesNotFallBack)$' ./proxy

The benchmark covers warmed 128 KiB, 1 MiB, and 4 MiB embedded-cache hits through handlers.Server.

Workload: warmed 128 KiB full-object embedded-cache HTTP GET through handlers.Server

Metric Before After Change
B/op 77632 44200 43.1% lower
allocs/op 142 134 5.6% lower

Workload: warmed 1 MiB full-object embedded-cache HTTP GET through handlers.Server

Metric Before After Change
B/op 77824 44395 43% lower

Workload: warmed 4 MiB full-object embedded-cache HTTP GET through handlers.Server

Metric Before After Change
B/op 78639 45039 42.7% lower
Checks: 2 passed.

Generated by Perfloop. Measurements and checks.


Note

Medium Risk
Changes hot-path GET/cache-hit response commitment and error propagation for large objects; incorrect lazy-commit logic could break fallback or leave clients with truncated bodies, though new tests cover the main edge cases.

Overview
Large whole-object cache hits no longer go through io.Pipe, a background goroutine, and an extra copy. serveFromCache streams the cache body straight to the client via a new lazyCommitWriter that writes cached headers and X-Cache: HIT only on the first non-empty chunk, so empty streams and failures before any body bytes still allow upstream fallback without sending cache-hit headers.

If streaming fails after the response is committed, the handler no longer returns that error (avoiding a second upstream response appended to a partial body) but records the request as an error in metrics and logs a warning. Small-object buffering, range/block paths, and miss behavior are unchanged aside from comment tweaks on the 64KB threshold.

Adds proxy/cache_hit_stream_test.go for multi-chunk success, pre-commit failure, empty-body miss fallback, and post-commit failure/metrics behavior, plus BenchmarkHandlerWarmedEmbeddedCacheHit in cmd/tag for warmed 128 KiB–4 MiB embedded-cache GETs through handlers.Server.

Reviewed by Cursor Bugbot for commit c48ec45. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR replaces pipe-staged delivery of large whole-object cache hits with direct streaming through a lazy response writer.

  • Commits cached headers only after the first non-empty body write, preserving upstream fallback for empty or pre-commit failures.
  • Classifies post-commit stream failures as request errors without appending an upstream response to the partial body.
  • Adds focused cache-stream regression tests and warmed embedded-cache benchmarks.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
proxy/get_object.go Adds the lazy writer that commits cache-hit headers on the first non-empty write and tracks streamed bytes.
proxy/revalidation.go Streams large cache hits directly and correctly records committed stream failures as errors while preventing unsafe fallback.
proxy/cache_hit_stream_test.go Covers successful direct streaming, pre-commit failure, empty-body fallback, and post-commit error metrics.
cmd/tag/cache_hit_benchmark_test.go Adds end-to-end warmed embedded-cache benchmarks for several large-object sizes.

Reviews (2): Last reviewed commit: "perf: classify incomplete cache streams ..." | Re-trigger Greptile

Comment thread proxy/revalidation.go Resolved
@ovaistariq

Copy link
Copy Markdown
Contributor

@greptile

@greptile-apps

greptile-apps Bot commented Aug 29, 2026

Copy link
Copy Markdown

Want your agent to iterate on Greptile's feedback? Try greploops.

@ovaistariq
ovaistariq merged commit c4ee248 into main Sep 3, 2026
12 checks passed
@ovaistariq
ovaistariq deleted the perfloop-pr-open-mej8rnv47a branch September 3, 2026 00:32
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 3, 2026
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.21.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants