Skip to content

[Java] Cache DoFn invoker constructors per instance#39310

Open
lofifnc wants to merge 2 commits into
apache:masterfrom
lofifnc:fix/cache-dofn-type-descriptors
Open

[Java] Cache DoFn invoker constructors per instance#39310
lofifnc wants to merge 2 commits into
apache:masterfrom
lofifnc:fix/cache-dofn-type-descriptors

Conversation

@lofifnc

@lofifnc lofifnc commented Jul 13, 2026

Copy link
Copy Markdown

Summary

ByteBuddyDoFnInvokerFactory currently resolves a DoFn's input and output type descriptors before every generated-constructor cache lookup. Runners that create a new invoker at each bundle boundary therefore repeat reflective generic-type resolution even when they reuse the same DoFn instance and the constructor cache hits.

This change adds a weak, identity-keyed cache from each live DoFn instance to its selected generated constructor. A new or deserialized instance still resolves its descriptors and uses the existing (DoFn class, input type, output type) constructor cache. Later invoker creation for that instance skips descriptor resolution.

The constructor value does not reference its DoFn key, so weak keys remain collectible. Identity equality is intentional: separate instances that compare equal can still expose different generic descriptors.

The patch preserves the generic cache-collision fix from #37355. It also strengthens that regression test by using equal DoFn instances with different descriptors.

Fixes #39309.

Validation

  • ./gradlew :sdks:java:core:test --tests org.apache.beam.sdk.transforms.reflect.DoFnInvokersTest
  • ./gradlew :sdks:java:core:check

Both pass on Java 21. The full core check includes formatting, compilation, unit tests, Checkstyle, SpotBugs, Javadocs, and shaded-JAR validation.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (Fixes #39309).
  • Update CHANGES.md with noteworthy changes.
  • This is not a large contribution; an ICLA is not required for its size.

See the Contributor Guide for more tips on how to make review process smoother.

lofifnc and others added 2 commits July 13, 2026 22:11
Co-Authored-By: Codex <noreply@openai.com>
Co-Authored-By: Codex <noreply@openai.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes the ByteBuddyDoFnInvokerFactory by caching the generated constructor for each DoFn instance. Previously, runners creating new invokers at bundle boundaries would repeatedly perform expensive reflective type descriptor resolution. By implementing an identity-keyed cache, the system now reuses the resolved constructor for the same DoFn instance, significantly reducing overhead while maintaining correctness for separate instances that might share equal values but different generic types.

Highlights

  • Performance Optimization: Introduced a weak, identity-keyed cache for DoFn invoker constructors to avoid redundant reflective generic-type resolution.
  • Cache Implementation: Utilized MapMaker with weakKeys to ensure that DoFn instances are not leaked while maintaining identity-based caching.
  • Regression Testing: Added a new test case to verify that type descriptor resolution is performed only once per DoFn instance.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the java label Jul 13, 2026

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces caching for Java DoFn type descriptor resolution when creating cached invokers in ByteBuddyDoFnInvokerFactory. By utilizing a weak, identity-keyed cache (byteBuddyInvokerConstructorInstanceCache), it avoids expensive repeated resolution of input and output type descriptors for the same DoFn instance. Unit tests have been added to verify that type descriptor resolution is cached per DoFn instance and that identity is used for caching. There are no review comments, so I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@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

@lofifnc

lofifnc commented Jul 14, 2026

Copy link
Copy Markdown
Author

The only failing check is Java IOs Direct. Its Java 11 step fails because javac 11 rejects -Xlint:-this-escape. The PR's exact base commit fails in the identical step:
https://github.com/apache/beam/actions/runs/29276895305/job/86908120778

All PR-related checks passed, including Java PreCommit, Spotless, SpotBugs, and all 2,186 published tests. This is a base-branch CI regression, not introduced by this change.

@lofifnc

lofifnc commented Jul 14, 2026

Copy link
Copy Markdown
Author

assign set of reviewers

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @kennknowles for label java.

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

@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.00%. Comparing base (42daed3) to head (5e0cc1a).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #39310      +/-   ##
============================================
- Coverage     57.00%   57.00%   -0.01%     
- Complexity     3627     3630       +3     
============================================
  Files          1186     1186              
  Lines        190593   190593              
  Branches       3768     3768              
============================================
- Hits         108644   108643       -1     
- Misses        78471    78472       +1     
  Partials       3478     3478              
Flag Coverage Δ
java 72.48% <ø> (-0.01%) ⬇️

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.

@Abacn

Abacn commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Thanks, since #39309 mentioned benchmark numbers. Have you verified that this fixes the performance issue? To run a pipeline against modified Beam code, one can refer to this doc:

https://github.com/apache/beam/blob/master/contributor-docs/code-change-guide.md#run-your-pipeline-with-modified-beam-code

Comment thread CHANGES.md
## Bugfixes

* Fixed unbounded checkpoint state growth for splittable DoFns that self-checkpoint on the portable Flink runner (Java) ([#27648](https://github.com/apache/beam/issues/27648)).
* Avoided repeated Java `DoFn` type descriptor resolution when creating cached invokers ([#39309](https://github.com/apache/beam/issues/39309)).

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.

Probably mention this is for performance

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.

[Bug]: DoFn invoker cache hits repeatedly resolve generic type descriptors

2 participants