[Dataflow Streaming] Reuse ByteStringOutputStream buffers to reduce GC overhead - #36165
Conversation
|
R: @scwhittle |
There was a problem hiding this comment.
Summary of Changes
Hello @arunpandianp, 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 focuses on optimizing the performance of Dataflow streaming jobs by significantly reducing garbage collection overhead. It achieves this by introducing a robust strategy for reusing ByteStringOutputStream buffers across multiple operations within the WindmillSink and WindmillStateUtil components. This change is crucial for improving the efficiency and stability of high-throughput streaming applications where frequent buffer allocations can lead to performance bottlenecks.
Highlights
- ByteStringOutputStream Reuse: Implemented reuse of
ByteStringOutputStreaminstances inWindmillSinkandWindmillStateUtilto reduce object allocation and garbage collection overhead, particularly in performance-critical encoding paths. - Thread-Local Buffer Management:
WindmillStateUtilnow utilizes aThreadLocalwithSoftReferenceto manageByteStringOutputStreaminstances, enabling efficient per-thread buffer reuse for encoding state keys. - Refactored Metadata Encoding: The
encodeMetadatamethod inWindmillSinkhas been refactored to accept and utilize a reusableByteStringOutputStream, ensuring the buffer is reset after use viatoByteStringAndReset(). - Removed Guava Preconditions: The dependency on Guava's
Preconditions.checkStatewas removed fromWindmillSink. - Potential Logical Error in Stream Check: A logical inversion was introduced when replacing
checkState(stream.size() == 0, ...)withif (stream.size() == 0) { throw new IllegalStateException(...) }inWindmillSink.WindmillStreamWriter.encode. The original code asserted that the stream must be empty before encoding, throwing an error if it was not. The new code, however, throws an exception if the stream is empty, which is the expected and desired state. This appears to be a bug that could prevent correct operation.
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 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 counter productive. 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.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
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. ↩
|
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment |
527c723 to
e5290df
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a good optimization by reusing ByteStringOutputStream buffers to reduce GC overhead in WindmillSink and WindmillStateUtil. The removal of the unconditional stream.toByteString() in the checkState is also a nice performance improvement.
My review includes a few suggestions to further improve the implementation:
- In
WindmillStateUtil, the new thread-local caching logic can be made more efficient and robust by improving the reset logic and exception handling to prevent cache corruption. - In
WindmillSink, I've suggested a minor change to make exception handling more consistent and align with best practices.
Overall, these are valuable performance enhancements.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces performance optimizations by reusing ByteStringOutputStream buffers to reduce garbage collection overhead. This is achieved by caching the stream in a ThreadLocal in WindmillStateUtil and by passing a reusable stream to encodeMetadata in WindmillSink. Additionally, an unnecessary call to stream.toByteString() is removed from a precondition check. The changes are well-implemented and should improve performance. I have a couple of suggestions regarding exception handling to improve code clarity and robustness.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a performance optimization by reusing ByteStringOutputStream buffers to reduce garbage collection overhead. The changes are implemented in WindmillSink and WindmillStateUtil. In WindmillSink, a stream is now held as a field and reused for encoding metadata and values. In WindmillStateUtil, a ThreadLocal<SoftReference<...>> is used to cache and reuse a stream for encoding state keys. Additionally, an unconditional call to stream.toByteString() within a checkState has been removed to avoid unnecessary object creation. The changes are well-implemented and robust, ensuring that the reusable streams are reset correctly even in case of exceptions. I have one suggestion to simplify exception handling logic in WindmillStateUtil to reduce code duplication.
a4d902a to
a581e34
Compare
a581e34 to
0ec55b7
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #36165 +/- ##
============================================
+ Coverage 56.85% 58.92% +2.06%
- Complexity 3385 15514 +12129
============================================
Files 1220 2846 +1626
Lines 185458 277621 +92163
Branches 3520 12444 +8924
============================================
+ Hits 105447 163575 +58128
- Misses 76681 107600 +30919
- Partials 3330 6446 +3116
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| throw new RuntimeException(e); | ||
| } finally { | ||
| if (stream.size() > 0) { | ||
| stream.toByteStringAndReset(); |
There was a problem hiding this comment.
we could add a reset or clear method that is cheap if empty and perhaps remove the size check. could use in the Windmill sink exception handling also
|
Failures unrelated (sending fix soon for GetData stream one). |
#33578
Also removed an unconditional
stream.toByteString()in checkState.