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

ART translates hot code right away (JIT) and notes it in a profile; at night dex2oat translates the profiled code ahead of time (AOT); a Baseline Profile ships the notes in the APK kernel APKDEX your app ART hot code JIT fast profile dex2oat AOT: kept Baseline Profile fast first launchfresh install
  1. Some of your code runs over and over: it's "hot". Interpreting it every time is wasteful.

    Step 1 of 5
  2. So ART translates hot code into machine code right away, while the app runs: JIT, just-in-time.

    Step 2 of 5
  3. ART also notes the hot code in a notebook: the profile. Predict: what's the notebook for?

    Step 3 of 5
  4. About 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 5
  5. A 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

Your app gets faster a few days after install, without an update. Why?

Right. The profile survives; AOT translation from it is what makes the next starts faster.

Not quite. CPUs don't learn. The difference is which code is already translated.

Not quite. JIT's machine code lives in memory while the app runs and is lost when it stops. The notebook is what's kept.

Why not translate all the DEX ahead of time, right at install?

Right. The profile focuses the work on code that's actually used.

Not quite. AOT translates DEX, but only what's worth it: the hot code from the profile.

What's the difference between JIT and AOT?

Not quite. Both happen on the phone. The difference is when.

Right. Same goal, different moment.

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.