Skip to content

[Java SDK] Optimize DoFnOutputReceivers and OutputBuilderSuppliers with direct build paths - #41814

Open
reuvenlax wants to merge 1 commit into
perf/windowed-valuesfrom
perf/dofn-output-receivers
Open

reuvenlax wants to merge 1 commit into
perf/windowed-valuesfrom
perf/dofn-output-receivers

Conversation

@reuvenlax

@reuvenlax reuvenlax commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #41813 (perf/windowed-values).

Avoids intermediate WindowedValues.Builder and lambda allocations when emitting elements through DoFnOutputReceivers and OutputBuilderSuppliers:

  • OutputBuilderSupplier / OutputBuilderSuppliers: Adds default build, buildWithTimestamp, and buildWindowedValue methods and implements them in ElementOutputBuilderSupplier by delegating directly to WindowedValues.ofWithCurrentContext(...) and WindowedValues.builder(template, value).
  • DoFnOutputReceivers: Implements direct output, outputWithTimestamp, and outputWindowedValue methods on WindowedContextOutputReceiver and RowOutputReceiver, caches fromRowFunction on RowOutputReceiver, and removes redundant WindowedValues.builder(...) wrapping in builder().
  • Updates SplittableParDo and SplittableParDoNaiveBounded to use WindowedValues.builder(template, value).
  • Adds unit tests in DoFnOutputReceiversTest and OutputBuilderSuppliersTest, plus JMH microbenchmarks (OutputReceiverBenchmark and OutputReceiverBenchmarkTest) covering both output receivers and FullWindowedValueCoder.

Performance Results

Measured via forked A/B JMH on JDK 21 (OutputReceiverBenchmark, Mode.AverageTime, -f 1 -wi 2 -i 3 -w 500ms -r 500ms -prof gc), comparing stack base #41813 (perf/windowed-values @ 718a3fe2ba8) vs. #41814 (perf/dofn-output-receivers @ 3db35d11a55) — both runs share identical WindowedValues / WindowedValue / IterableLikeCoder classes from #41813, isolating only DoFnOutputReceivers, OutputBuilderSupplier, and OutputBuilderSuppliers (lower ns/op is better):

Benchmark templateKind Base (ns/op) After (ns/op) Speedup Base (B/op) After (B/op) Alloc Δ
windowedReceiverOutputTagged GLOBAL_MIN 35.10 8.29 4.24× 208 40 -80.8% (-168 B)
windowedReceiverOutputTagged SINGLE_INTERVAL 32.08 8.43 3.81× 216 48 -77.8% (-168 B)
windowedReceiverOutputTagged MULTI_INTERVAL 33.06 8.74 3.78× 216 48 -77.8% (-168 B)
windowedReceiverOutputUntagged GLOBAL_MIN 32.31 6.20 5.21× 208 40 -80.8% (-168 B)
windowedReceiverOutputUntagged SINGLE_INTERVAL 31.67 8.41 3.77× 216 48 -77.8% (-168 B)
windowedReceiverOutputUntagged MULTI_INTERVAL 32.17 8.65 3.72× 216 48 -77.8% (-168 B)
windowedReceiverOutputViaBuilder GLOBAL_MIN 30.88 8.84 3.49× 208 40 -80.8% (-168 B)
windowedReceiverOutputViaBuilder SINGLE_INTERVAL 30.81 9.91 3.11× 216 48 -77.8% (-168 B)
windowedReceiverOutputViaBuilder MULTI_INTERVAL 31.11 9.73 3.20× 216 48 -77.8% (-168 B)
windowedReceiverOutputWithTimestamp GLOBAL_MIN 32.43 10.79 3.01× 216 48 -77.8% (-168 B)
windowedReceiverOutputWithTimestamp SINGLE_INTERVAL 35.07 8.75 4.01× 216 48 -77.8% (-168 B)
windowedReceiverOutputWithTimestamp MULTI_INTERVAL 33.63 8.98 3.75× 216 48 -77.8% (-168 B)
windowedReceiverOutputWindowedValue GLOBAL_MIN 36.26 10.58 3.43× 216 48 -77.8% (-168 B)
windowedReceiverOutputWindowedValue SINGLE_INTERVAL 37.83 10.31 3.67× 216 48 -77.8% (-168 B)
windowedReceiverOutputWindowedValue MULTI_INTERVAL 33.41 10.32 3.24× 216 48 -77.8% (-168 B)
windowedReceiverPerElementConstruction GLOBAL_MIN 34.32 5.61 6.12× 248 40 -83.9% (-208 B)
windowedReceiverPerElementConstruction SINGLE_INTERVAL 35.68 10.61 3.36× 256 88 -65.6% (-168 B)
windowedReceiverPerElementConstruction MULTI_INTERVAL 41.43 12.98 3.19× 256 88 -65.6% (-168 B)
rowReceiverOutput GLOBAL_MIN 41.89 11.46 3.65× 176 40 -77.3% (-136 B)
rowReceiverOutput SINGLE_INTERVAL 29.17 11.85 2.46× 184 48 -73.9% (-136 B)
rowReceiverOutput MULTI_INTERVAL 27.75 12.03 2.31× 184 48 -73.9% (-136 B)
rowReceiverOutputWithTimestamp GLOBAL_MIN 27.30 9.54 2.86× 184 48 -73.9% (-136 B)
rowReceiverOutputWithTimestamp SINGLE_INTERVAL 30.07 10.64 2.83× 184 48 -73.9% (-136 B)
rowReceiverOutputWithTimestamp MULTI_INTERVAL 30.07 9.81 3.07× 184 48 -73.9% (-136 B)
rowReceiverOutputViaBuilder GLOBAL_MIN 26.39 15.73 1.68× 176 112 -36.4% (-64 B)
rowReceiverOutputViaBuilder SINGLE_INTERVAL 26.84 17.99 1.49× 184 120 -34.8% (-64 B)
rowReceiverOutputViaBuilder MULTI_INTERVAL 28.18 17.53 1.61× 184 120 -34.8% (-64 B)

Key Takeaways

  • Direct WindowedContextOutputReceiver paths (3.0×–5.2× faster, -168 B/op / -77.8% to -80.8% allocation): Eliminating intermediate WindowedValues.Builder and capturing-lambda allocations drops every output call from ~31–38 ns (208–216 B/op) to ~6–11 ns (40–48 B/op, leaving only the emitted WindowedValue itself).
  • builder().output() path (3.1×–3.5× faster, -168 B/op): Removing the redundant WindowedValues.builder(...) wrapper brings windowedReceiverOutputViaBuilder down to the same 40–48 B/op floor.
  • Per-element receiver construction (3.2×–6.1× faster, -168 to -208 B/op): With the builder/lambda chain removed, C2 even scalar-replaces the transient receiver and supplier on GLOBAL_MIN (248 → 40 B/op, 6.12× faster).
  • RowOutputReceiver (2.3×–3.7× faster, -136 B/op on direct paths; 1.5×–1.7× faster, -64 B/op on builder path): Direct output/outputWithTimestamp plus caching fromRowFunction removes 136 B/op (-73.9% to -77.3%).

@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 emission hot paths in DoFnOutputReceivers and OutputBuilderSuppliers by introducing direct build methods (build, buildWithTimestamp, buildWindowedValue) on OutputBuilderSupplier and bypassing intermediate WindowedValues.Builder, WindowedValue<Row>, and receiver lambda allocations. It also caches SchemaCoder.getFromRowFunction() in RowOutputReceiver, removes redundant builder wrapping, and includes comprehensive unit tests and JMH benchmarks.

🔍 General Feedback

  • The fast-path implementations in ElementOutputBuilderSupplier, WindowedContextOutputReceiver, and RowOutputReceiver cleanly preserve all element metadata (timestamp, windows, PaneInfo, recordId, recordOffset, CausedByDrain, ValueKind, and active OpenTelemetry Context with Context.root() normalization) while maintaining @FunctionalInterface compatibility on OutputBuilderSupplier.
  • Test coverage in DoFnOutputReceiversTest and OutputBuilderSuppliersTest thoroughly exercises all WindowedValue shapes, tagged/untagged receivers, row receivers, and edge cases such as nested WindowedValue payloads and null elements.

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

@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

@reuvenlax
reuvenlax added this pull request to stack #41818 November 30, 2026 22:16
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @ahmedabu98 for label java.
Backup reviewer: damccorm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.59%. Comparing base (718a3fe) to head (3db35d1).

Additional details and impacted files
@@                   Coverage Diff                   @@
##             perf/windowed-values   #41814   +/-   ##
=======================================================
  Coverage                   59.59%   59.59%           
  Complexity                   5157     5157           
=======================================================
  Files                        1470     1470           
  Lines                      187850   187850           
  Branches                     3828     3828           
=======================================================
  Hits                       111942   111942           
  Misses                      72503    72503           
  Partials                     3405     3405           

☔ 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.

…uild paths

- OutputBuilderSupplier / OutputBuilderSuppliers: add direct build,
  buildWithTimestamp, and buildWindowedValue methods backed by
  WindowedValues.ofWithCurrentContext to avoid intermediate Builder allocations.
- DoFnOutputReceivers: implement direct output/outputWithTimestamp/
  outputWindowedValue on WindowedContextOutputReceiver and RowOutputReceiver,
  cache fromRowFunction, and drop redundant Builder copying.
- Update SplittableParDo and SplittableParDoNaiveBounded to use
  WindowedValues.builder(template, value).
- Add unit tests in DoFnOutputReceiversTest and OutputBuilderSuppliersTest,
  and JMH benchmarks in OutputReceiverBenchmark.
@reuvenlax
reuvenlax force-pushed the perf/dofn-output-receivers branch from 3db35d1 to 450947c Compare December 3, 2026 17:57
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