Skip to content

Commit

Permalink
jdk11 create compact Strings when possible from split(), substring()
Browse files Browse the repository at this point in the history
Issue eclipse-openj9#19543

Signed-off-by: Peter Shipton <Peter_Shipton@ca.ibm.com>
  • Loading branch information
pshipton committed May 27, 2024
1 parent e61548b commit a4b5b11
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
16 changes: 16 additions & 0 deletions jcl/src/java.base/share/classes/com/ibm/jit/JITHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,22 @@ public char getCharFromArrayByIndex(Object obj, int index) {
}
}

public boolean canEncodeAsLatin1(byte[] array, int start, int length) {
int count = 0;
int index = start << 1;
if (!IS_BIG_ENDIAN) {
index += 1;
}
while (count < length) {
if (array[index] != 0) {
return false;
}
index += 2;
count += 1;
}
return true;
}

/**
* Returns the first index of the target character array within the source character array starting from the specified
* offset.
Expand Down
62 changes: 42 additions & 20 deletions jcl/src/java.base/share/classes/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,19 +824,25 @@ public String(char[] data, int start, int length) {
} else {
char theChar = helpers.getCharFromArrayByIndex(data, start);

if (theChar <= 255) {
value = decompressedAsciiTable[theChar];
if (COMPACT_STRINGS && (theChar <= 255)) {
value = compressedAsciiTable[theChar];
coder = LATIN1;
hash = theChar;
} else {
value = new byte[2];

helpers.putCharInArrayByIndex(value, 0, theChar);
}
if (theChar <= 255) {
value = decompressedAsciiTable[theChar];
} else {
value = new byte[2];

helpers.putCharInArrayByIndex(value, 0, theChar);
}

coder = UTF16;
hash = theChar;
coder = UTF16;
hash = theChar;

if (COMPACT_STRINGS) {
initCompressionFlag();
if (COMPACT_STRINGS) {
initCompressionFlag();
}
}
}
} else {
Expand All @@ -850,6 +856,11 @@ public String(char[] data, int start, int length) {
}

coder = LATIN1;
} else if (COMPACT_STRINGS && ((start != 0) || (data.length != length * 2)) && helpers.canEncodeAsLatin1(data, start, length)) {
value = new byte[length];
coder = LATIN1;

compress(data, start, value, 0, length);
} else {
if (start == 0 && data.length == length * 2) {
value = data;
Expand Down Expand Up @@ -887,19 +898,25 @@ public String(char[] data, int start, int length) {
} else {
char theChar = helpers.getCharFromArrayByIndex(data, start);

if (theChar <= 255) {
value = decompressedAsciiTable[theChar];
if (COMPACT_STRINGS && (theChar <= 255)) {
value = compressedAsciiTable[theChar];
coder = LATIN1;
hash = theChar;
} else {
value = new byte[2];

helpers.putCharInArrayByIndex(value, 0, theChar);
}
if (theChar <= 255) {
value = decompressedAsciiTable[theChar];
} else {
value = new byte[2];

helpers.putCharInArrayByIndex(value, 0, theChar);
}

coder = UTF16;
hash = theChar;
coder = UTF16;
hash = theChar;

if (COMPACT_STRINGS) {
initCompressionFlag();
if (COMPACT_STRINGS) {
initCompressionFlag();
}
}
}
} else {
Expand All @@ -913,6 +930,11 @@ public String(char[] data, int start, int length) {
}

coder = LATIN1;
} else if (COMPACT_STRINGS && ((start != 0) || (data.length != length * 2)) && helpers.canEncodeAsLatin1(data, start, length)) {
value = new byte[length];
coder = LATIN1;

compress(data, start, value, 0, length);
} else {
if (sharingIsAllowed && start == 0 && data.length == length * 2) {
value = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,12 @@ public void test_indexOf3() {
String needle = "\u00b0\u00b1";
String hay = new StringBuilder("a").append(needle).toString();
AssertJUnit.assertEquals("Failed to find string 3", 1, hay.indexOf(needle));

String multi = "\u0100:abc";
String[] splits = multi.split(":");
String sub = multi.substring(2);
AssertJUnit.assertEquals("Failed to find string 4", 3, "123abc".indexOf(splits[1]));
AssertJUnit.assertEquals("Failed to find string 5", 3, "123abc".indexOf(sub));
}

/**
Expand Down

0 comments on commit a4b5b11

Please sign in to comment.