How does GraalVM make the optimization in Reflexion possible
My previous article explained how Reflexion implements this optimization. In this article, I’ll explain how GraalVM makes that optimization possible.
To verify that GraalVM actually performs these optimizations, we can inspect the generated machine code. Luckily, there is a way to read the assembly generated by Graal Compiler.
The key question is whether Graal Compiler is able to inline the reflective call to the target method. If it succeeds, the generated machine code will contain very little abstraction.
Using a compiler directives file:
1
2
3
4
5
6
7
[
{
match: "*::*Call*",
PrintAssembly: true,
PrintInlining: true
}
]
And run the benchmark with the following VM options: -XX:+UnlockDiagnosticVMOptions -XX:PrintAssemblyOptions=intel -XX:CompilerDirectivesFile=directives.json -XX:+LogVMOutput -XX:-DisplayVMOutput -XX:LogFile=asm.log, we can find the generated assembly code.
For Reflexion custom call, the remaining work is simply checking if the cached object is the expected lambda class after optimization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Load the benchmark class.
0x00000155d2a342bf: movabs rax,0x62c30d478 ; {oop(a 'java/lang/Class'{0x000000062c30d478} = 'io/github/lumine1909/Benchmark')}
// Read the compressed oop of field.
0x00000155d2a342c9: mov eax,DWORD PTR [rax+0xac] ; ImmutableOopMap {}
;*getstatic custom$call {reexecute=1 rethrow=0 return_oop=0}
; - (reexecute) io.github.lumine1909.Benchmark::customCall@0 (line 170)
// Compare the object's Klass pointer, check if the object is the expected lambda class.
0x00000155d2a342cf: mov eax,DWORD PTR [r12+rax*8+0x20]; implicit exception: dispatches to 0x00000155d2a34406
; ImmutableOopMap {}
;*getstatic custom$call {reexecute=1 rethrow=0 return_oop=0}
; - (reexecute) io.github.lumine1909.Benchmark::customCall@0 (line 170)
0x00000155d2a342d4: cmp DWORD PTR [r12+rax*8+0x8],0x1084450
; implicit exception: dispatches to 0x00000155d2a34429
; {metadata('io/github/lumine1909/reflexion/internal/MethodUtil$$Lambda+0x0000000029084450')}
0x00000155d2a342dd: jne 0x00000155d2a343a4
// Load the instance.
0x00000155d2a342e3: movabs rax,0x62be2c908 ; {oop(a 'io/github/lumine1909/Benchmark$A'{0x000000062be2c908})}
// GC related things, normally do jump.
0x00000155d2a342ed: cmp BYTE PTR [r15+0x48],0x0
0x00000155d2a342f2: je 0x00000155d2a34326
0x00000155d2a342f8: mov r11d,DWORD PTR [rax+0x10]
0x00000155d2a342fc: shl r11,0x3
0x00000155d2a34300: test r11,r11
0x00000155d2a34303: je 0x00000155d2a34326
0x00000155d2a34309: mov r10,QWORD PTR [r15+0x38]
0x00000155d2a3430d: cmp r10,0x0
0x00000155d2a34311: je 0x00000155d2a343d7
0x00000155d2a34317: sub r10,0x8
0x00000155d2a3431b: mov QWORD PTR [r15+0x38],r10
0x00000155d2a3431f: add r10,QWORD PTR [r15+0x40]
0x00000155d2a34323: mov QWORD PTR [r10],r11
// What the target method actually does (set a String field to "42").
0x00000155d2a34326: mov DWORD PTR [rax+0x10],0xc57c5924; {oop("42"{0x000000062be2c920})}
In contrast, mhCall (MethodHandle invokeExact) is not fully inlined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Load the benchmark class.
0x00000292b16e20bf: movabs rdx,0x62c30aa38 ; {oop(a 'java/lang/Class'{0x000000062c30aa38} = 'io/github/lumine1909/Benchmark')}
// Read the compressed oop of field.
0x00000292b16e20c9: mov edx,DWORD PTR [rdx+0x88] ; ImmutableOopMap {rdx=NarrowOop }
;*getstatic mh$call {reexecute=1 rethrow=0 return_oop=0}
; - (reexecute) io.github.lumine1909.Benchmark::mhCall@0 (line 140)
// Compare the object's Klass pointer, check if the object is the expected method handle.
0x00000292b16e20cf: cmp DWORD PTR [r12+rdx*8+0x8],0x206808; implicit exception: dispatches to 0x00000292b16e21f5
; {metadata('java/lang/invoke/DirectMethodHandle')}
0x00000292b16e20d8: jne 0x00000292b16e21af ; ImmutableOopMap {rdx=NarrowOop }
;*getstatic mh$call {reexecute=1 rethrow=0 return_oop=0}
; - (reexecute) io.github.lumine1909.Benchmark::mhCall@0 (line 140)
// Check if the method type is expected.
0x00000292b16e20de: mov r8d,DWORD PTR [r12+rdx*8+0x14]; implicit exception: dispatches to 0x00000292b16e2218
; ImmutableOopMap {rdx=NarrowOop }
;*getstatic mh$call {reexecute=1 rethrow=0 return_oop=0}
; - (reexecute) io.github.lumine1909.Benchmark::mhCall@0 (line 140)
0x00000292b16e20e3: mov r8d,DWORD PTR [r12+r8*8+0x1c]; implicit exception: dispatches to 0x00000292b16e223b
0x00000292b16e20e8: cmp DWORD PTR [r12+rdx*8+0x10],0xc5713307
; {oop(a 'java/lang/invoke/MethodType'{0x000000062b899838} = (Lio/github/lumine1909/Benchmark$A;I)Ljava/lang/String;)}
0x00000292b16e20f1: jne 0x00000292b16e2188 ;*if_acmpeq {reexecute=0 rethrow=0 return_oop=0}
; - java.lang.invoke.Invokers::checkExactType@7 (line 530)
; - java.lang.invoke.LambdaForm$MH/0x000000009808cc00::invokeExact_MT@10
; - io.github.lumine1909.Benchmark::mhCall@8 (line 140)
0x00000292b16e20f7: shl rdx,0x3
// Load the instance.
0x00000292b16e20fb: movabs r10,0x62be2c7f0 ; {oop(a 'io/github/lumine1909/Benchmark$A'{0x000000062be2c7f0})}
// Check if the method handle is customized.
0x00000292b16e2105: test r8d,r8d
0x00000292b16e2108: je 0x00000292b16e2138 ;*ifnonnull {reexecute=0 rethrow=0 return_oop=0}
; - java.lang.invoke.Invokers::checkCustomized@15 (line 625)
; - java.lang.invoke.LambdaForm$MH/0x000000009808cc00::invokeExact_MT@14
; - io.github.lumine1909.Benchmark::mhCall@8 (line 140)
0x00000292b16e210e: mov r8,r10 ;*invokevirtual invokeBasic {reexecute=0 rethrow=0 return_oop=0}
; - java.lang.invoke.LambdaForm$MH/0x000000009808cc00::invokeExact_MT@20
; - io.github.lumine1909.Benchmark::mhCall@8 (line 140)
0x00000292b16e2111: mov r9d,0x2a
// MethodHandle call the target through invokeBasic, resulting an additional overhead.
0x00000292b16e2117: call 0x00000292b0f2f1e0 ; ImmutableOopMap {}
;*invokevirtual invokeBasic {reexecute=0 rethrow=0 return_oop=1}
; - java.lang.invoke.LambdaForm$MH/0x000000009808cc00::invokeExact_MT@20
; - io.github.lumine1909.Benchmark::mhCall@8 (line 140)
; {optimized virtual_call}
The generated machine code shows that Graal JIT compiler completely inlines Reflexion’s Method invocation, while the MethodHandle still goes through invokeBasic. This is the reason why Reflexion’s version is faster as the benchmark results from the previous article shows.