8302863: Speed up String::encodeASCII using countPositives

Reviewed-by: alanb
This commit is contained in:
Claes Redestad 2023-02-21 13:31:15 +00:00
parent 8b20aa919b
commit 92dfa1175e

View File

@ -874,7 +874,7 @@ public final class String
if (coder == LATIN1 && if (coder == LATIN1 &&
ae.isASCIICompatible() && ae.isASCIICompatible() &&
!StringCoding.hasNegatives(val, 0, val.length)) { !StringCoding.hasNegatives(val, 0, val.length)) {
return Arrays.copyOf(val, val.length); return val.clone();
} }
byte[] ba = new byte[en]; byte[] ba = new byte[en];
if (len == 0) { if (len == 0) {
@ -973,11 +973,10 @@ public final class String
private static byte[] encodeASCII(byte coder, byte[] val) { private static byte[] encodeASCII(byte coder, byte[] val) {
if (coder == LATIN1) { if (coder == LATIN1) {
byte[] dst = Arrays.copyOf(val, val.length); int positives = StringCoding.countPositives(val, 0, val.length);
for (int i = 0; i < dst.length; i++) { byte[] dst = val.clone();
if (dst[i] < 0) { if (positives < dst.length) {
dst[i] = '?'; replaceNegatives(dst, positives);
}
} }
return dst; return dst;
} }
@ -1002,13 +1001,21 @@ public final class String
return Arrays.copyOf(dst, dp); return Arrays.copyOf(dst, dp);
} }
private static void replaceNegatives(byte[] val, int fromIndex) {
for (int i = fromIndex; i < val.length; i++) {
if (val[i] < 0) {
val[i] = '?';
}
}
}
private static byte[] encode8859_1(byte coder, byte[] val) { private static byte[] encode8859_1(byte coder, byte[] val) {
return encode8859_1(coder, val, true); return encode8859_1(coder, val, true);
} }
private static byte[] encode8859_1(byte coder, byte[] val, boolean doReplace) { private static byte[] encode8859_1(byte coder, byte[] val, boolean doReplace) {
if (coder == LATIN1) { if (coder == LATIN1) {
return Arrays.copyOf(val, val.length); return val.clone();
} }
int len = val.length >> 1; int len = val.length >> 1;
byte[] dst = new byte[len]; byte[] dst = new byte[len];
@ -1262,8 +1269,7 @@ public final class String
} }
private static void throwMalformed(byte[] val) { private static void throwMalformed(byte[] val) {
int dp = 0; int dp = StringCoding.countPositives(val, 0, val.length);
while (dp < val.length && val[dp] >=0) { dp++; }
throwMalformed(dp, 1); throwMalformed(dp, 1);
} }
@ -1278,11 +1284,13 @@ public final class String
} }
private static byte[] encodeUTF8(byte coder, byte[] val, boolean doReplace) { private static byte[] encodeUTF8(byte coder, byte[] val, boolean doReplace) {
if (coder == UTF16) if (coder == UTF16) {
return encodeUTF8_UTF16(val, doReplace); return encodeUTF8_UTF16(val, doReplace);
}
if (!StringCoding.hasNegatives(val, 0, val.length)) if (!StringCoding.hasNegatives(val, 0, val.length)) {
return Arrays.copyOf(val, val.length); return val.clone();
}
int dp = 0; int dp = 0;
byte[] dst = new byte[val.length << 1]; byte[] dst = new byte[val.length << 1];
@ -1294,8 +1302,9 @@ public final class String
dst[dp++] = c; dst[dp++] = c;
} }
} }
if (dp == dst.length) if (dp == dst.length) {
return dst; return dst;
}
return Arrays.copyOf(dst, dp); return Arrays.copyOf(dst, dp);
} }