Android from the foundation · Puzzle 9 of 16
Faster code, now and later
Reading DEX one instruction at a time is slow. So ART translates busy code into machine code while the app runs, and the phone translates more of it later.
The problem
In puzzle 8, ART ran your DEX by interpreting it, which is slow. But some of your code runs thousands of times: scrolling a list, drawing the screen. Translating that once into machine code would pay off every time it runs again. When should that happen?
See it
Some of your code runs over and over: it's "hot". Interpreting it every time is wasteful.
Step 1 of 5So ART translates hot code into machine code right away, while the app runs: JIT, just-in-time.
Step 2 of 5ART also notes the hot code in a notebook: the profile. Predict: what's the notebook for?
Step 3 of 5About once a day, while the phone is idle and charging,
dex2oat, a program that's part of Android, translates the profiled code and keeps it in a file on the phone: AOT, ahead-of-time.Step 4 of 5A Baseline Profile is that notebook written by the app's developers and shipped in the APK. The phone translates that code at install, so it's fast from the first launch.
Step 5 of 5
The building block
ART watches which code runs often, "hot" code, and translates it to machine code right away, while the app runs: that's JIT, just-in-time. That machine code lives in the process's memory, so it's gone when the app stops. It also writes down which code was hot in a notebook, the profile.
Later, about once a day while the phone is idle and charging, dex2oat, a program that's part of Android, translates the code in the profile to machine code and keeps it in a file on the phone: that's AOT, ahead-of-time. Next start, that code is fast from the first moment. It doesn't translate everything: that would take a long time and use storage, and most code is rarely used.
A Baseline Profile is a profile the app's developers write and ship inside the APK, so dex2oat can translate the important code when the app is installed, before its first launch.
The analogy
The translator reads new recipes word by word, but recipes asked for often get translated on the spot and noted in a notebook. Once a day, at night, a professional translates everything in the notebook for good.
Check yourself
Sources
What you now know
JIT = translate busy code now, while the app runs. AOT = translate it ahead of time, later, from ART's notes. A Baseline Profile sends those notes with the APK.
Answer every question above correctly to unlock the next puzzle.