RFR: 8321283: Reuse StringLatin1::equals in regionMatches
David Schlosnagle
duke at openjdk.org
Wed Nov 26 20:14:13 UTC 2025
On Sat, 2 Dec 2023 16:56:22 GMT, Francesco Nigro <duke at openjdk.org> wrote:
> This improvement has been found on https://github.com/vert-x3/vertx-web/pull/2526.
>
> It can potentially affect the existing ArraysSupport.mismatch caller code-path performance ie requires investigation.
src/java.base/share/classes/java/lang/String.java line 2184:
> 2182: byte[] tv = value;
> 2183: byte[] ov = other.value;
> 2184: if (coder == otherCoder) {
Is this change needed as `otherCoder` is not reused?
Suggestion:
byte[] tv = value;
byte[] ov = other.value;
byte coder = coder();
if (coder == other.coder()) {
src/java.base/share/classes/java/lang/String.java line 2186:
> 2184: if (coder == otherCoder) {
> 2185: if (ooffset == 0 && toffset == 0 && len == (tv.length >> coder) && ov.length == tv.length) {
> 2186: return StringLatin1.equals(tv, ov);
A few questions on this:
- Should this also handle `UTF-16`?
- Could we save a comparison by `OR`ing offsets?
- The `equals` method will do the `tv` and `ov` length comparisons themselves, could we remove that check here or is the method call overhead too large?
- Do you happen to have results for `make test TEST="micro:java.lang.StringComparisons"`?
Suggestion:
if ((ooffset | toffset) == 0 && len == (tv.length >> coder)) {
return coder == LATIN1
? StringLatin1.equals(tv, ov)
: StringUTF16.equals(tv, ov);
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/16933#discussion_r1412902721
PR Review Comment: https://git.openjdk.org/jdk/pull/16933#discussion_r1412887365
More information about the core-libs-dev
mailing list