Skip to content

[Dataflow] Optimize SimpleParDoFn dispatch, output routing, and state cleanup - #41806

Open
reuvenlax wants to merge 3 commits into
apache:masterfrom
reuvenlax:perf/dataflow-simple-pardo-fn
Open

reuvenlax wants to merge 3 commits into
apache:masterfrom
reuvenlax:perf/dataflow-simple-pardo-fn

Conversation

@reuvenlax

Copy link
Copy Markdown
Contributor

Reduces per-element dispatch and allocation overhead in Dataflow worker SimpleParDoFn and SimpleParDoFnHelpers:

SimpleParDoFn: Outlines lazyStartBundle() and processWithSideInputs() to keep processElement()/onProcessWindowedValue() small, and skips elem.getWindows() entirely on stateless DoFns.
SimpleParDoFnHelpers: Caches hasState, isStreaming, and mainReceiverIndex instead of re-deriving them per element, and resets bundle state in finally on abort().
OutputManager: Replaces the anonymous output manager with a dedicated class using reference-equality fast paths for mainOutputTag (and a deserialized altMainTag), a bounded per-slot identity cache (<= 8 outputs), and lazily allocated undeclaredOutputs.
State cleanup: Fast-paths known GlobalWindow WindowedValue implementations in registerStateCleanup() without allocating/iterating elem.getWindows().
Adds unit tests in SimpleParDoFnHelpersTest covering hasState lifecycle/abort cleanup, multi-output/undeclared/null-tag routing across bundles, and state cleanup fast paths.

@github-actions github-actions Bot added runners dataflow size/l L: 250-999 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 optimizes hot-path execution in SimpleParDoFn and SimpleParDoFnHelpers by replacing the anonymous WindowedValueMultiReceiver with a static OutputManager featuring identity-based TupleTag caching, precomputing hasState per bundle, and adding a GlobalWindow fast path for state cleanup registration. It also hardens bundle lifecycle state cleanup in abort() and adds comprehensive unit test coverage in SimpleParDoFnHelpersTest.

🔍 General Feedback

  • The OutputManager identity caching (mainOutputTag, altMainTag, and bounded cachedTags) cleanly handles both direct c.output(elem) calls and deserialized static final TupleTag instances without hash map lookups or write-barrier thrashing.
  • Extracting cold paths (lazyStartBundle, processWithSideInputs) and avoiding WindowedValue#getWindows() / iterator allocations on ValueInGlobalWindow and TimestampedValueInGlobalWindow are well-targeted JIT/allocation improvements.
  • State lifecycle management (hasState, fnSignature, and startBundle preconditions) and unit tests are thorough and well-structured.

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

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.47009% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.88%. Comparing base (4fb84c7) to head (a4fa7c2).
⚠️ Report is 24 commits behind head on master.

Files with missing lines Patch % Lines
...he/beam/runners/dataflow/worker/SimpleParDoFn.java 47.61% 11 Missing ⚠️
.../runners/dataflow/worker/SimpleParDoFnHelpers.java 93.61% 2 Missing and 4 partials ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             master   #41806       +/-   ##
=============================================
- Coverage     61.24%   58.88%    -2.37%     
+ Complexity    16932     2342    -14590     
=============================================
  Files          2856     1136     -1720     
  Lines        279320   171087   -108233     
  Branches      13478     1567    -11911     
=============================================
- Hits         171068   100740    -70328     
+ Misses       101414    67789    -33625     
+ Partials       6838     2558     -4280     
Flag Coverage Δ
java 76.53% <85.47%> (+10.16%) ⬆️

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

Assigning reviewers:

R: @kennknowles added as fallback since no labels match configuration
Backup reviewer: Abacn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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

@reuvenlax

Copy link
Copy Markdown
Contributor Author

Also R: @arunpandianp as you've recently been in this code.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

Comment on lines +183 to +184
@Nullable Receiver mainReceiver =
receivers != null && mainReceiverIndex >= 0 && mainReceiverIndex < receivers.length

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.

any reason to not move this to OutputManager constructor?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// checks across concurrent worker threads.
//
// TODO: Once Beam is on JDK 24, we can revert to a simpler instanceof call.
private static final Class<?> VALUE_IN_GLOBAL_WINDOW_CLASS =

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.

can we reuse constants in runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/DataflowOutputCounter.java?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, though I'm not sure having SimpleParDoFn read the constants out of DataflowOutputCounter is cleaner. Added a comment to cleanup. Also it does seem like this is a workaround to a hotspot bug that will be fully fixed by JDK 24, so we might be able to remove entirely then.

activeFnInfo = null;
fnSignature = null;
hasState = false;
sideInputProcessor = null;

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.

nit: sideInputProcessor = null can be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

if (hasState) {
// These elements are now processed. Register cleanup timers for all the unblocked
// windows.
helpers.registerStateCleanup(

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.

this can call the new registerStateCleanup method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

- Outline lazyStartBundle and processWithSideInputs in SimpleParDoFn and skip
  elem.getWindows() on stateless DoFns.
- Cache hasState, isStreaming, and mainReceiverIndex in SimpleParDoFnHelpers
  and reset fields in finally on abort().
- Replace anonymous outputManager with OutputManager using reference-equality
  fast paths for mainOutputTag/altMainTag, a bounded per-slot tag cache for
  multi-output DoFns, and lazy undeclaredOutputs map allocation.
- Fast-path GlobalWindow state cleanup timer registration without calling
  elem.getWindows().
@reuvenlax
reuvenlax force-pushed the perf/dataflow-simple-pardo-fn branch from cc26f04 to a4fa7c2 Compare December 2, 2026 04:25
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.

2 participants