π£οΈ JIT compilation: cheat sheet
π¨ The analogyA tour guide translating live from a dictionary, who writes the most-asked phrases into a quick phrasebook.
π The regular explanationHow the JVM turns bytecode into fast machine code while your program runs. It interprets first, profiles which methods and loops are hot, compiles them in tiers with optimisations based on what it has seen, and falls back to the interpreter when a guess turns out wrong.
- The script writer writes the tour in guide language, which any guide anywhere can read.
- The guide reads the script line by line, looks each one up in her dictionary and says it to the tourists. It works right away, but it's slow.
- The guide notices she keeps saying the same sentence about prices, so she writes its translation into her phrasebook once.
- Now she reads that sentence straight from the phrasebook, fast. Everything else still comes from the dictionary.
- Every time the guide says the price sentence, she adds a mark to her tally sheet.
- Enough marks! She writes a quick pencil draft into the phrasebook and keeps noting who asks and how she answers.
- The sentence stays busy, so the careful writer uses all those notes to write a polished version in ink, the fastest of all.
- Halfway through a very long, repeated announcement, the guide switches to the phrasebook without starting over.
- The price sentence is always followed by the tax sentence, so the writer joins them into one smooth sentence.
- The guide jots a number on a scrap of paper that nobody else ever sees, so the ink version skips the paper and she keeps it in her head.
- Instead of naming each stop one by one, the ink version reads out four stops in one breath.
- The finished ink version is short and very fast to read out.
- So far, every tourist asking for the museum meant the art museum. The ink version just says "straight ahead", fast.
- Then a tourist asks about the science museum. The ink sentence doesn't fit.
- The guide crosses that sentence out, goes back to her dictionary for it and answers properly.
- The tally sheet now shows both museums, so the writer makes a new ink version that handles both.
- The first tour of the morning is slow, because the guide looks almost everything up in her dictionary.
- As busy sentences go into the phrasebook, her tours get much quicker.
- A fair stopwatch lets her do practice tours first, then times the real ones, and repeats on fresh days.
- The phrasebook fills its last pages. Now nothing new can be written, and new busy sentences stay slow.
- Another careful writer can take the place of the usual one. Same job, and the result is sometimes better, sometimes not.
- A full translation printed the night before makes the tour fast from the first sentence, but it can't change on the way.
- The guide keeps yesterday's notes and tally sheet, so this morning she starts with a head start.
- Or take a snapshot of the guide at full speed and start the next tour from that exact moment.
π§© Key questions
- Why doesn't the guide write every sentence of the script into her phrasebook before the tour starts?
- Because writing it all down first would delay the tour, and most sentences are only said once. She only writes down the sentences her tally sheet shows are busy, so the effort goes where it pays off.
- A tourist asks about the science museum, but the ink version assumed everyone means the art museum. What does the guide do?
- She crosses that sentence out, answers properly from her dictionary, and later a new ink version covers both. Guesses make the ink version fast, and going back to the dictionary keeps every answer right.
- You time a guide on her very first tour of the morning and decide she's slow. What's wrong with that?
- She hasn't warmed up yet: her phrasebook is still empty, so she's looking everything up. A fair stopwatch lets her do practice tours first, then times the real ones on fresh days.
- The guide's phrasebook has run out of pages. What happens next?
- She can't write anything new, so new busy sentences stay slow. The phrasebook only has so many pages, so a busy tour needs a bigger one.
- Which way of starting fast can't change anything during the tour, because everything had to be known the night before?
- A full translation printed before the trip. The printed book is fast from the first sentence, but it can't change on the way.