-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Improve performance of ContainerProperties and ConsumerProperties toString() methods using StringBuilder #4013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of ContainerProperties and ConsumerProperties toString() methods using StringBuilder #4013
Conversation
45a6782
to
d1e9c6f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add your name to the @author
list of the affected classes.
I wonder if your English name could be added to the commit message, like you have mentioned in the PR description:
Signed-off-by: [Choi Wang Gyu] [dhkdrb897@gmail.com](mailto:dhkdrb897@gmail.com)
I also believe in the power of the commit message.
The PR is going to be lost eventually, but commit with stay in the history.
Therefore I anticipate to move all of your PR description to the final commit.
More info about commit importance here: https://cbea.ms/git-commit/
I also think that it is safe to back-port your change to the previous version.
Thank you so far!
|
||
/** | ||
* Append an optional property only if it's not null. | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No blank lines in JavaDocs, please.
* @param name the property name | ||
* @param value the property value (nullable) | ||
*/ | ||
private void appendOptionalProperty(StringBuilder sb, String name, @Nullable Object value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need separate methods for the logic.
Just one appendProperty()
with @Nullable Object value
and that if
is enough.
d1e9c6f
to
505b828
Compare
* @param name the property name | ||
* @param value the property value | ||
*/ | ||
private void appendProperty(StringBuilder sb, String name, Object value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, actually my last review has asked to keep only one method, but have that value
as @Nullable
and perform not null check exactly in this method.
This way that toString()
would be much simpler, without all those if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea. I misunderstood it at first, but thank you — I've applied the change now.
This commit improves the performance of toString() methods in ContainerProperties and ConsumerProperties classes by replacing string concatenations with StringBuilder usage. Motivation: The ContainerProperties.toString() method is frequently used in Spring Boot applications for debugging Kafka container configuration issues. With 40+ properties being concatenated using the + operator, the original implementation created excessive temporary String objects, leading to: - Memory allocation overhead - Performance degradation in logging-heavy scenarios - Increased GC pressure Changes: ContainerProperties: - Replaced string concatenation with StringBuilder in toString() method - Added helper methods for consistent formatting: - appendProperty(): for regular non-null properties - appendEnabledProperty(): for properties with "enabled/not enabled" formatting - Improved organization with logical property grouping and descriptive comments - Enhanced maintainability through reduced code duplication ConsumerProperties: - Updated renderProperties() method to use StringBuilder instead of string concatenations - Modified renderTopics() to accept StringBuilder parameter for efficiency - Applied ContainerProperties improvements Performance Impact: - Reduced memory allocations during toString() operations - Improved performance in logging-heavy scenarios - Better scalability for applications with frequent container configuration logging Backward Compatibility: - No breaking changes - Output format unchanged - All existing functionality preserved Resolves: spring-projects#4012 Signed-off-by: Choi Wang Gyu <dhkdrb897@gmail.com>
505b828
to
797da20
Compare
You don't need to squash commits all the time. |
* @param value the property value | ||
*/ | ||
private void appendProperty(StringBuilder sb, String name, @Nullable Object value) { | ||
sb.append("\n ").append(name).append("=").append(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, but why no if(value!=null)
any more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mistake sorry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error: eckstyle] [ERROR] /home/runner/work/spring-kafka/spring-kafka/spring-kafka/src/main/java/org/springframework/kafka/listener/ContainerProperties.java:1200:9: '/*' has more than 1 empty lines before. [EmptyLineSeparator]
Error: eckstyle] [ERROR] /home/runner/work/spring-kafka/spring-kafka/spring-kafka/src/main/java/org/springframework/kafka/listener/ConsumerProperties.java:517: Line matches the illegal pattern 'Trailing whitespace'. [Regexp]
Error: eckstyle] [ERROR] /home/runner/work/spring-kafka/spring-kafka/spring-kafka/src/main/java/org/springframework/kafka/listener/ConsumerProperties.java:533: Line matches the illegal pattern 'Trailing whitespace'. [Regexp]
Error: eckstyle] [ERROR] /home/runner/work/spring-kafka/spring-kafka/spring-kafka/src/main/java/org/springframework/kafka/listener/ConsumerProperties.java:535: Line matches the illegal pattern 'Trailing whitespace'. [Regexp]
Error: eckstyle] [ERROR] /home/runner/work/spring-kafka/spring-kafka/spring-kafka/src/main/java/org/springframework/kafka/listener/ConsumerProperties.java:542: Line matches the illegal pattern 'Trailing whitespace'. [Regexp]
Please, consider to run ./greadlew check
locally before pushing to PR.
Thanks
* @param value the property value | ||
*/ | ||
private void appendProperty(StringBuilder sb, String name, @Nullable Object value) { | ||
sb.append("\n ").append(name).append("=").append(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not resolved.
Omit properties with null values from the toString() output by updating the appendProperty method. This improves readability and avoids clutter in logs by excluding unset configuration fields. Signed-off-by: Choi Wang Gyu <dhkdrb897@gmail.com>
@@ -1193,7 +1192,9 @@ public String toString() { | |||
* @param value the property value | |||
*/ | |||
private void appendProperty(StringBuilder sb, String name, @Nullable Object value) { | |||
sb.append("\n ").append(name).append("=").append(value); | |||
if(value != null){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you have not run check
locally.
This is not a code style we follow.
See if you can apply src/idea/spring-framework.xml
into your IntelliJ IDEA for our Spring-specific code style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I saw your comment after opening the PR.
I'll fix it locally and push the update soon.
Please wait a moment. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@artembilan
I'm sorry for keeping you waiting.
This is my first time contributing to open source, so I was a bit clumsy.
Thanks to your help, I was able to resolve the issue and learned a lot.
If there's anything else I should fix, please feel free to let me know.
I really appreciate your time and guidance. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem! All good.
We do have this guidance, though: https://github.com/spring-projects/spring-kafka/blob/main/CONTRIBUTING.adoc.
Please, let us know if that is good enough to make your contribution routine smooth.
I do see that we need to mention that src/idea/spring-framework.xml
code style config over there.
Plus, we don't need to update Copyright anymore 😄 .
But who knows what else causes headache for you but we can help to avoid it with some simple improvement to the process.
Thank you!
Reformatted ContainerProperties.java to comply with the project's Spring-specific IntelliJ code style configuration. This ensures consistency and passes local style checks. Signed-off-by: Choi Wang Gyu <dhkdrb897@gmail.com>
Removed trailing spaces from lines 517, 533, 535, 542, and 546 in ConsumerProperties.java. This change ensures that `./gradlew check` passes successfully, addressing the reviewer's request for code style compliance. Signed-off-by: Choi Wang Gyu <dhkdrb897@gmail.com>
Improve toString() performance with StringBuilder
This commit improves the performance of toString() methods in
ContainerProperties and ConsumerProperties classes by replacing
string concatenations with StringBuilder usage.
Motivation:
The ContainerProperties.toString() method is frequently used in
Spring Boot applications for debugging Kafka container configuration
issues. With 40+ properties being concatenated using the + operator,
the original implementation created excessive temporary String
objects, leading to:
Changes:
ContainerProperties:
method
formatting
descriptive comments
ConsumerProperties:
string concatenations
efficiency
Performance Impact:
configuration logging
Backward Compatibility:
Resolves: #4012
Signed-off-by: Choi Wang Gyu dhkdrb897@gmail.com