Skip to content

[Core] Elide finished trigger state when no subtrigger can finish - #41719

Open
Anurag-S1ngh wants to merge 1 commit into
apache:masterfrom
Anurag-S1ngh:perf/17961-trigger-finished-set
Open

Anurag-S1ngh wants to merge 1 commit into
apache:masterfrom
Anurag-S1ngh:perf/17961-trigger-finished-set

Conversation

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Summary

Elide the finished-subtrigger bitset in TriggerStateMachineRunner when no trigger in the tree can leave a finished bit set. Today only DefaultTriggerStateMachine takes this shortcut. This extends the shortcut to Repeatedly.forever(...) over once-style triggers, plus NeverStateMachine and ReshuffleTriggerStateMachine.

For these trees the bitset is read on every element and timer, but no bit can survive between calls. Bits set during onFire are cleared by Repeatedly before the state is persisted, and onMerge only sets a bit when a merging window already had one stored. So the state is always empty and the reads are wasted work.

Changes

  • Compute whether the finished set is needed once in the constructor instead of on every call.
  • Add a conservative recursive check for elidable trees:
    • DefaultTriggerStateMachine, NeverStateMachine, ReshuffleTriggerStateMachine.
    • RepeatedlyStateMachine whose repeated subtree is once-style: AfterPaneStateMachine, AfterDelayFromFirstElementStateMachine, AfterWatermarkStateMachine.FromEndOfWindow, AfterFirstStateMachine or AfterAllStateMachine of once-style triggers, and nested RepeatedlyStateMachine.
  • Keep the finished set for AfterEachStateMachine, OrFinallyStateMachine, AfterWatermarkStateMachine with late firings, and when the new-window optimization is enabled.
  • Add tests covering elided and non-elided trigger shapes.

Testing

  • ./gradlew :runners:core-java:test --tests "org.apache.beam.runners.core.triggers.*" --tests "org.apache.beam.runners.core.ReduceFnRunnerTest" (142 tests passed)
  • ./gradlew :runners:core-java:spotlessCheck :runners:core-java:checkstyleMain :runners:core-java:checkstyleTest

Fixes #17961.

Cache whether the finished-subtrigger bitset is needed and skip it for triggers that never persist finished bits, including Repeatedly over once-style triggers. Keep the state for AfterEach, OrFinally, late AfterWatermark, and the new-window optimization.
@github-actions github-actions Bot added runners core size/m M: 50-249 lines changed labels Nov 27, 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 TriggerStateMachineRunner by precomputing finishedSetNeeded and eliding FINISHED_BITS_TAG state reads/writes for trigger trees that never persist finished bits across calls (such as Never, ReshuffleTrigger, and Repeatedly wrapping once-style triggers like AfterPane, AfterDelayFromFirstElement, AfterWatermark.FromEndOfWindow, AfterFirst, and AfterAll). It also properly preserves the state when useNewWindowOptimization is enabled or when stateful composite triggers (AfterEach, OrFinally, AfterWatermarkEarlyAndLate) are present.

🔍 General Feedback

  • The structural analysis of trigger state machines in canElideFinishedSet, canElideRepeatedSubtree, and isOnceStyleTrigger is sound: all included subtriggers under Repeatedly only set finished bits during onFire and unconditionally mark the direct child of Repeatedly as finished upon firing, which RepeatedlyStateMachine.onFire immediately clears via resetTree() before persistFinishedSet is called.
  • Unit test coverage in TriggerStateMachineRunnerTest thoroughly exercises both the elided trigger configurations and the cases that still require persisted finished bits.

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

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping added as fallback since no labels match configuration
Backup reviewer: kennknowles (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).

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.

In TriggerRunner, avoid reading the set of finished subtriggers when no subtrigger ever finishes.

1 participant