Skip to content
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

Performance: Getting rid of temporary internal StringWriter #1298

Merged
merged 3 commits into from
Jan 3, 2025

Conversation

mkarg
Copy link
Contributor

@mkarg mkarg commented Dec 22, 2024

TL;DR: This PR internally optimizes the JAX-RS API implementation code by getting rid of StringWriter for purely temporary use case.

Requesting fast-track according to our Committer Conventions, as this PR is a non-API, non-spec, non-javadoc change.

For those interested in the details:

  • StringWriter is implicitly synchronized due to its internal use of StringBuffer, while StringBuilder is non-synchronized. Using synchronized code in single-threaded contexts is not needed.
  • Providing initial content to the constructor reduces number of interally copied bytes on later append thanks to bigger upfront buffer allocation.
  • Appending null automatically appends "null". This is implied by StringBuilder, no need for custom code.
  • Appending a single char is faster than adding a single-character String.
  • StringBuilder has fluent API; no need to repeat sb again and again.
  • Since long time (JDK 8 and earlier), String + operation implies creation of StringBuilder, and is proven to be always faster than an explicit StringBuilder (see Video "String Concatenation - JEP Café #7" for the benchmark results).
  • Since JEP 280 (JDK 9), String + operator is indified, leading to higher performance in future, just by upgrading JRE.

@mkarg mkarg requested a review from a team December 23, 2024 12:38
@mkarg mkarg merged commit 97d52dc into jakartaee:main Jan 3, 2025
3 checks passed
@mkarg mkarg deleted the stringbuilder-vs-stringwriter branch January 3, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants