-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Enhance toString method in SockJsFrame #35510
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
Conversation
4fb3190
to
98c588e
Compare
Signed-off-by: KNU-K <[email protected]>
98c588e
to
fb8ae80
Compare
Signed-off-by: KNU-K <[email protected]>
Signed-off-by: KNU-K <[email protected]>
Signed-off-by: KNU-K <[email protected]>
…performance Signed-off-by: KNU-K <[email protected]>
Signed-off-by: KNU-K <[email protected]>
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 for the pull request.
The title says "for better content representation", but it looks like the output doesn't change, or does it? In other words it's about performance and streamlined implementation?
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java
Outdated
Show resolved
Hide resolved
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java
Outdated
Show resolved
Hide resolved
@rstoyanchev |
Signed-off-by: KNU-K <[email protected]>
bad431a
to
c5facc7
Compare
…g 80 characters Signed-off-by: KNU-K <[email protected]>
Signed-off-by: KNU-K <[email protected]>
…alization Signed-off-by: KNU-K <[email protected]>
…tion Signed-off-by: KNU-K <[email protected]>
Signed-off-by: KNU-K <[email protected]>
@rstoyanchev |
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 think the overall changes is making readability worse.
Instead of actively trying to guess the builder size in advance with multiple operations, I would favor using a default StringBuilder
instance and use it all the way (instead of performing concatenations on the last line).
This would cause probably some more allocation, but in the end this code is not on a hot path so treating StringUtils
as an "external library" and making maintenance worse is not worth it.
sb.append("...(truncated)"); | ||
} | ||
|
||
return "SockJsFrame content='" + sb + "'"; |
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.
Why not use the String Builder for this part as well? This PR is trying to optimize for memory allocation after all.
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.
Thank you for the review.
Regarding the other toString()
methods, they also use StringBuilder
, so I thought there’s no need for the unnecessary String objects to become GC targets in the JVM. Of course, I agree that the original code is more readable.
My perspective is that the readability of this part isn’t particularly high, and concatenating immutable Strings in this way is generally considered an anti-pattern, so I opted to use a mutable StringBuilder
.
I will revise the return
part as you suggested.
…handling Signed-off-by: KNU-K <[email protected]>
See gh-35510 Signed-off-by: KNU-K <[email protected]>
In the end I've gone for the simpler version of a fixed initialCapacity. The use of StringBuilder is good as it eliminates re-creating the String potentially multiple times. Given that SockJS frames typically have a |
@rstoyanchev thank u :) |
Description:
StringUtils
) andMath.min()
.String
objects to reduce GC pressure.StringBuilder
with pre-allocated capacity for better memory efficiency.\n
and\r
replacements and truncated output after 80 characters.Key Changes:
Benefits: