Skip to content

Perf/simple dofn runner - #41539

Open
reuvenlax wants to merge 4 commits into
apache:masterfrom
reuvenlax:perf/simple-dofn-runner
Open

reuvenlax wants to merge 4 commits into
apache:masterfrom
reuvenlax:perf/simple-dofn-runner

Conversation

@reuvenlax

@reuvenlax reuvenlax commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Eliminates per-element allocations and redundant OpenTelemetry / builder / receiver overhead across SimpleDoFnRunner, WindowedValues, DoFnOutputReceivers, and FnApiDoFnRunner.

Key Changes

  • SimpleDoFnRunner (runners/core-java):
    • Reuses a single DoFnProcessContext and cached DirectOutputReceiver / CachedMultiOutputReceiver instances across @ProcessElement invocations (failing fast with IllegalStateException on post-return access or invalid reentrant calls).
    • Avoids allocating Instant on every timestamp check via primitive long fast-path arithmetic in checkTimestamp.
    • Uses two mutable tag slots (cachedMainTag, cachedSingleExtraTag) plus a lazy HashMap in CachedMultiOutputReceiver so even deserialized TupleTag instances (e.g. Dataflow) resolve in O(1) with zero allocation.
    • Adds a zero-Scope OpenTelemetry ContextStorage.get().current() fast path in processElement, and preserves empty-window elements (ValueInEmptyWindows) without extra validation overhead.
  • WindowedValues / IterableLikeCoder / DoFnOutputReceivers / OutputBuilderSupplier(s) (sdks/java/core):
    • Adds allocation-free WindowedValues.ofWithCurrentContext(...) and WindowedValues.builder(template, value) fast paths, omitting redundant Context.root() references.
    • Adds direct OutputBuilderSupplier.build(...) / buildWithTimestamp(...) / buildWindowedValue(...) methods to avoid intermediate WindowedValues.Builder and lambda allocations in DoFnOutputReceivers.
    • Adds single-window fast paths in FullWindowedValueCoder (encode, decode, registerByteSizeObserver) delegating wire-format header handling to IterableLikeCoder.readSizeHeader / decodeAfterSizeHeader / encodeSingleElement.
  • FnApiDoFnRunner (sdks/java/harness):
    • Uses WindowedValues.ofWithCurrentContext(...) and direct output-receiver fast paths.
  • Tests & Benchmarks:
    • Adds unit tests across SimpleDoFnRunnerTest, WindowedValueTest, DoFnOutputReceiversTest, OutputBuilderSuppliersTest, CollectionCoderTest, and FnApiDoFnRunnerTest, plus OutputReceiverBenchmark / OutputReceiverBenchmarkTest.

Benchmark Results (JDK 21, 2 forks, -prof gc, lower is better)

SimpleDoFnRunner (processElement end-to-end)

Benchmark Master (ns/op) Branch (ns/op) Speedup Alloc (B/op)
processElement_multiOutput2TagsDeserialized (2 outputs, distinct tag refs) 91.88 ± 10.76 20.66 ± 2.12 4.45× 536 → 96 (-82%)
processElement_outputReceiver (@Element, OutputReceiver) 52.38 ± 8.06 12.34 ± 1.46 4.25× 296 → 48 (-84%)
processElement_outputWithTimestamp 49.31 ± 9.41 12.58 ± 2.73 3.92× 296 → 48 (-84%)
processElement_multiOutput4Tags (4 outputs) 196.43 ± 49.91 56.19 ± 11.76 3.50× 1,028 → 192 (-81%)
processElement_observesWindow (FixedWindows) 47.43 ± 10.83 13.86 ± 2.02 3.42× 320 → 48 (-85%)
processElement_processContextOutput (ProcessContext.output) 11.24 ± 1.52 10.84 ± 1.84 1.04× 48 → 48

OutputReceiverBenchmark (sdks/java/core:jmh)

Benchmark Master (ns/op) Branch (ns/op) Speedup Alloc (B/op)
windowedReceiverOutputUntagged 24.25 7.44 3.26× 216 → 48 (-78%)
windowedReceiverOutputTagged 24.57 7.54 3.26× 216 → 48 (-78%)
windowedReceiverPerElementConstruction 26.19 7.98 3.28× 256 → 48 (-81%)
windowedReceiverOutputViaBuilder 26.09 9.33 2.80× 216 → 48 (-78%)
rowReceiverOutput 23.25 8.58 2.71× 184 → 48 (-74%)
rowReceiverOutputWithTimestamp 22.79 11.91 1.91× 184 → 48 (-74%)
windowedReceiverOutputWindowedValue 8.54 5.96 1.43× 48 → 48
fullWindowedValueCoderDecode 50.56 47.15 1.07× 512 → 512

Shows strong results in localized benchmarks - e.g. Nexmark Q5 is 7.5% faster on DirectRunner. Combined with its sibling PRs, we see 25% savings on many real e2e Dataflow pipelines.

Note: if a user tries to hold onto a ProcessContext between elements (e.g. buffer in the DoFn), this will now break. This was always abroken/undefined usage however.

@github-actions github-actions Bot added java runners core size/xl XL: >=1000 lines changed labels Nov 20, 2026
@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 (or comment /summarize-failures for a summary of the failing checks).

…ivers / FnApiDoFnRunner

- WindowedValues: add ofWithCurrentContext(...) and builder(template, value) helpers,
  omit redundant OpenTelemetry Context.root() references, and add SingleWindowedValue
  fast paths in FullWindowedValueCoder encode/decode/registerByteSizeObserver.
- OutputBuilderSupplier / OutputBuilderSuppliers / DoFnOutputReceivers: add direct
  build(value) and buildWithTimestamp(value, ts) methods avoiding intermediate Builder
  and lambda allocations on every emitted element.
- FnApiDoFnRunner: use WindowedValues.ofWithCurrentContext(...) and builder(elem, val)
  in output receivers.
- Add unit tests in WindowedValueTest and JMH benchmarks in OutputReceiverBenchmark.
@reuvenlax
reuvenlax force-pushed the perf/simple-dofn-runner branch from e904cdf to 1772c58 Compare November 28, 2026 05:03
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