I got question on atomicinteger-java-7-vs-java-8 blog about implementation of unsafe.getAndAddInt function
Compiler makes decision to use native/special function if is available other wise if will fallback to default implementation.
Unsafe.getAndAddInt is special function that gets advantage of optimization, but normal code is
still required for case when optimization is not requested or not available
To verify this we have to look at Assembly code generated.
how-to-print-dissasembly-from-jit-code article has all the details you need for this.
You can also download dll/lib for windows/linux from github
For below code snippet, optimization kicks in
Assembly code for above code looks like below ( look at line number 7, it is xadd)
Java maintains list of all intrinsic functions in code @ vmSymbols.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final int getAndAddInt(Object var1, long var2, int var4) { | |
int var5; | |
do { | |
var5 = this.getIntVolatile(var1, var2); | |
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); | |
return var5; | |
} |
It looks like JDK8 is still using CAS version.
If you just look at the code in IDE then you are correct , but JVM is intelligent hotspot compiler.
If you just look at the code in IDE then you are correct , but JVM is intelligent hotspot compiler.
Code goes through various optimization before it is executed, optimization list is huge and it will required blog series to just touch on it.
As per WikiPedia
In compiler theory, an intrinsic function is a function available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it substitutes a sequence of automatically generated instructions for the original function call, similar to an inline function. Unlike an inline function though, the compiler has an intimate knowledge of the intrinsic function and can therefore better integrate it and optimize it for the situation. This is also called builtin function in many languages.
Compilers that implement intrinsic functions generally enable them only when the user has requested optimization, falling back to a default implementation provided by the language runtime environment otherwise.
So in other words, some function are just like native function, it is different from native function written by us.Compiler makes decision to use native/special function if is available other wise if will fallback to default implementation.
Unsafe.getAndAddInt is special function that gets advantage of optimization, but normal code is
still required for case when optimization is not requested or not available
To verify this we have to look at Assembly code generated.
how-to-print-dissasembly-from-jit-code article has all the details you need for this.
You can also download dll/lib for windows/linux from github
For below code snippet, optimization kicks in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AtomicInteger i = new AtomicInteger(); | |
int total = 0; | |
for(int x=0;x<300;x++) { | |
total = i.incrementAndGet(); | |
} | |
System.out.println("Total Value - " + total); |
Assembly code for above code looks like below ( look at line number 7, it is xadd)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0x00000000031fa22f: and esi,1ff8h | |
0x00000000031fa235: cmp esi,0h | |
0x00000000031fa238: je 31fa256h ;*getstatic unsafe | |
; - java.util.concurrent.atomic.AtomicInteger::incrementAndGet@0 (line 186) | |
0x00000000031fa23e: mov eax,1h | |
0x00000000031fa243: lock xadd dword ptr [rdx+0ch],eax | |
0x00000000031fa248: inc eax | |
0x00000000031fa24a: add rsp,40h |
Java maintains list of all intrinsic functions in code @ vmSymbols.hpp
Hotspot compiler is full of magic:-)