Skip to content

[Java] Cache Encoding.values() in PaneInfoCoder.Encoding.fromTag - #41655

Open
acrites wants to merge 1 commit into
apache:masterfrom
acrites:cache-pane-encoding-values
Open

acrites wants to merge 1 commit into
apache:masterfrom
acrites:cache-pane-encoding-values

Conversation

@acrites

@acrites acrites commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Avoid allocating a cloned Encoding[] array twice per decoded element in PaneInfo.PaneInfoCoder.Encoding.fromTag(byte).

Summary

  • Cache Encoding.values() once in a private static final Encoding[] VALUES field inside PaneInfo.PaneInfoCoder.Encoding.
  • Reuse the cached VALUES array and computed index in fromTag(byte).
  • Expand PaneInfoTest to cover Encoding.ONE_INDEX across non-EARLY timings and add testDecodeInvalidEncodingThrowsCoderException for invalid encoding tag bytes.

Testing

  • ./gradlew :sdks:java:core:spotlessCheck :sdks:java:core:checkstyleMain :sdks:java:core:checkstyleTest :sdks:java:core:spotbugsMain :sdks:java:core:test --tests "org.apache.beam.sdk.transforms.windowing.PaneInfoTest" --tests "org.apache.beam.sdk.util.WindowedValueTest"

Java's Enum.values() clones a new array on every invocation, and PaneInfo.PaneInfoCoder.Encoding.fromTag previously called values() twice per decoded element. Cache Encoding.values() in a private static final array and reuse the computed index to eliminate per-element Encoding[] allocations during PaneInfoCoder.decode.
@github-actions github-actions Bot added java core size/s S: 10-49 lines changed labels Nov 24, 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 PaneInfo.PaneInfoCoder.Encoding.fromTag(byte) by caching Encoding.values() in a static final array, avoiding two unnecessary array clone allocations per decoded element. It also expands PaneInfoTest to cover the ONE_INDEX encoding path for non-EARLY timings and tests invalid encoding tags.

🔍 General Feedback

  • The changes are clean, well-scoped, and strictly preserve the existing behavior while eliminating hot-path allocations during PaneInfo decoding.

💡 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: @kennknowles for label java.
Backup reviewer: ahmedabu98 (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.

1 participant