Skip to content

perf(storage): reuse pooled buffer for streamed puts - #241

Merged
ovaistariq merged 2 commits into
mainfrom
perfloop-pr-open-hr4wx04w3p
Aug 28, 2026
Merged

ovaistariq merged 2 commits into
mainfrom
perfloop-pr-open-hr4wx04w3p

Conversation

@perfloop

@perfloop perfloop Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Local streamed Puts that cross the inline threshold join the consumed prefix to an io.PipeReader. io.MultiReader can select WriteTo and bypass FileManager's pooled copy buffer. Keep pipe-backed remainders reader-only so the existing buffer drives the copy.

The change

joinPrefix serves the prefix before the remainder. It preserves the direct WriterTo path for byte-backed unary Puts, so that path does not take the streamed copy behavior. The pipe-backed path still writes the prefix and remainder in order.

Move raw-file payload copying and checksum calculation into a helper used by FileManager. Add coverage for prefix ordering, short writes, checksums, payload readback, and the local streamed Put path. The new benchmark covers threshold-sized, 1 MiB, and raw-file sync-boundary inputs.

This update replays the two commits on current main with matching DCO sign-offs; the source patch is unchanged.

Verification

  • make fmt-check
  • make vet
  • Focused raw-file payload, prefix reader, and large-object storage tests
  • BenchmarkCacheServicePutLocalStreamFirstWrite

Workload: four-worker first-write local streamed CacheService.Put allocation series with one Go execution thread, 100,000 fixed 64 KiB threshold-plus-two Puts

Metric Before After Change
B/op 38032 4289 88.7% lower
Checks: 3 passed.

Generated by Perfloop. Measurements and checks.


Note

Medium Risk
Changes the large-object Put I/O path in storage and file writes; behavior is intentionally split between streamed and byte-backed readers, with tests and benchmarks but still on a performance-critical hot path.

Overview
Streamed Puts that spill past the inline threshold no longer use io.MultiReader to stitch the peeked prefix to the rest of the body. joinPrefix serves the prefix first and only exposes io.WriterTo when the remainder already implements it (e.g. unary bytes.NewReader puts), so pipe-backed local streams stay reader-only and FileManager’s copyRawFilePayload 1 MiB pooled buffer drives the remainder copy instead of an extra scratch allocation path.

Raw-file streaming and CRC32 checksum logic move into copyRawFilePayload in storage/files/payload.go, with FileManager.Write calling that helper. Tests cover prefix ordering, WriterTo preservation, short writes, and checksums; TestStorage_PutGetDelete_LargeObject asserts the direct-write path for WriterTo inputs. BenchmarkCacheServicePutLocalStreamFirstWrite measures concurrent first-write local streamed puts (threshold-sized, 1 MiB, and sync-boundary sizes) including allocation and live-heap metrics.

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

@greptile-apps

greptile-apps Bot commented Aug 24, 2026

Copy link
Copy Markdown

Greptile Summary

The PR changes oversized streamed local puts to retain a reader-only pipe remainder so FileManager's pooled buffer performs the copy, while preserving WriterTo for byte-backed unary puts.

  • Adds prefix-reader implementations for buffered and direct-write paths.
  • Extracts raw-file copying and CRC32 calculation into a focused helper.
  • Adds correctness tests and a streamed-put performance benchmark covering allocation, heap, latency, throughput, and readback.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete correctness, security, or repository-rule issue identified.

The changed reader composition preserves payload order and error handling for the reachable pipe-backed and byte-backed inputs, while the payload-copy extraction remains behaviorally equivalent to the previous implementation.

Important Files Changed

Filename Overview
storage/storage.go Introduces prefix-aware reader composition that preserves byte ordering while selecting pooled copying or the existing WriterTo path based on the remainder.
storage/files/payload.go Extracts the existing pooled copy and CRC32 calculation without changing its success, error, or byte-count behavior.
storage/files/manager.go Delegates raw-file payload copying to the new helper while retaining file cleanup and durability handling.
storage/prefix_reader_test.go Covers prefix ordering, pooled-buffer use, WriterTo preservation, byte counts, and short-prefix-write handling.
server/service/streaming_put_benchmark_test.go Adds a benchmark for concurrent first-write local streams with readback and memory, GC, latency, and throughput metrics.

Reviews (1): Last reviewed commit: "perf: preserve streamed copy write bound..." | Re-trigger Greptile

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-hr4wx04w3p branch from e70e0bc to 47a66bb Compare August 24, 2026 17:48
@ovaistariq
ovaistariq merged commit e3d8358 into main Aug 28, 2026
13 checks passed
@ovaistariq
ovaistariq deleted the perfloop-pr-open-hr4wx04w3p branch August 28, 2026 21:22
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.12.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants