RFR: 8302863: Speed up String::encodeASCII using countPositives

Claes Redestad redestad at openjdk.org
Mon Feb 20 11:59:22 UTC 2023


On Sat, 18 Feb 2023 23:26:08 GMT, Claes Redestad <redestad at openjdk.org> wrote:

> When encoding Strings to US-ASCII we can speed up the happy path significantly by using `StringCoding.countPositives` as a speculative check for whether there are any chars that needs to be replaced by `'?'`. Once a non-ASCII char is encountered we fall back to the slow loop and replace as needed.
> 
> An alternative could be unrolling or using a byte array VarHandle, as show-cased by Brett Okken here: https://mail.openjdk.org/pipermail/core-libs-dev/2023-February/100573.html Having to replace chars with `?` is essentially an encoding error so it might be safe to assume this case is exceptional in practice.

Baseline:

Benchmark                      (charsetName)  Mode  Cnt      Score     Error  Units
StringEncode.encodeAsciiLong        US-ASCII  avgt    5  26626,025 ± 448,307  ns/op
StringEncode.encodeAsciiShort       US-ASCII  avgt    5     33,336 ±   2,032  ns/op


Patch:

Benchmark                      (charsetName)  Mode  Cnt     Score    Error  Units
StringEncode.encodeAsciiLong        US-ASCII  avgt    5  5492,985 ± 40,066  ns/op
StringEncode.encodeAsciiShort       US-ASCII  avgt    5    28,545 ±  4,883  ns/op

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

PR: https://git.openjdk.org/jdk/pull/12640


More information about the core-libs-dev mailing list