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

Avoid byte array copy in getContentAsString #31731

Conversation

kilink
Copy link
Contributor

@kilink kilink commented Nov 30, 2023

The getContentAsString method was originally added in d9b8826 to avoid the extra copying inherent to calling ByteArrayOutputStream.toByteArray; however, in f83c609 the class was updated to instead use FastByteArrayOutputStream, and in the process the extra copy was brought back when getContentAsString was changed to call toByteArray. Switch to calling toByteArrayUnsafe, a method provided by FastByteArrayOutputStream, which avoids the extra copy; since we immediately pass the byte array to the String constructor and it isn't accessed anywhere else, the usage is safe.

The getContentAsString method was originally added in d9b8826 to avoid the extra copying inherent
to calling ByteArrayOutputStream.toByteArray; however, in f83c609 the class was updated to
instead use FastByteArrayOutputStream, and in the process the extra copy was brought back when
getContentAsString was changed to call toByteArray. Switch to calling toByteArrayUnsafe, a method
provided by FastByteArrayOutputStream, which avoids the extra copy; since we immediately pass the
byte array to the String constructor and it isn't accessed anywhere else, the usage is safe.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 30, 2023
@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 1, 2023
@snicoll snicoll self-assigned this Dec 1, 2023
@snicoll snicoll added this to the 6.1.2 milestone Dec 1, 2023
snicoll pushed a commit that referenced this pull request Dec 1, 2023
The getContentAsString method was originally added in d9b8826 to avoid
the extra copying inherent to calling ByteArrayOutputStream.toByteArray;
however, in f83c609 the class was updated to instead use
FastByteArrayOutputStream, and in the process the extra copy was brought
back when getContentAsString was changed to call toByteArray.

Switch to calling toByteArrayUnsafe, a method provided by
FastByteArrayOutputStream, which avoids the extra copy; since we
immediately pass the byte array to the String constructor, and it isn't
accessed anywhere else, the usage is safe.

See gh-31731
@snicoll snicoll closed this in 0dbb0f5 Dec 1, 2023
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Dec 2, 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) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants