Skip to content

perf: extract value_length off the wire in size accounting - #207

Merged
ovaistariq merged 1 commit into
mainfrom
fix/size-accounting-skip-payload-decode
Aug 14, 2026
Merged

ovaistariq merged 1 commit into
mainfrom
fix/size-accounting-skip-payload-decode

Conversation

@ovaistariq

@ovaistariq ovaistariq commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Two size-accounting paths consume only ValueMessage.value_length, but each fully proto.Unmarshal'd the metadata row — which allocates and copies the inline Data payload (up to the 64 KiB inline threshold) that neither caller reads:

On an inline-heavy cache this is a discarded payload copy + message allocation for every inline row scanned.

Fix

Add a shared protowire extractor, valueMessageValueLength, that scans and validates the whole message but reads only field 7, skipping Data (field 2) without materializing it. It preserves the callers' existing semantics exactly:

  • empty message → (0, true);
  • repeated field 7 → last value wins (matching proto scalar merge);
  • structurally malformed record → ok=false, which maps onto the same "return 0 / skip the row" fallback the callers already took on a proto.Unmarshal error.

Wire it into both sites.

Numbers

BenchmarkValueLength_Inline64KiB (64 KiB inline row):

ns/op allocs/op B/op
full-unmarshal 6331 2 65,680
wire-extract 13.2 0 0

Raw-file and segment rows (the large-object workload) carry no Data, so their behavior is unchanged — the win is specifically the allocation churn on inline-heavy metadata scans.

Test

storage/value_wire_test.go differential-tests the extractor against proto.Unmarshal across the ValueMessage shapes Put writes (inline/raw-file/segment/empty/all-fields), plus duplicate-field last-wins and malformed/truncated inputs. Full make test-storage passes (incl. #205's TestPut_Overwrite_TotalSizeStaysExact and the cleaner tests).

Provenance

Surfaced by Perfloop frontier cases case_2j2veg0hs5 and case_0pe7dc8ta7 (pattern avoidable_copy), confirmed against source before implementing.

🤖 Generated with Claude Code


Note

Low Risk
Localized performance change with tests asserting parity with proto.Unmarshal; size accounting semantics are preserved on success and error paths.

Overview
Size accounting no longer fully unmarshals each metadata ValueMessage when it only needs value_length. A shared valueMessageValueLength helper scans protobuf wire format with protowire, reads field 7, and skips field 2 (Data) without copying—avoiding up to 64 KiB allocations per inline row.

That helper replaces proto.Unmarshal in existingValueLength (Put overwrite deltas) and calculateTotalSize (cleaner startup and hourly reconciliation scans). Malformed records still yield ok=false, mapping to the same return-0 / skip-row behavior as before.

storage/value_wire_test.go differential-tests the extractor against full decode (inline/raw/segment shapes, duplicate field last-wins, malformed input) plus a 64 KiB inline benchmark.

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

existingValueLength (the Put-overwrite delta) and calculateTotalSize (the
cleaner's startup + hourly total-size scan) both consume only
ValueMessage.value_length, yet each fully proto.Unmarshal'd the row —
allocating and copying the inline Data payload (up to the 64 KiB inline
threshold) that neither caller reads.

Add a shared protowire extractor, valueMessageValueLength, that scans and
validates the whole message but reads only field 7, skipping Data without
materializing it. It preserves the callers' existing semantics: an empty
message yields (0, true), a repeated field 7 takes the last value (proto
scalar last-wins), and a structurally malformed record yields ok=false
(the same "return 0 / skip row" fallback proto.Unmarshal errors took).

For a 64 KiB inline row this turns the per-row decode from 6331 ns / 2
allocs / 65,680 B into 13 ns / 0 allocs / 0 B, cutting allocation churn on
the metadata scan for inline-heavy caches. Raw-file/segment rows (the
large-object workload) carry no Data, so behavior there is unchanged.

A differential test cross-checks the extractor against proto.Unmarshal
across the ValueMessage shapes Put writes, plus duplicate-field and
malformed inputs. make test-storage passes.

Surfaced by Perfloop cases case_2j2veg0hs5 and case_0pe7dc8ta7.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YCoN4Him5ZY4hZovVp37ra
Signed-off-by: Ovais Tariq <[email protected]>
@greptile-apps

greptile-apps Bot commented Aug 14, 2026

Copy link
Copy Markdown

Greptile Summary

This PR optimizes logical-size accounting by extracting ValueMessage.value_length directly from protobuf wire data instead of allocating and copying complete inline payloads.

  • Adds a shared, allocation-free wire-level extractor that validates the complete message.
  • Uses the extractor for Put overwrite accounting and cleaner reconciliation scans.
  • Adds differential tests for canonical storage shapes, duplicate fields, malformed inputs, and a 64 KiB benchmark.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete changed-code failure identified.

The new extractor matches the stored ValueMessage schema and repository-generated encodings, while both accounting callers retain their prior handling of malformed records and preserve existing overwrite and reconciliation invariants.

Important Files Changed

Filename Overview
storage/value_wire.go Adds a focused protowire scanner that preserves canonical protobuf scalar semantics while avoiding payload materialization.
storage/value_wire_test.go Differentially covers storage-generated message shapes, duplicate-field last-wins behavior, malformed input, and the intended allocation improvement.
storage/storage.go Replaces full decoding in overwrite accounting with the shared extractor while retaining the existing zero-on-decode-failure fallback.
storage/cleaner.go Uses the extractor during metadata reconciliation without changing snapshot or counter-delta behavior.

Reviews (1): Last reviewed commit: "perf: extract value_length off the wire ..." | Re-trigger Greptile

@ovaistariq
ovaistariq merged commit 9953384 into main Aug 14, 2026
13 checks passed
@ovaistariq
ovaistariq deleted the fix/size-accounting-skip-payload-decode branch August 14, 2026 17:58
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.10.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.

2 participants