-XX: + UseCompressedStrings
private final char[] value;
private final byte[] value;
private final byte coder;
static final byte LATIN1 = 0;
static final byte UTF16 = 1;
public int indexOf(int ch, int fromIndex) {
return isLatin1()
? StringLatin1.indexOf(value, ch, fromIndex)
: StringUTF16.indexOf(value, ch, fromIndex);
}
private boolean isLatin1() {
return COMPACT_STRINGS && coder == LATIN1;
}
+XX:-CompactStrings
public int length () {
return value.length >> coder;
}
package ru.topjava;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CompactStringTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
List<String> strings = IntStream.rangeClosed(1, 100_000)
.mapToObj(Integer::toString)
.collect(Collectors.toList());
long totalTime = System.currentTimeMillis() - startTime;
System.out.println("Generated " + strings.size() + " strings in " + totalTime + " ms.");
startTime = System.currentTimeMillis();
String appended = strings.stream()
.reduce("", (l, r) -> l + r);
totalTime = System.currentTimeMillis() - startTime;
System.out.println("Created string of length " + appended.length() + " in " + totalTime + " ms.");
}
}
-XX: -CompactStrings