RFR: 8378698: Optimize Base64.Encoder#encodeToString

Patrick Strawderman duke at openjdk.org
Thu Feb 26 19:25:51 UTC 2026


On Wed, 25 Feb 2026 15:37:02 GMT, Patrick Strawderman <duke at openjdk.org> wrote:

> Avoid a byte array copy in encodeToString by using JavaLangAccess#uncheckedNewStringWithLatin1Bytes

I put together a JMH benchmark that tests `encodeToString` on various size inputs (100, 100, 10000), and here are the results running locally:

Baseline

Benchmark                              (inputSize)   Mode  Cnt      Score      Error   Units
Base64Encode.testBase64EncodeToString          100  thrpt   12  25830.222 ± 1515.781  ops/ms
Base64Encode.testBase64EncodeToString         1000  thrpt   12   6864.086 ±  361.713  ops/ms
Base64Encode.testBase64EncodeToString        10000  thrpt   12    413.448 ±   83.529  ops/ms


This patch:

Benchmark                              (inputSize)   Mode  Cnt      Score      Error   Units
Base64Encode.testBase64EncodeToString          100  thrpt   12  31614.860 ± 2434.919  ops/ms
Base64Encode.testBase64EncodeToString         1000  thrpt   12   9134.750 ±  315.348  ops/ms
Base64Encode.testBase64EncodeToString        10000  thrpt   12    706.133 ±  163.285  ops/ms


Looks like an improvement across the board just looking at throughput, I didn't run an allocation rate benchmark but it seems fairly clear to me it would show an improvement. I can clean up the benchmark and add it to this PR if desired.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/29920#issuecomment-3968707787


More information about the core-libs-dev mailing list