The Workhorses of the JVM: Stubs and Intrinsics
This article describes what stubs are and why they matter so much inside the JVM.
Several weeks ago, I was examining a hot path in the project I currently work on. The code made a number of calls to this method:
System.arracopy(args...)
Based on that, I suspected the pattern might not be optimal, assuming that the copy required an expensive call into a native library. My assumption stemmed from the method signature:
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
Yet when I looked at the machine code produced by C2, I noticed several calls to a Final Stub.
In light of these findings, this article explains what stubs are and why they matter so much inside the JVM.
Environment
For the analysis, I used this JDK version: OpenJDK Runtime Environment Temurin-25.0.2+10 (build 25.0.2+10-LTS). The machine was an Intel(R) Xeon(R) @ 2.20GHz - 4 vCPU (2 cores), 16GB RAM, running Debian GNU/Linux 12 (bookworm).
For testing, I relied on the Adoptium mirror of the OpenJDK repository at the tag jdk-25.0.2+10.
Stubs
Stubs are fragments of assembly code that are generated and optimized for particular tasks, such as cryptographic functions, mathematical operations, and copying arrays. They are produced while the JVM starts up.
The init.cpp file contains the following function calls:
// stub routines in initial blob are referenced by later generated code
initial_stubs_init();
...
continuation_stubs_init(); // depends on continuations_init
...
compiler_stubs_init(false /* in_compiler_thread */); // compiler's intrinsics stubs
final_stubs_init(); // final StubRoutines stubs
This shows that generating stub routines is split into a few separate steps:
- Initial stubs
- Continuation stubs
- Compiler stubs
- Final stubs
Since some stubs rely on others, this order is essential for generating them correctly.
To check how long stub generation takes, we can run the following command:
scala-cli . --main-class playground.Main --java-opt -Xlog:startuptime --power
It prints the following:
[0.005s][info][startuptime] StubRoutines generation initial stubs, 0.0005537 secs
...
[0.019s][info][startuptime] StubRoutines generation continuation stubs, 0.0000309 secs
...
[0.026s][info][startuptime] StubRoutines generation final stubs, 0.0003121 secs
...
[0.041s][info][startuptime] StubRoutines generation compiler stubs, 0.0033504 secs
Generation turns out to be extremely fast. The longest phase – generating compiler stubs – took about 3 ms, and the remaining phases finished within microseconds.
The generated assembly code can also be inspected by turning on the -XX:+PrintStubCode option.
As the documentation in stubRoutines.hpp explains, even though each step (initial, final, etc.) may produce many stubs, they all end up in a single BufferBlob, which just holds multiple entry points. This is unlike the stubs (also known as blobs) generated by SharedRuntime, each of which is packed into its own BufferBlob.
A BufferBlob is a container holding machine code kept in the Code Cache.
Stubs have one more interesting trait that makes them even more efficient: some of them can be compiled using intrinsics.
Intrinsics
You can think of intrinsics as specialized assembly code written specifically for a particular CPU architecture. An intrinsic function makes use of particular Instruction Set Architecture (ISA) instructions, which differ from one processor to another.
Although the full list of available intrinsics is defined in vmIntrinsics.hpp, not every one of them is implemented for every CPU architecture. That is why the StubCodeGenerator has to be implemented separately for each target architecture.
Let’s play with them using a simple Java example:
private static long power() {
var result = 0L;
for (int i = 0; i < 1_000_000; i++) {
result += (long) Math.pow(i, 2.0);
}
return result;
}
To begin, we can list the intrinsics the program uses by adding the PrintIntrinsics option to the command:
scala-cli . --main-class playground.Main --java-opt -XX:+UnlockDiagnosticVMOptions --java-opt -XX:+PrintIntrinsics --power
Here, the output will contain a log line like this one:
@ 16 java.lang.Math::pow (6 bytes) (intrinsic)
This tells us that the JIT compiler intrinsified the pow function. There are, however, also ways of turning intrinsics off, which I look at next.
Disable InlineNatives
The first option at our disposal is turning off InlineNatives. Once this setting is disabled and the program is run like this:
scala-cli . --main-class playground.Main --java-opt -XX:+UnlockDiagnosticVMOptions --java-opt -XX:+PrintIntrinsics --java-opt -XX:-InlineNatives --power
the pow function stops being intrinsified. The logs show instead that some methods got inlined, but none got intrinsified:
@ 2 java.lang.FdLibm$Pow::compute (1533 bytes) failed to inline: hot method too big
@ 9 java.lang.Double::isNaN (12 bytes) inline (hot)
@ 16 java.lang.Double::isNaN (12 bytes) inline (hot)
@ 27 java.lang.Math::abs (12 bytes) inline (hot)
@ 1 java.lang.Double::doubleToRawLongBits (0 bytes) failed to inline: native method
@ 8 java.lang.Double::longBitsToDouble (0 bytes) failed to inline: native method
@ 33 java.lang.Math::abs (12 bytes) inline (hot)
@ 1 java.lang.Double::doubleToRawLongBits (0 bytes) failed to inline: native method
@ 8 java.lang.Double::longBitsToDouble (0 bytes) failed to inline: native method
@ 16 java.lang.Math::pow (6 bytes) inline (hot)
@ 2 java.lang.StrictMath::pow (6 bytes) inline (hot)
@ 2 java.lang.FdLibm$Pow::compute (1533 bytes) failed to inline: hot method too big
Let’s look at the generated stubs again. With InlineNatives turned off, the JVM skips creating stubs for many functions, including pow, sin, and doubleToRawLongBits. The full list of functions affected can be obtained by examining the vmIntrinsics::disabled_by_jvm_flags method.
Importantly, with this option enabled, no stubs get produced at all, so they take up no memory in the Code Cache.
ControlIntrinsic
Whereas the previous flag works “globally” – switching off many stubs in one go – the ControlIntrinsic option gives us much finer control (a DisableIntrinsic option exists as well, but the documentation in vmIntrinsics.hpp says it is due to be deprecated).
To turn off intrinsics just for the pow function, we have to supply that function’s intrinsic ID. We can look it up in the vmIntrinsics.hpp file, which, for every intrinsic, includes code along these lines:
do_intrinsic(_dpow, java_lang_Math, pow_name, double2_double_signature, F_S)
The ID we are after is the first argument, _ dpow. Knowing it, we can launch the program like this:
scala-cli . --main-class playground.Main --java-opt -XX:+UnlockDiagnosticVMOptions --java-opt -XX:+PrintIntrinsics --java-opt -XX:ControlIntrinsic=-_dpow --power
This time, the output looks different from before:
@ 2 java.lang.FdLibm$Pow::compute (1533 bytes) failed to inline: hot method too big
@ 9 java.lang.Double::isNaN (12 bytes) inline (hot)
@ 16 java.lang.Double::isNaN (12 bytes) inline (hot)
@ 27 java.lang.Math::abs (12 bytes) (intrinsic)
@ 33 java.lang.Math::abs (12 bytes) (intrinsic)
@ 16 java.lang.Math::pow (6 bytes) inline (hot)
@ 2 java.lang.StrictMath::pow (6 bytes) inline (hot)
@ 2 java.lang.FdLibm$Pow::compute (1533 bytes) failed to inline: hot method too big
We can see that several methods were inlined, yet abs stayed intrinsified. In other words, this approach lets us switch off one intrinsic while keeping the rest enabled. The generated stubs confirm it: only the pow stub is absent.
Use specific options
Yet another way of turning off particular intrinsics is a flag dedicated to a single function or a group of functions. To list every available option, we can run this command:
java -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal -version | grep -Ei 'use.*intrinsic'
Among the results, one option stands out as especially relevant: UseLibmIntrinsic.
Let’s run the program again, this time with UseLibmIntrinsic turned off:
scala-cli . --main-class playground.Main --java-opt -XX:+UnlockDiagnosticVMOptions --java-opt -XX:+PrintIntrinsics --java-opt -XX:-UseLibmIntrinsic --power
The result came as a surprise:
@ 16 java.lang.Math::pow (6 bytes) (intrinsic)
It matches the output we get with every intrinsic enabled. Yet a look at the generated stubs shows that the stubs for the pow function – and for other math functions such as sin or log – were not generated.
Not for the first time, the JVM has shown me that just as I believe I have figured out how it behaves, there is still a lot left for me to learn. I will not try to give a definitive explanation of why the PrintIntrinsics option acts like this. Instead, I will share a few educated guesses drawn from what I found.
To find out what really goes on under the hood, I wrote a few benchmarks.
Benchmarks
The benchmarks I wrote compare every approach discussed above:
@Benchmark
public void power_Intrinsics_On(Blackhole bh) {
bh.consume(Math.pow(2.0, 10.0));
}
@Benchmark
@Fork(value = 3, jvmArgsAppend = {"-XX:+UnlockDiagnosticVMOptions", "-XX:-InlineNatives"})
public void power_Intrinsics_Off_Disabled_By_Inline_Natives(Blackhole bh) {
bh.consume(Math.pow(2.0, 10.0));
}
@Benchmark
@Fork(value = 3, jvmArgsAppend = {"-XX:+UnlockDiagnosticVMOptions", "-XX:ControlIntrinsic=-_dpow"})
public void power_Intrinsics_Off_Disabled_By_Control_Intrinsic(Blackhole bh) {
bh.consume(Math.pow(2.0, 10.0));
}
@Benchmark
@Fork(value = 3, jvmArgsAppend = {"-XX:+UnlockDiagnosticVMOptions", "-XX:-UseLibmIntrinsic"})
public void power_Intrinsics_Off_Disabled_By_Libm_Intrinsics(Blackhole bh) {
bh.consume(Math.pow(2.0, 10.0));
}
Here are the results:
Benchmark Mode Cnt Score Error Units
IntrinsicsPowerBenchmark.power_Intrinsics_Off_Disabled_By_Control_Intrinsic avgt 15 91.580 ? 0.251 ns/op
IntrinsicsPowerBenchmark.power_Intrinsics_Off_Disabled_By_Inline_Natives avgt 15 398.977 ? 1.606 ns/op
IntrinsicsPowerBenchmark.power_Intrinsics_Off_Disabled_By_Libm_Intrinsics avgt 15 92.155 ? 0.377 ns/op
IntrinsicsPowerBenchmark.power_Intrinsics_On avgt 15 22.240 ? 0.089 ns/op
The numbers show plainly that the intrinsified pow function beats every alternative by a wide margin. The variant with the InlineNatives option turned off is, beyond any doubt, the slowest. Interestingly, the remaining two variants perform almost exactly the same.
Performance Analysis
To understand in more depth what exactly the JVM runs in each case, I executed the benchmarks with a profiler turned on, using this command:
scala-cli . --jmh --power -- -f 1 -prof "perfasm:events=cpu-clock;intel=true"
Intrinsics On
This case is easy to analyze, since the bulk of the execution time went into calling the stub generated for the pow function:
88.14% runtime stub StubRoutines::dpow
Performance is good because there are no superfluous function calls and the code is fully optimized.
Intrinsics Disabled by InlineNatives
This case, too, is fairly easy to analyze. The main bottlenecks are:
47.33% Unknown, level 0 java.lang.Double::doubleToRawLongBits, version 1, compile id 312
32.45% Unknown, level 0 java.lang.Double::longBitsToDouble, version 1, compile id 666
10.52% c2, level 4 java.lang.FdLibm$Pow::compute, version 2, compile id 683
Because this option switches off a wide range of intrinsics, the JVM falls back on the FdLibm implementation of pow (a Java port of C math libraries built with portability, not performance, in mind).
Most of the time, however, went into two methods of the Double class:
- doubleToRawLongBits
- longBitsToDouble
These are native methods that would ordinarily be intrinsified, so with that feature turned off, the JVM has to call into the native library over and over. This adds considerable overhead because of the “ceremony” involved in switching state between the Java world and the native world.
The math operations themselves inside libjava.so ran quite quickly:
2.31% libjava.so Java_java_lang_Double_longBitsToDouble
Intrinsics Disabled by ControlIntrinsic
Here, we turned off the intrinsic stub for the pow function alone and kept every other intrinsic in place. As in the previous case, the JVM fell back on the FdLibm implementation:
92.40% c2, level 4 java.lang.FdLibm$Pow::compute, version 2, compile id 670
This time, though, the JVM had more optimization tools available. It could, for example, still intrinsify doubleToRawLongBits and longBitsToDouble, the main bottlenecks of the previous example. As a result, these numbers show the real time needed to run the plain FdLibm pow implementation, without the JNI overhead we saw before.
Intrinsics Disabled by UseLibmIntrinsic
This is the most puzzling case. Even though the logs claimed the pow function had been intrinsified, the profiler painted a different picture:
92.01% libjvm.so SharedRuntime::dpow
Most of the execution time was spent inside the native library.
My investigation brought me to the inline_math_pow function. After the initial checks, its final return statement looks like this:
return StubRoutines::dpow() != nullptr ?
runtime_math(OptoRuntime::Math_DD_D_Type(), StubRoutines::dpow(), "dpow") :
runtime_math(OptoRuntime::Math_DD_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::dpow), "POW");
When a stub routine has been compiled, the JVM picks that stub; I assume this is what happened in the fully optimized path. When no stub exists, though, it falls back to SharedRuntime::dpow. This fallback relies on a lightweight internal JVM call, which sidesteps the usual overhead of regular native JNI calls.
I also tracked down the matching macro and method implementation:
JRT_LEAF(jdouble, SharedRuntime::dpow(jdouble x, jdouble y))
return __ieee754_pow(x, y);
JRT_END
This backs up the first part of my hypothesis: the function was compiled into libjvm.so and called as a fallback because the stub was not available. It seems the log marks the pow function as “intrinsified” before the JVM has decided which concrete implementation the C2 compiler will eventually pick.
This is still a hypothesis that needs more research, but I will leave a deeper investigation for later so as not to tumble down yet another “rabbit hole”.
Conclusion
With this article, I wanted to explain how stubs and intrinsics work. They are not new features; on the contrary, they are stable, mature mechanisms that have been a core part of the JVM for many years.
Many developers may not be very familiar with them – mostly because they work so smoothly in the background – but knowing about them gives a clearer view of how the JVM reaches such high performance on critical code paths. I hope you found this article helpful.