TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About Β· Labs Β· Cheat sheets Β· Learning paths

← Back to JIT compilation

πŸ—£οΈ 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.
From bytecode to machine codeStep 4 of 4
Tour scriptaddkey addItem(): add0prikey price(): pri1taxkey tax(): tax23✍️Script writerπŸ§‘β€πŸ’ΌGuide with dictionarycold code only🧳TouristsπŸ“’ Phrasebookprice written
  1. The script writer writes the tour in guide language, which any guide anywhere can read.
  2. 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.
  3. The guide notices she keeps saying the same sentence about prices, so she writes its translation into her phrasebook once.
  4. Now she reads that sentence straight from the phrasebook, fast. Everything else still comes from the dictionary.
Counting, then compiling in tiersStep 4 of 4
Tally sheet10key 10 calls: 100100key 100 calls: 1001typkey receiver types: Euro only: typ234πŸ§‘β€πŸ’ΌGuide with dictionaryswitched mid-way✏️Quick pencil writerpencilπŸ–‹οΈCareful writerink
  1. Every time the guide says the price sentence, she adds a mark to her tally sheet.
  2. Enough marks! She writes a quick pencil draft into the phrasebook and keeps noting who asks and how she answers.
  3. The sentence stays busy, so the careful writer uses all those notes to write a polished version in ink, the fastest of all.
  4. Halfway through a very long, repeated announcement, the guide switches to the phrasebook without starting over.
What the optimising compiler doesStep 4 of 4
Ink version Β· fastinlkey tax() inlined into price(): inl0scakey scalar replaced (no allocation): sca1unrkey loop unrolled by 4: unr23πŸ’Ά Price sentence🧾 Tax sentenceinlinedπŸ–‹οΈCareful writerπŸ“„ Scrap of paperonly used here
  1. The price sentence is always followed by the tax sentence, so the writer joins them into one smooth sentence.
  2. 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.
  3. Instead of naming each stop one by one, the ink version reads out four stops in one breath.
  4. The finished ink version is short and very fast to read out.
Speculate, then deoptimiseStep 4 of 4
🧳TouristπŸ“’ Ink versionhandles bothπŸ§‘β€πŸ’ΌGuide with dictionaryπŸ–‹οΈCareful writer
  1. So far, every tourist asking for the museum meant the art museum. The ink version just says "straight ahead", fast.
  2. Then a tourist asks about the science museum. The ink sentence doesn't fit.
  3. The guide crosses that sentence out, goes back to her dictionary for it and answers properly.
  4. The tally sheet now shows both museums, so the writer makes a new ink version that handles both.
Warm-up, benchmarks and the code cacheStep 4 of 4
Tour times90m90ms080m80ms15ms5ms234Phrasebook pages Β· fullprikey price(): pri0taxkey tax(): tax1logkey log(): log✦✦✦✦✦2mapkey map(): map✦✦✦✦✦3πŸ§‘β€πŸ’ΌGuidewarm⏱️Fair stopwatchpractice first
  1. The first tour of the morning is slow, because the guide looks almost everything up in her dictionary.
  2. As busy sentences go into the phrasebook, her tours get much quicker.
  3. A fair stopwatch lets her do practice tours first, then times the real ones, and repeats on fresh days.
  4. The phrasebook fills its last pages. Now nothing new can be written, and new busy sentences stay slow.
Other ways to start fastStep 4 of 4
πŸ–‹οΈAnother careful writersame jobπŸ“• Printed translationfixed in advanceπŸ—’οΈ Yesterday's notesclasses + profilesπŸ“Έ Snapshot photospecial kit🚌Today's tourfast start
  1. Another careful writer can take the place of the usual one. Same job, and the result is sometimes better, sometimes not.
  2. A full translation printed the night before makes the tour fast from the first sentence, but it can't change on the way.
  3. The guide keeps yesterday's notes and tally sheet, so this morning she starts with a head start.
  4. 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.