Skip to content

Conversation

@copybara-service
Copy link

Change Utf8.encodedLength to just encode and check length on server (unsafeprocessor)

Maintain the current loop behavior on mobile (safeprocessor).

Encoding conceptually does a lot more work (both computationally and an allocation) than needed to simply determine how many bytes it should take to encode in Utf8 string. However, JDK has privilege to access string internals as byte[], which enables it to implement the getBytes() method including encoding faster than any other way to determine the byte length that we are able to write via read loop.

Several alternatives were benchmarked, including various alternate loops, other JDK APIs, but this version benchmarks as 10x faster on ascii strings, and 2x faster on most latin1 and higher unicode codepoint strings (with some regression cases for mysterious reasons), the second best implementations benchmarked is what we have today, and other alternatives were slower than that.

…unsafeprocessor)

Maintain the current loop behavior on mobile (safeprocessor).

Encoding conceptually does a lot more work (both computationally and an allocation) than needed to simply determine how many bytes it should take to encode in Utf8 string. However, JDK has privilege to access string internals as byte[], which enables it to implement the getBytes() method including encoding faster than any other way to determine the byte length that we are able to write via read loop.

Several alternatives were benchmarked, including various alternate loops, other JDK APIs, but this version benchmarks as 10x faster on ascii strings, and 2x faster on most latin1 and higher unicode codepoint strings (with some regression cases for mysterious reasons), the second best implementations benchmarked is what we have today, and other alternatives were slower than that.

PiperOrigin-RevId: 825109983
@github-actions
Copy link

Auto-closing Copybara pull request

@github-actions github-actions bot closed this Nov 14, 2025
@github-actions github-actions bot deleted the test_825109983 branch November 14, 2025 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants