Skip to content

Optimize per-element metadata overhead. - #41739

Open
reuvenlax wants to merge 3 commits into
apache:masterfrom
reuvenlax:perf/skip-default-element-metadata
Open

reuvenlax wants to merge 3 commits into
apache:masterfrom
reuvenlax:perf/skip-default-element-metadata

Conversation

@reuvenlax

Copy link
Copy Markdown
Contributor

Summary

Avoids allocating, serializing, and parsing ElementMetadata proto payloads on the wire when all metadata fields have their wire defaults (CausedByDrain.NORMAL, ValueKind.INSERT, no non-root OpenTelemetry Context), and decodes Windmill message metadata in a single pass.

Per beam:coder:windowed_value:v2, metadata_length and metadata are only present when the 0x80 bit is set on the first byte of PaneInfo. Previously, enabling element_metadata_supported unconditionally set 0x80 and allocated/serialized/parsed an ElementMetadata proto + byte[] on every element, even when all fields were defaults.

Key Changes

  • FullWindowedValueCoder & ValueInSingleWindow.Coder (sdks/java/core):
    • Added @Internal FullWindowedValueCoder.hasNonDefaultElementMetadata(...) and omit the ElementMetadata payload (clearing the 0x80 bit in PaneInfo) when all fields are wire defaults.
    • When non-default metadata is present, omit default proto fields (drain when NORMAL, value_kind when INSERT) to shrink wire payloads.
    • On decode, check plain paneInfo.isElementMetadata() before volatile isMetadataSupported(), and skip OpenTelemetryContextPropagator.read(...) when traceparent and tracestate are empty.
  • PaneInfo / PaneInfoCoder (sdks/java/core):
    • Short-circuit PaneInfo.withElementMetadata(boolean) to return this when the flag is unchanged (eliminates new PaneInfo(...) allocation for interned FIRST panes on encode/decode).
    • Removed duplicate chooseEncoding(value) call in PaneInfoCoder.encode.
  • WindmillSink, UngroupedWindmillReader, & WindmillKeyedWorkItem (runners/google-cloud-dataflow-java/worker):
    • Avoid allocating ElementMetadata.Builder in WindmillSink.add when metadata is unsupported or all-default; omit default metadata bytes in WindmillSink.encodeMetadata and write non-default protos directly via metadata.writeTo(stream).
    • Decode Windmill message metadata (PaneInfo, windows, optional ElementMetadata) in a single InputStream pass instead of 3 separate passes.
    • Parse trailing Windmill ElementMetadata via zero-copy ByteString.substring(...) instead of parseFrom(InputStream) (which allocates a 4 KB CodedInputStream buffer per call), and return ElementMetadata.getDefaultInstance() when unset.
  • Tests:
    • Added wire-elision, round-trip, and PaneInfo reuse unit tests in WindowedValueTest, PaneInfoTest, and UngroupedWindmillReaderTest.

Wire Compatibility

  • Old producer → New consumer: Existing wire payloads with 0x80 set continue to decode cleanly in all updated decoders.
  • New producer → Old consumer: Default elements encode identically to isMetadataSupported() == false (0x80 cleared), which existing decoders already handle by defaulting to (NORMAL, null/root, INSERT). Non-default elements omit default enum fields (DRAIN_MODE_UNSPECIFIED, VALUE_KIND_UNSPECIFIED), which existing decoders already map to NORMAL and INSERT.

Benchmark Results (JDK 21, 2 forks, -prof gc, setMetadataSupported() ON)

Wire Size (Default Element)

Coder Master Branch Delta
FullWindowedValueCoder 48 B 43 B -5 B (-10.4%)
ValueInSingleWindow.Coder 44 B 39 B -5 B (-11.4%)
WindmillSink metadata 20 B 16 B -4 B (-20.0%)

Latency & Allocations (lower is better)

Benchmark Master (ns/op) Branch (ns/op) Speedup Alloc (B/op)
windmillKeyedWorkItem_decode_default 227.9 ± 38.6 132.3 ± 24.1 1.72× 1,212 → 800 (-34%)
fullWindowedValueCoder_decode_default 90.8 ± 26.6 57.5 ± 7.6 1.58× 552 → 328 (-41%)
valueInSingleWindowCoder_encode_default 28.4 ± 4.9 18.3 ± 2.6 1.55× 232 → 88 (-62%)
valueInSingleWindowCoder_decode_default 75.1 ± 6.1 49.7 ± 4.4 1.51× 504 → 280 (-44%)
fullWindowedValueCoder_encode_default 26.6 ± 4.6 17.9 ± 2.4 1.49× 208 → 88 (-58%)
windmillSink_encodeMetadata_default* 35.6 ± 4.0 25.9 ± 2.3 1.37× 376 → 272 (-28%)
windmillKeyedWorkItem_decode_drain (non-default) 219.7 ± 54.5 174.3 ± 18.4 1.26× 1,212 → 1,032 (-15%)
fullWindowedValueCoder_encode_traced (non-default) 94.4 ± 16.4 76.4 ± 9.7 1.24× 384 → 336 (-13%)

*WindmillSink.add() also skips allocating ElementMetadata.Builder + build() altogether before calling encodeMetadata.

Verification

  • :sdks:java:core: spotlessCheck, checkstyleMain/Test, spotbugsMain, 18/18 tests (WindowedValueTest, PaneInfoTest, MetadataPropagationTest, ValueKindTest).
  • :runners:google-cloud-dataflow-java:worker: spotlessCheck, checkstyleMain/Test, 26/26 tests (UngroupedWindmillReaderTest, WindmillKeyedWorkItemTest, WindmillSinkTest, StreamingGroupAlsoByWindowFnsTest, StreamingGroupAlsoByWindowsReshuffleDoFnTest, WindowingWindmillReaderTest).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR optimizes element metadata encoding and decoding across WindowedValues, ValueInSingleWindow, WindmillSink, UngroupedWindmillReader, and WindmillKeyedWorkItem by avoiding intermediate ElementMetadata builder allocations when metadata is at default values and decoding Windmill metadata in a single pass. The changes preserve wire compatibility and include unit test coverage for full and partial element metadata round-tripping.

🔍 General Feedback

  • Single-pass decoding via WindmillSink.decodeMetadata and zero-copy slicing via ByteString.substring cleanly eliminate redundant PaneInfo and window decoding passes when reading from Windmill.
  • Short-circuiting hasNonDefaultElementMetadata and checking paneInfo.isElementMetadata() before the volatile isMetadataSupported() read reduce hot-path overhead when element metadata is unused.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.46154% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.91%. Comparing base (1c20e38) to head (c5c0d04).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...che/beam/runners/dataflow/worker/WindmillSink.java 84.31% 2 Missing and 14 partials ⚠️
...worker/WindmillOpenTelemetryContextPropagator.java 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41739      +/-   ##
============================================
- Coverage     59.59%   58.91%   -0.68%     
+ Complexity     5157     2342    -2815     
============================================
  Files          1470     1135     -335     
  Lines        187850   170674   -17176     
  Branches       3828     1530    -2298     
============================================
- Hits         111942   100554   -11388     
+ Misses        72503    67564    -4939     
+ Partials       3405     2556     -849     
Flag Coverage Δ
java 76.31% <88.46%> (+5.43%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@github-actions github-actions Bot added size/xl XL: >=1000 lines changed and removed size/m M: 50-249 lines changed labels Nov 29, 2026
@reuvenlax reuvenlax changed the title Optimize element metadata in the common case Optimize per-element metadata overhead. Nov 29, 2026
@reuvenlax

Copy link
Copy Markdown
Contributor Author

@kennknowles I think you know the most about element metadata. I'm trying to address extra overhead that runs per element (per-bundle overhead is ok, but per-element overhead is much more expensive and a scalability blocker). Metadata and also the OpenTelemetry contexts are per element, so trying to minimize that.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant