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

Simplify conversion of ByteArrayOutputStream to String #24785

Closed
wants to merge 1 commit into from

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Mar 26, 2020

Currently we often convert BAOS to String as

String contents = new String(baos.toByteArray(), StandardCharsets.ISO_8859_1);

which can be made more simple and effective as

String contents = baos.toString(StandardCharsets.ISO_8859_1.name());

BAOS.toByteArray() creates a copy of underlying array which is later decoded inside of String constructor, while BAOS.toString() doesn't create intermediate copy making convertion more memory-effective.

For JDK 8 I've got following results:

Benchmark                                                        (length)  Mode     Score     Error   Units
ByteArrayOutputStreamBenchmark.newString                               10  avgt    97.420 ±   7.340   ns/op
ByteArrayOutputStreamBenchmark.newString                              100  avgt   480.689 ±  20.615   ns/op
ByteArrayOutputStreamBenchmark.newString                             1000  avgt  4717.681 ± 175.550   ns/op

ByteArrayOutputStreamBenchmark.toString                                10  avgt    85.578 ±   5.233   ns/op
ByteArrayOutputStreamBenchmark.toString                               100  avgt   533.850 ±  23.902   ns/op
ByteArrayOutputStreamBenchmark.toString                              1000  avgt  4703.280 ± 113.713   ns/op

ByteArrayOutputStreamBenchmark.newString:·gc.alloc.rate.norm           10  avgt   134.400 ±  15.271    B/op
ByteArrayOutputStreamBenchmark.newString:·gc.alloc.rate.norm          100  avgt   722.401 ±  10.494    B/op
ByteArrayOutputStreamBenchmark.newString:·gc.alloc.rate.norm         1000  avgt  6380.016 ±  46.441    B/op

ByteArrayOutputStreamBenchmark.toString:·gc.alloc.rate.norm            10  avgt   109.600 ±  14.586    B/op
ByteArrayOutputStreamBenchmark.toString:·gc.alloc.rate.norm           100  avgt   664.002 ±  16.986    B/op
ByteArrayOutputStreamBenchmark.toString:·gc.alloc.rate.norm          1000  avgt  5433.618 ±  39.002    B/op

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 26, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@stsypanov stsypanov changed the title Simplify conversion of ByteArrayOutputStream ot String Simplify conversion of ByteArrayOutputStream to String Mar 26, 2020
@sbrannen sbrannen 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 Mar 26, 2020
@sbrannen sbrannen self-assigned this Mar 26, 2020
@sbrannen sbrannen added this to the 5.2.6 milestone Mar 26, 2020
@sbrannen sbrannen closed this in 65aa2d0 Mar 26, 2020
sbrannen added a commit that referenced this pull request Mar 26, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
See gh-24785
@sbrannen
Copy link
Member

This has been merged into master in 65aa2d0 and revised in 6222efc.

Thanks

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) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants