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

JavaNano: improved encoding performance #545

Closed
wants to merge 2 commits into from
Closed

JavaNano: improved encoding performance #545

wants to merge 2 commits into from

Conversation

ennerf
Copy link
Contributor

@ennerf ennerf commented Jun 25, 2015

Pull request for fixing #511

I've modified writeStringNoTag to work on byte[] and reverted writeRawLittleEndian32 to what it used to be. Note that I've left all the behavior the same as before, but I believe that the position() getter is a bit odd.

Reverted to backing byte[] for performance reasons, and removed
unnecessary guava helper functions
@ennerf
Copy link
Contributor Author

ennerf commented Jun 27, 2015

Is this the correct place for JavaNano pull requests? Here is some more information:

A while ago, someone from Google (@xfxyjwf?) mentioned that writing to a byte[] instead of ByteBuffer was a conscious design decision for performance reasons. I believe that in an attempt to reduce allocations for utf8 string encoding, byte[] was changed to ByteBuffer in order to interface with code copy-and-pasted from Guava (@charlesmunger). However, if you look into the Guava code, CodedOutputByteBufferNano currently does the following:

  1. user provides byte[]
  2. byte[] gets wrapped into a ByteBuffer (additional allocation and performance hit)
  3. writeStringNoTag(String) calls encode(CharSequence,ByteBuffer) and does some extra work to translate ByteBuffer exceptions
  4. encode(CharSequence,ByteBuffer) checks whether it is a direct (0% of the time) or an array backed buffer (100% of the time)
  5. encode(CharSequence,ByteBuffer) then extracts the backing byte[]
  6. encode(CharSequence,byte[],int,int) writes utf8 into the byte[]

This patch removes the unnecessary steps 2-5 and encodes strings directly into the byte array. The other changes are mostly restored code from before the switch to ByteBuffer.

@xfxyjwf xfxyjwf closed this Jun 23, 2018
taoso pushed a commit to taoso/protobuf that referenced this pull request Aug 1, 2018
Broken by addition of GoImportPath/GoPackageName.
rinarakaki pushed a commit to rinarakaki/protobuf that referenced this pull request Aug 30, 2023
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.

None yet

3 participants