Skip to content

Commit b40d373

Browse files
author
nipa
committed
Demo string compaction
1 parent f1a88c7 commit b40d373

4 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Check out [my blog](http://blog.codefx.org/tag/project-jigsaw/)
4343
* [UTF-8 property files](src/org/codefx/demo/java9/internal/resources/ResourceFileEncoding.java) ([JEP 226](http://openjdk.java.net/jeps/226))
4444
* [reserved stack areas](src/org/codefx/demo/java9/internal/stack/ReservingStackAreas.java) ([JEP 270](http://openjdk.java.net/jeps/270))
4545
* [DRGB implementations for `SecureRandom`](src/org/codefx/demo/java9/internal/security/Drbg.java) ([JEP 273](http://openjdk.java.net/jeps/273))
46+
* [string compaction](src/org/codefx/demo/java9/internal/string) ([JEP 254](http://openjdk.java.net/jeps/254))

‎compile-run.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/bin/bash
22

33
./compile.sh
4+
# --add-exports-private java.base/java.lang=ALL-UNNAMED
5+
# is needed for reflection over String internals in `Compaction`
46
# -XX:-RestrictReservedStack
57
# is needed to unlock stack frame reservation in `ReservingStackAreas`
68
$JAVA9_HOME/bin/java \
79
-cp out \
10+
--add-exports-private java.base/java.lang=ALL-UNNAMED \
811
-XX:-RestrictReservedStack \
912
$1
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.codefx.demo.java9.internal.string;
2+
3+
import java.lang.reflect.Field;
4+
5+
public class Compaction {
6+
7+
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
8+
process("Hello, World.");
9+
process("Hello, World. 😎");
10+
}
11+
12+
private static void process(String string) throws NoSuchFieldException, IllegalAccessException {
13+
printInternals(string);
14+
System.out.println(string);
15+
}
16+
17+
private static void printInternals(String hello) throws NoSuchFieldException, IllegalAccessException {
18+
String javaVersion = System.getProperty("java.version");
19+
switch (javaVersion.charAt(0)) {
20+
case '1' : printInternalsInJava8(hello); break;
21+
case '9' : printInternalsInJava9(hello); break;
22+
default: throw new IllegalStateException("Fancy Java version " + javaVersion);
23+
}
24+
}
25+
26+
private static void printInternalsInJava8(String hello) throws NoSuchFieldException, IllegalAccessException {
27+
char[] value = extractValues(hello, "value", char[].class);
28+
for (char c : value)
29+
System.out.print(c + " ");
30+
System.out.println();
31+
}
32+
33+
private static void printInternalsInJava9(String hello) throws NoSuchFieldException, IllegalAccessException {
34+
byte[] value = extractValues(hello, "value", byte[].class);
35+
for (byte b : value)
36+
System.out.print(b + " ");
37+
System.out.println();
38+
}
39+
40+
private static <T> T extractValues(String instance, String fieldName, Class<T> fieldType) throws NoSuchFieldException, IllegalAccessException {
41+
Field values = instance.getClass().getDeclaredField(fieldName);
42+
values.setAccessible(true);
43+
return fieldType.cast(values.get(instance));
44+
}
45+
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.codefx.demo.java9.internal.string;
2+
3+
import java.util.List;
4+
import java.util.stream.IntStream;
5+
6+
import static java.util.stream.Collectors.toList;
7+
8+
public class CompactionPerformance {
9+
10+
// To play around with this:
11+
// (1) compile using `compile.sh`
12+
// (2) run with either of the following lines
13+
//
14+
// WITH COMPACT STRINGS (enabled by default):
15+
// $JAVA9_HOME/bin/java -cp out org.codefx.demo.java9.internal.string.CompactionPerformance
16+
//
17+
// WITHOUT COMPACT STRINGS:
18+
// $JAVA9_HOME/bin/java -cp out -XX:-CompactStrings org.codefx.demo.java9.internal.string.CompactionPerformance
19+
//
20+
// This is just for fun! Use JMH for a serious/reliable performance examination.
21+
22+
public static void main(String[] args) throws InterruptedException {
23+
// time to connect profiler (at least JVisualVM doesn't want to :( )
24+
// Thread.sleep(5_000);
25+
26+
long launchTime = System.currentTimeMillis();
27+
List<String> strings = IntStream.rangeClosed(1, 10_000_000)
28+
.mapToObj(Integer::toString)
29+
.collect(toList());
30+
long runTime = System.currentTimeMillis() - launchTime;
31+
System.out.println("Generated " + strings.size() + " strings in " + runTime + " ms.");
32+
33+
launchTime = System.currentTimeMillis();
34+
String appended = strings.stream()
35+
.limit(100_000)
36+
.reduce("", (left, right) -> left + right);
37+
runTime = System.currentTimeMillis() - launchTime;
38+
System.out.println("Created string of length " + appended.length() + " in " + runTime + " ms.");
39+
}
40+
41+
}

0 commit comments

Comments
 (0)