[Java] Cache DoFn invoker constructors per instance#39310
Conversation
Co-Authored-By: Codex <noreply@openai.com>
Co-Authored-By: Codex <noreply@openai.com>
Summary of ChangesHello, 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
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 AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
|
The only failing check is Java IOs Direct. Its Java 11 step fails because javac 11 rejects 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. |
|
assign set of reviewers |
|
Assigning reviewers: R: @kennknowles for label java. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
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: |
| ## 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)). |
There was a problem hiding this comment.
Probably mention this is for performance
Summary
ByteBuddyDoFnInvokerFactorycurrently resolves aDoFn'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 sameDoFninstance and the constructor cache hits.This change adds a weak, identity-keyed cache from each live
DoFninstance 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
DoFnkey, 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
DoFninstances with different descriptors.Fixes #39309.
Validation
./gradlew :sdks:java:core:test --tests org.apache.beam.sdk.transforms.reflect.DoFnInvokersTest./gradlew :sdks:java:core:checkBoth 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:
Fixes #39309).CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.