GeezerVison is showing github.com — Fix AsciiString.cached(String) performance regression (#17074) · netty/netty@1683570
File tree
Expand file treeCollapse file tree
Open diff view settings
common/src/main/java/io/netty/util
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/AsciiString.java +10-9Lines changed: 10 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1396,17 +1396,18 @@ public static AsciiString of(CharSequence string) { |
1396 | 1396 | * where the guaranteed use of the {@link #toString()} method. |
1397 | 1397 | */ |
1398 | 1398 | public static AsciiString cached(String string) { |
1399 | | - AsciiString asciiString = new AsciiString(string); |
1400 | | - asciiString.string = string; |
1401 | | - // Check if the input contains any non-Latin-1 characters that would have been |
1402 | | - // truncated to '?' by c2b() during construction. If so, reconstruct the cached |
1403 | | - // string from the byte array to ensure consistency. |
1404 | | - for (int i = 0; i < string.length(); i++) { |
1405 | | - if (string.charAt(i) > MAX_CHAR_VALUE) { |
1406 | | - asciiString.string = asciiString.toString(0); |
1407 | | - break; |
| 1399 | + byte[] value = PlatformDependent.allocateUninitializedArray(string.length()); |
| 1400 | + boolean allLatin1 = true; |
| 1401 | + for (int i = 0; i < value.length; i++) { |
| 1402 | + char c = string.charAt(i); |
| 1403 | + value[i] = c2b(c); |
| 1404 | + if (c > MAX_CHAR_VALUE) { |
| 1405 | + allLatin1 = false; |
1408 | 1406 | } |
1409 | 1407 | } |
| 1408 | + |
| 1409 | + AsciiString asciiString = new AsciiString(value, false); |
| 1410 | + asciiString.string = allLatin1 ? string : asciiString.toString(0); |
1410 | 1411 | return asciiString; |
1411 | 1412 | } |
1412 | 1413 |
|
|