Java Machine Code Profiler: Instructive Library

Java machine code profiler output showing CPU instruction sample counts

A new Java machine code profiler called instructive shows which CPU instructions your program spends its time on, using only a few lines added to an ordinary Java program. Richard Startin built the library, and a test on a RoaringBitmap loop shows why it’s useful: the same code ran seven times faster after a JDK upgrade on Apple silicon.

What instructive does

Timing tools such as JMH tell you how slow code is, not why. To find the cause, you need to see the machine code the JIT compiler produced and where the processor stalls inside it.

Instructive samples the running instruction every 10 ms of CPU time. When it stops, it reports the hottest compiled methods and counts how many samples landed on each instruction. You call Sampler.start() and Sampler.stop() around a workload, then format the result with an InstructionProfileFormatter. The jar is attached with -javaagent.

With JDK 25 on the Apple M4, each population count goes through vector register v16, and the insert makes each word wait for the previous one.
Image: Daniel Lemire's blog

The test workload

The test uses RoaringBitmap, a compressed integer-set format used by Apache Spark, Hive, Druid, Pinot, Iceberg and Flink, Netflix Atlas, Google Procella and Apache Lucene. Roaring splits integers into chunks of 65,536 values. Dense chunks become bitmap containers of 1,024 64-bit words.

The profiled method is andCardinality, which counts how many elements two sets share without building a new bitmap. For two bitmap containers it runs a simple loop: AND two words, then count the set bits with Long.bitCount.

The test builds two random sets over the integers 0 to 4,194,303, so each of 64 chunks holds about half its values. One call walks 65,536 words. The program warms up with 10,000 calls so the optimizing C2 compiler kicks in, then samples 100,000 calls.

With JDK 27, two words are loaded into a 128-bit vector register, their bits counted with cnt, and the byte counts added in pairs until there is one count per word.
Image: Daniel Lemire's blog

Apple M4: a hidden dependency chain

On an Apple M4 Max with JDK 25, each call took 115 µs. That is 1.75 ns per word, roughly 8 cycles for an AND and a population count. Slow.

The profile explained why. ARM chips lack a regular instruction to count bits in a 64-bit register, at least before the recent CSSC extension. Java works around it in four steps: copy the word into a vector register, count bits per byte (cnt), add the counts (addv), and copy the result back. About 80% of samples landed on the two copies.

The copies are cheap. The trouble is that every count reuses the same register, v16, and the first copy only writes half of it. It has to wait for the previous count, so each word waits on the one before. In C with inline assembly, an instruction that writes the full register made the same sequence six times faster.

With AVX-512, one instruction counts the bits of eight words at once. Each iteration of the loop handles 64 words.
Image: Daniel Lemire's blog

JDK 27 vectorizes the loop

On JDK 27, each call took 15.6 µs, seven times faster. The compiler now loads two words at a time into vector registers, ANDs and counts them there, and never copies back and forth.

The catch: the code generated for Long.bitCount itself didn’t change. If the compiler can’t vectorize your loop, it’s just as slow as on JDK 25.

Intel Xeon: one instruction does the work

On an Intel Xeon Gold 6548N (Emerald Rapids) pinned to one core, both JDK 25 and 27 (Temurin 25.0.5 and 27.0.1.1) use AVX-512, including vpopcntq, which counts bits in eight 64-bit words at once. Each call took 10 µs, or 0.15 ns per word. That beats the M4 on either Java version.

The loop loads eight words per 512-bit register, ANDs them, counts bits, narrows the counts to 32 bits and adds them to a running total. Each iteration covers 64 words.

How it compares with JMH

JMH’s perfasm profiler can show similar data, but it needs a JMH project, the hsdis disassembler and a system profiler: perf on Linux or DTrace with root on macOS. Instructive needs three added lines, no root and no extra tools. Because it runs inside your program, you can use it on your real application and data. The profile is a Java object you can sort and filter, and built-in rules flag long dependency chains and loops that weren’t unrolled.

What to try next

If a hot Java loop underperforms on ARM, check whether your JDK vectorizes it before rewriting code. Upgrading to JDK 27 is worth a benchmark run. The disassembler doesn’t yet decode ARM vector instructions, so expect raw .word entries there.

Source: Daniel Lemire's blog

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.