Use standard way to create numbered list 路 oracle/graal@89bc892 (original) (raw)
`@@ -19,70 +19,70 @@ A simple example based on the [Streams API](https://docs.oracle.com/javase/8/doc
`
19
19
`This example counts the number of uppercase characters in a body of text.
`
20
20
`To simulate a large load, the same sentence is processed 10 million times:
`
21
21
``
22
``
`` -
1. Save the following code snippet to a file named CountUppercase.java:
``
23
``
-
24
``
```java
25
``
`-
public class CountUppercase {
`
26
``
`-
static final int ITERATIONS = Math.max(Integer.getInteger("iterations", 1), 1);
`
27
``
`-
public static void main(String[] args) {
`
28
``
`-
String sentence = String.join(" ", args);
`
29
``
`-
for (int iter = 0; iter < ITERATIONS; iter++) {
`
30
``
`-
if (ITERATIONS != 1) System.out.println("-- iteration " + (iter + 1) + " --");
`
31
``
`-
long total = 0, start = System.currentTimeMillis(), last = start;
`
32
``
`-
for (int i = 1; i < 10_000_000; i++) {
`
33
``
`-
total += sentence.chars().filter(Character::isUpperCase).count();
`
34
``
`-
if (i % 1_000_000 == 0) {
`
35
``
`-
long now = System.currentTimeMillis();
`
36
``
`-
System.out.printf("%d (%d ms)%n", i / 1_000_000, now - last);
`
37
``
`-
last = now;
`
``
22
`` +
- Save the following code snippet to a file named
CountUppercase.java:
``
``
23
+
``
24
```java
``
25
`+
public class CountUppercase {
`
``
26
`+
static final int ITERATIONS = Math.max(Integer.getInteger("iterations", 1), 1);
`
``
27
`+
public static void main(String[] args) {
`
``
28
`+
String sentence = String.join(" ", args);
`
``
29
`+
for (int iter = 0; iter < ITERATIONS; iter++) {
`
``
30
`+
if (ITERATIONS != 1) System.out.println("-- iteration " + (iter + 1) + " --");
`
``
31
`+
long total = 0, start = System.currentTimeMillis(), last = start;
`
``
32
`+
for (int i = 1; i < 10_000_000; i++) {
`
``
33
`+
total += sentence.chars().filter(Character::isUpperCase).count();
`
``
34
`+
if (i % 1_000_000 == 0) {
`
``
35
`+
long now = System.currentTimeMillis();
`
``
36
`+
System.out.printf("%d (%d ms)%n", i / 1_000_000, now - last);
`
``
37
`+
last = now;
`
``
38
`+
}
`
38
39
` }
`
``
40
`+
System.out.printf("total: %d (%d ms)%n", total, System.currentTimeMillis() - start);
`
39
41
` }
`
40
``
`-
System.out.printf("total: %d (%d ms)%n", total, System.currentTimeMillis() - start);
`
41
42
` }
`
42
43
` }
`
43
``
`-
}
`
44
``
```
45
``
-
46
``
`-
2. Compile it and run as follows:
`
47
``
```shell
48
``
`-
javac CountUppercase.java
`
49
``
`-
java CountUppercase This year I would like to run ALL languages in one VM.
`
50
``
`-
1 (319 ms)
`
51
``
`-
2 (275 ms)
`
52
``
`-
3 (164 ms)
`
53
``
`-
4 (113 ms)
`
54
``
`-
5 (100 ms)
`
55
``
`-
6 (124 ms)
`
56
``
`-
7 (86 ms)
`
57
``
`-
8 (76 ms)
`
58
``
`-
9 (77 ms)
`
59
``
`-
total: 69999993 (1414 ms)
`
60
``
```
61
``
-
62
``
`-
The warmup time depends on numerous factors like the source code or how many cores a machine has.
`
63
``
`` -
If the performance profile of CountUppercase on your machine does not match the above, run it for more iterations by adding -Diterations=N just after java for some N greater than 1.
``
64
``
-
65
``
`` -
3. Add the -Dgraal.PrintCompilation=true option to see statistics for the compilations:
``
66
``
```shell
67
``
`-
java -Dgraal.PrintCompilation=true CountUppercase This year I would like to run ALL languages in one VM.
`
68
``
```
69
``
-
70
``
`-
This option prints a line after each compilation that shows the method compiled, time taken, bytecodes processed (including inlined methods), size of machine code produced, and amount of memory allocated during compilation.
`
71
``
-
72
``
`` -
4. Use the -XX:-UseJVMCICompiler option to disable the GraalVM compiler and use the native top tier compiler in the VM to compare performance:
``
73
``
```shell
74
``
`-
java -XX:-UseJVMCICompiler CountUppercase This year I would like to run ALL languages in one VM.
`
75
``
`-
1 (492 ms)
`
76
``
`-
2 (441 ms)
`
77
``
`-
3 (443 ms)
`
78
``
`-
4 (470 ms)
`
79
``
`-
5 (422 ms)
`
80
``
`-
6 (382 ms)
`
81
``
`-
7 (407 ms)
`
82
``
`-
8 (425 ms)
`
83
``
`-
9 (343 ms)
`
84
``
`-
total: 69999993 (4249 ms)
`
85
``
```
``
44
```
``
45
+
``
46
`+
- Compile it and run as follows:
`
``
47
```shell
``
48
`+
javac CountUppercase.java
`
``
49
`+
java CountUppercase This year I would like to run ALL languages in one VM.
`
``
50
`+
1 (319 ms)
`
``
51
`+
2 (275 ms)
`
``
52
`+
3 (164 ms)
`
``
53
`+
4 (113 ms)
`
``
54
`+
5 (100 ms)
`
``
55
`+
6 (124 ms)
`
``
56
`+
7 (86 ms)
`
``
57
`+
8 (76 ms)
`
``
58
`+
9 (77 ms)
`
``
59
`+
total: 69999993 (1414 ms)
`
``
60
```
``
61
+
``
62
`+
The warmup time depends on numerous factors like the source code or how many cores a machine has.
`
``
63
`` +
If the performance profile of CountUppercase on your machine does not match the above, run it for more iterations by adding -Diterations=N just after java for some N greater than 1.
``
``
64
+
``
65
`` +
- Add the
-Dgraal.PrintCompilation=trueoption to see statistics for the compilations:
``
``
66
```shell
``
67
`+
java -Dgraal.PrintCompilation=true CountUppercase This year I would like to run ALL languages in one VM.
`
``
68
```
``
69
+
``
70
`+
This option prints a line after each compilation that shows the method compiled, time taken, bytecodes processed (including inlined methods), size of machine code produced, and amount of memory allocated during compilation.
`
``
71
+
``
72
`` +
- Use the
-XX:-UseJVMCICompileroption to disable the GraalVM compiler and use the native top tier compiler in the VM to compare performance:
``
``
73
```shell
``
74
`+
java -XX:-UseJVMCICompiler CountUppercase This year I would like to run ALL languages in one VM.
`
``
75
`+
1 (492 ms)
`
``
76
`+
2 (441 ms)
`
``
77
`+
3 (443 ms)
`
``
78
`+
4 (470 ms)
`
``
79
`+
5 (422 ms)
`
``
80
`+
6 (382 ms)
`
``
81
`+
7 (407 ms)
`
``
82
`+
8 (425 ms)
`
``
83
`+
9 (343 ms)
`
``
84
`+
total: 69999993 (4249 ms)
`
``
85
```
86
86
``
87
87
`The preceding example demonstrates the benefits of partial escape analysis (PEA) and advanced inlining, which combine to significantly reduce heap allocation.
`
88
88
`The results were obtained using Oracle GraalVM Enterprise Edition.
`
`@@ -96,111 +96,111 @@ You can simulate the Community Edition on the Enterprise Edition by adding the o
`
96
96
`The following example is a simplified version of the Sunflow engine core code.
`
97
97
`It performs calculations to blend various values for a point of light in a rendered scene.
`
98
98
``
99
``
`` -
1. Save the following code snippet to a file named Blender.java:
``
100
``
```java
101
``
`-
public class Blender {
`
``
99
`` +
- Save the following code snippet to a file named
Blender.java:
``
``
100
```java
``
101
`+
public class Blender {
`
102
102
``
103
``
`-
private static class Color {
`
104
``
`-
double r, g, b;
`
``
103
`+
private static class Color {
`
``
104
`+
double r, g, b;
`
105
105
``
106
``
`-
private Color(double r, double g, double b) {
`
107
``
`-
this.r = r;
`
108
``
`-
this.g = g;
`
109
``
`-
this.b = b;
`
110
``
`-
}
`
``
106
`+
private Color(double r, double g, double b) {
`
``
107
`+
this.r = r;
`
``
108
`+
this.g = g;
`
``
109
`+
this.b = b;
`
``
110
`+
}
`
111
111
``
112
``
`-
public static Color color() {
`
113
``
`-
return new Color(0, 0, 0);
`
114
``
`-
}
`
``
112
`+
public static Color color() {
`
``
113
`+
return new Color(0, 0, 0);
`
``
114
`+
}
`
115
115
``
116
``
`-
public void add(Color other) {
`
117
``
`-
r += other.r;
`
118
``
`-
g += other.g;
`
119
``
`-
b += other.b;
`
120
``
`-
}
`
``
116
`+
public void add(Color other) {
`
``
117
`+
r += other.r;
`
``
118
`+
g += other.g;
`
``
119
`+
b += other.b;
`
``
120
`+
}
`
121
121
``
122
``
`-
public void add(double nr, double ng, double nb) {
`
123
``
`-
r += nr;
`
124
``
`-
g += ng;
`
125
``
`-
b += nb;
`
126
``
`-
}
`
``
122
`+
public void add(double nr, double ng, double nb) {
`
``
123
`+
r += nr;
`
``
124
`+
g += ng;
`
``
125
`+
b += nb;
`
``
126
`+
}
`
127
127
``
128
``
`-
public void multiply(double factor) {
`
129
``
`-
r *= factor;
`
130
``
`-
g *= factor;
`
131
``
`-
b *= factor;
`
``
128
`+
public void multiply(double factor) {
`
``
129
`+
r *= factor;
`
``
130
`+
g *= factor;
`
``
131
`+
b *= factor;
`
``
132
`+
}
`
132
133
` }
`
133
``
`-
}
`
134
134
``
135
``
`-
private static final Color[][][] colors = new Color[100][100][100];
`
``
135
`+
private static final Color[][][] colors = new Color[100][100][100];
`
136
136
``
137
``
`-
public static void main(String[] args) {
`
138
``
`-
for (int j = 0; j < 10; j++) {
`
139
``
`-
long t = System.nanoTime();
`
140
``
`-
for (int i = 0; i < 100; i++) {
`
141
``
`-
initialize(new Color(j / 20, 0, 1));
`
``
137
`+
public static void main(String[] args) {
`
``
138
`+
for (int j = 0; j < 10; j++) {
`
``
139
`+
long t = System.nanoTime();
`
``
140
`+
for (int i = 0; i < 100; i++) {
`
``
141
`+
initialize(new Color(j / 20, 0, 1));
`
``
142
`+
}
`
``
143
`+
long d = System.nanoTime() - t;
`
``
144
`+
System.out.println(d / 1_000_000 + " ms");
`
142
145
` }
`
143
``
`-
long d = System.nanoTime() - t;
`
144
``
`-
System.out.println(d / 1_000_000 + " ms");
`
145
146
` }
`
146
``
`-
}
`
147
147
``
148
``
`-
private static void initialize(Color id) {
`
149
``
`-
for (int x = 0; x < colors.length; x++) {
`
150
``
`-
Color[][] plane = colors[x];
`
151
``
`-
for (int y = 0; y < plane.length; y++) {
`
152
``
`-
Color[] row = plane[y];
`
153
``
`-
for (int z = 0; z < row.length; z++) {
`
154
``
`-
Color color = new Color(x, y, z);
`
155
``
`-
color.add(id);
`
156
``
`-
if ((color.r + color.g + color.b) % 42 == 0) {
`
157
``
`-
// PEA only allocates a color object here
`
158
``
`-
row[z] = color;
`
159
``
`-
} else {
`
160
``
`-
// Here the color object is not allocated at all
`
``
148
`+
private static void initialize(Color id) {
`
``
149
`+
for (int x = 0; x < colors.length; x++) {
`
``
150
`+
Color[][] plane = colors[x];
`
``
151
`+
for (int y = 0; y < plane.length; y++) {
`
``
152
`+
Color[] row = plane[y];
`
``
153
`+
for (int z = 0; z < row.length; z++) {
`
``
154
`+
Color color = new Color(x, y, z);
`
``
155
`+
color.add(id);
`
``
156
`+
if ((color.r + color.g + color.b) % 42 == 0) {
`
``
157
`+
// PEA only allocates a color object here
`
``
158
`+
row[z] = color;
`
``
159
`+
} else {
`
``
160
`+
// Here the color object is not allocated at all
`
``
161
`+
}
`
161
162
` }
`
162
163
` }
`
163
164
` }
`
164
165
` }
`
165
166
` }
`
166
``
`-
}
`
167
``
```
168
``
-
169
``
`-
2. Compile it and run as follows:
`
170
``
```shell
171
``
`-
javac Blender.java
`
172
``
`-
java Blender
`
173
``
`-
1156 ms
`
174
``
`-
916 ms
`
175
``
`-
925 ms
`
176
``
`-
980 ms
`
177
``
`-
913 ms
`
178
``
`-
904 ms
`
179
``
`-
862 ms
`
180
``
`-
863 ms
`
181
``
`-
919 ms
`
182
``
`-
868 ms
`
183
``
```
184
``
-
185
``
`-
If you would like to check how it would behave when using GraalVM Community, use the following configuration flag:
`
186
``
```shell
187
``
`-
java -Dgraal.CompilerConfiguration=community Blender
`
188
``
```
189
``
-
190
``
`` -
3. Use the -XX:-UseJVMCICompiler option to disable the Graal compiler and run with the default HotSpot JIT compiler:
``
191
``
```shell
192
``
`-
java -XX:-UseJVMCICompiler Blender
`
193
``
`-
2546 ms
`
194
``
`-
2522 ms
`
195
``
`-
1710 ms
`
196
``
`-
1741 ms
`
197
``
`-
1724 ms
`
198
``
`-
1722 ms
`
199
``
`-
1763 ms
`
200
``
`-
1742 ms
`
201
``
`-
1714 ms
`
202
``
`-
1733 ms
`
203
``
```
``
167
```
``
168
+
``
169
`+
- Compile it and run as follows:
`
``
170
```shell
``
171
`+
javac Blender.java
`
``
172
`+
java Blender
`
``
173
`+
1156 ms
`
``
174
`+
916 ms
`
``
175
`+
925 ms
`
``
176
`+
980 ms
`
``
177
`+
913 ms
`
``
178
`+
904 ms
`
``
179
`+
862 ms
`
``
180
`+
863 ms
`
``
181
`+
919 ms
`
``
182
`+
868 ms
`
``
183
```
``
184
+
``
185
`+
If you would like to check how it would behave when using GraalVM Community, use the following configuration flag:
`
``
186
```shell
``
187
`+
java -Dgraal.CompilerConfiguration=community Blender
`
``
188
```
``
189
+
``
190
`` +
- Use the
-XX:-UseJVMCICompileroption to disable the Graal compiler and run with the default HotSpot JIT compiler:
``
``
191
```shell
``
192
`+
java -XX:-UseJVMCICompiler Blender
`
``
193
`+
2546 ms
`
``
194
`+
2522 ms
`
``
195
`+
1710 ms
`
``
196
`+
1741 ms
`
``
197
`+
1724 ms
`
``
198
`+
1722 ms
`
``
199
`+
1763 ms
`
``
200
`+
1742 ms
`
``
201
`+
1714 ms
`
``
202
`+
1733 ms
`
``
203
```
204
204
``
205
205
`` The performance improvement comes from the partial escape analysis moving the allocation of color in initialize down to the point where it is stored into colors (i.e., the point at which it escapes).
``
206
206
``