Skip to content

Conversation

cwangg897
Copy link
Contributor

@cwangg897 cwangg897 commented Jul 24, 2025

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:

  • 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: #4012

Signed-off-by: Choi Wang Gyu dhkdrb897@gmail.com

Copy link
Member

@artembilan artembilan left a 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.
*
Copy link
Member

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) {
Copy link
Member

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.

@cwangg897 cwangg897 force-pushed the refactor-containerproperties-tostring branch from d1e9c6f to 505b828 Compare July 26, 2025 06:36
@cwangg897 cwangg897 changed the base branch from main to 3.3.x July 28, 2025 08:32
@cwangg897 cwangg897 changed the base branch from 3.3.x to main July 28, 2025 08:34
* @param name the property name
* @param value the property value
*/
private void appendProperty(StringBuilder sb, String name, Object value) {
Copy link
Member

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.

Copy link
Contributor Author

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>
@cwangg897 cwangg897 force-pushed the refactor-containerproperties-tostring branch from 505b828 to 797da20 Compare July 28, 2025 14:23
@artembilan
Copy link
Member

You don't need to squash commits all the time.
It is better to preserve history for review traceability.
We will squash them on merge then.
Thanks

* @param value the property value
*/
private void appendProperty(StringBuilder sb, String name, @Nullable Object value) {
sb.append("\n ").append(name).append("=").append(value);
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mistake sorry

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not resolved.

Copy link
Member

@artembilan artembilan left a 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);
Copy link
Member

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){
Copy link
Member

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.

Copy link
Contributor Author

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!

Copy link
Contributor Author

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!

Copy link
Member

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>
@artembilan artembilan enabled auto-merge (squash) July 28, 2025 15:44
@artembilan artembilan merged commit 08985a3 into spring-projects:main Jul 28, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance: Optimize toString() methods with StringBuilder
2 participants