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

Skip buffer allocation in StreamUtils.copy(String) #31621

Merged
merged 1 commit into from
Nov 20, 2023

Conversation

7fantasy7
Copy link

Writer writer = new OutputStreamWriter(out, charset);
writer.write(in);
writer.flush();

OutputStreamWriter constuctor does unnecessary buffer allocation (8kb)

Given that it already has a string, maybe it can be avoided writing a string to the stream directly.

out.write(in.getBytes(charset));
out.flush();

It's used in plaintext endpoints through StringHttpMessageConverter

Here's approximate difference in time
| String Length | copy (ns/op) | copyOSW (ns/op) |
| ------------- | ------------ | --------------- |
| 10            | 4.314        | 311.848         |
| 100           | 9.187        | 310.556         |
| 1,000         | 35.274       | 569.202         |
| 10,000        | 545.262      | 1581.522        |
| 50,000        | 1833.014     | 6171.236        |
| 100,000       | 3341.869     | 11976.815       |

@pivotal-cla
Copy link

@7fantasy7 Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 18, 2023
@pivotal-cla
Copy link

@7fantasy7 Thank you for signing the Contributor License Agreement!

@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 20, 2023
@jhoeller jhoeller added this to the 6.1.1 milestone Nov 20, 2023
@jhoeller jhoeller self-assigned this Nov 20, 2023
@sbrannen sbrannen changed the title Skip buffer allocation in StreamUtils#copy(String) Skip buffer allocation in StreamUtils.copy(String) Nov 20, 2023
@jhoeller jhoeller merged commit 54f87f1 into spring-projects:main Nov 20, 2023
5 of 6 checks passed
@jhoeller jhoeller added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.0.x labels Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants