JVM internals for Kotlin and Android developers · check

Know How the JVM makes code fast already?

One question for each of the 8 puzzles in this module. A right answer marks that puzzle as passed, so you can jump ahead. A wrong one tells you which puzzle to start with. Nothing is sent anywhere: it's saved in this browser.

Puzzle 19: Why are the first thousand calls slow?

You run the 200 batches with the Xint option and print batch 1,000 instead of 200. What does it cost?

Not quite. The interpreter does the same work per instruction on every call; only compiled code is faster. Learn it in puzzle 19 →

Right. Xint turns the compilers off for the whole run.

Not quite. The counter only decides when to compile; with Xint it decides nothing. Learn it in puzzle 19 →

Puzzle 20: Which methods get compiled, and by whom?

You change batch's loop to 10,000 calls per batch and keep 200 batches. After which batch do you expect batch's loop to be compiled by on-stack replacement?

Right. The trigger is 60,000 loop turns: 6 batches of 10,000.

Not quite. A rarely called method with a hot loop is exactly the case on-stack replacement exists for. Learn it in puzzle 20 →

Not quite. The back-edge counter counts loop turns, not calls of batch; ten times more turns per batch get there ten times sooner. Learn it in puzzle 20 →

Puzzle 21: Why does C2 copy small methods into their callers?

use has run 100,000 times with only Loud objects; Counter and Quiet are loaded but never passed. What does C2 compile for c.next(1)?

Right. One receiver class: monomorphic.

Not quite. The instruction names the method; the profile says which body ran. Learn it in puzzle 21 →

Not quite. The type profile counts what the call site met, not what is loaded. Learn it in puzzle 21 →

Puzzle 22: Where did 240 megabytes of objects go?

scoreOf now adds every Stats to a list that main prints at the end. With the default options, what does round 2 allocate?

Not quite. Escape is a property of the code, not of how long it runs. Learn it in puzzle 22 →

Not quite. Inlining lets C2 see the object; here it sees the object stored in a list that outlives the call. Learn it in puzzle 22 →

Right. Stored where outside code can reach it means allocated.

Puzzle 23: What happens when a new class breaks C2's proof?

Phase 2 creates a Loud but never passes it to use; it keeps passing Counter objects. Does use's level 4 code survive?

Not quite. Deoptimization happens when the dependency fails, at class loading, not when the collector runs. Learn it in puzzle 23 →

Not quite. The assumption was about the loaded classes, and loading Loud breaks it. Learn it in puzzle 23 →

Right. CHA's assumption is about what is loaded.

Puzzle 24: What if the profile guessed wrong?

In Hot.kt, main creates a Loud before phase 1 but passes only Counter objects until phase 2. When phase 2 starts, what happens to use's level 4 code?

Not quite. C2 still assumed something, from the type profile: only Counter. That check fails for Loud. Learn it in puzzle 24 →

Right. The assumption moved from the loaded classes to the profile.

Not quite. use hasn't been compiled yet when Loud is loaded at the start. Learn it in puzzle 24 →

Puzzle 25: How long does score really take?

You change the naive loop to add every result to a sum and print the sum at the end. What do rounds 4 and 5 show now?

Not quite. The sum is printed, so its value is needed and the computation must run. Learn it in puzzle 25 →

Right. A used result can't be eliminated.

Not quite. Printing once at the end doesn't stop main's loop from being compiled. Learn it in puzzle 25 →

Puzzle 26: Why does every run load the same classes again?

You add a new class to your app and rebuild start.jar, but keep the old app.jsa. What happens at startup?

Not quite. HotSpot checks the jar's timestamp and drops the whole app archive when it changed. Learn it in puzzle 26 →

Not quite. Only -Xshare:on makes an unusable archive an error; by default the JVM continues. Learn it in puzzle 26 →

Right. A changed jar means a new training run.

Back to the overview →