Skip to content

Optimize DoFnOutputReceivers and OutputBuilderSuppliers with direct build paths - #41808

Closed
reuvenlax wants to merge 2 commits into
apache:masterfrom
reuvenlax:perf/dofn-output-receivers
Closed

reuvenlax wants to merge 2 commits into
apache:masterfrom
reuvenlax:perf/dofn-output-receivers

Conversation

@reuvenlax

@reuvenlax reuvenlax commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Depends on the WindowedValues fast-paths PR (pr/41803) - for now contains all those changes.

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.

… helpers

- WindowedValues: add singleWindowOrNull, builder(template, value), and
  ofWithCurrentContext(...) overloads; track singleWindow in Builder without
  allocating wrapper collections; normalize OpenTelemetry Context.root() to null;
  mark concrete subclasses final and optimize of()/withValue()/explodeWindows().
- IterableLikeCoder & FullWindowedValueCoder: add @internal single-element and
  size-header helpers to avoid singleton-list allocations on single-window
  encode/decode/registerByteSizeObserver, reuse cached windowsCoder on multi-window
  decode, and check plain paneInfo metadata bit before volatile load.
- Add unit tests in WindowedValueTest and CollectionCoderTest.
…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.
@github-actions github-actions Bot added java core size/xl XL: >=1000 lines changed labels Nov 30, 2026

@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 introduces several well-targeted Java SDK hot-path optimizations, including direct WindowedValue construction via OutputBuilderSupplier.build(...), cached OutputReceiver and MultiOutputReceiver instances in SimpleDoFnRunner and FnApiDoFnRunner, a dedicated two-window DualWindowedValue representation, and direct byte-size accumulation in FullWindowedValueCoder and IterableLikeCoder. The changes are thoroughly benchmarked and well-covered by unit tests.

🔍 General Feedback

  • The new OutputBuilderSupplier.build default method and fast-path implementations cleanly bypass intermediate AutoValue_WindowedValues_Builder allocations while preserving context metadata (recordId, recordOffset, causedByDrain, openTelemetryContext).
  • One compatibility issue was identified where SimpleDoFnRunner.outputWindowedValue now passes null to WindowedValueMultiReceiver.output(TupleTag, WindowedValue) when emitting to the main output; several existing runner implementations (such as DirectRunner, Spark, and Flink) assume a non-null TupleTag and use it as a map key or check it with checkArgumentNotNull.

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

💬 Inline Findings

  • runners/core-java/src/main/java/org/apache/beam/runners/core/SimpleDoFnRunner.java (line 313): 🔴 Critical: outputWindowedValue(WindowedValue<OutputT>) delegates to outputWindowedValue((TupleTag<OutputT>) null, windowedElem), which now passes a null TupleTag directly to outputManager.output(outputTag, windowedElem). Previously, outputWindowedValue(WindowedValue<OutputT>) passed mainOutputTag (outputManager.output(mainOutputTag, windowedElem)). Several WindowedValueMultiReceiver implementations across runners require a non-null tag (e.g., ParDoEvaluator.BundleOutputManager in direct-java calls checkArgumentNotNull(bundles.get(tag), ...), and Spark/Samza/Jet runners index into maps or compare against mainOutputTag), which will cause NullPointerException or IllegalArgumentException on main outputs. Resolve null to mainOutputTag before calling outputManager.output(...).
    outputManager.output(outputTag == null ? (TupleTag<T>) mainOutputTag : outputTag, windowedElem);

@reuvenlax

Copy link
Copy Markdown
Contributor Author

Superseded by stacked PR #41814 (recreated from an apache/beam branch to use GitHub native stacked PRs).

@reuvenlax reuvenlax closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core java size/xl XL: >=1000 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant