The second time is faster
Watch / warmstart
Filmed on the Apple IIe 80-column display. The same session on the Videx UltraTerm: same keys, same text.
The Videx UltraTerm was an 80-column video card for the Apple IIe, with its own video memory and character generator, real hardware scrolling and more screen modes than the IIe's own 80 columns. This demo runs on either display; the UltraTerm film shows the original Videx UltraTerm, emulated.
Every process lives in a bank of its own. When one exits, the
kernel does not free its bank: it keeps it, tagged with the file whose code is in
it. Launch that file again and the same bank is handed back, code intact. The
film runs vi twice, and a copy of the assembler nas
three times, and times each launch.
What it types
Everything it types, in order. A line starting with $ is a command at the shell, RET included; keys lines are single keystrokes to a program (RET, ESC and the control keys are named).
$ cd /tmp $ vi scratch $ vi scratch $ cp /bin/nas /tmp/asm $ /tmp/asm $ /tmp/asm $ cp /bin/nas /tmp/asm $ /tmp/asm
What the captions say
On the IIe's own 80 columns:
- Run a program once, and the second launch is faster.
- When a process exits, the kernel keeps its bank.
- The bank is tagged with the file whose code is in it.
- Launch that file again and the same bank comes back, code intact.
- V7 had a sticky bit to keep a program's text in swap.
- Here the kernel keeps the bank without being asked.
- A stock IIe has no cache. Here the bank is the cache.
- Each launch is timed from RETURN to the first output.
- vi, first. Nothing has run it since boot.
- vi, the first time: 2.14 million cycles.
- Again. The kernel finds vi's bank waiting.
- vi again, warm: 0.61 million cycles.
- vi still starts up and draws its screen. The load is what went.
- Now nas, the assembler, copied to a new name.
- A kept bank is tagged with a path. A new path is a new file.
- asm, a new file, the first time: 1.97 million cycles.
- asm again, warm: 0.27 million cycles.
- Same program, same file: only the load is gone.
- The kernel still checks the file's date before it reuses a bank.
- Write to the file, and the kept bank is dropped.
- asm after the file was rewritten: 2.01 million cycles.
- Phil Karlton: two hard things, cache invalidation and naming things.
- A program that needs a bank can take a kept one back.
On the UltraTerm:
- Run a program once, and the second launch is faster.
- When a process exits, the kernel keeps its bank.
- The bank is tagged with the file whose code is in it.
- Launch that file again and the same bank comes back, code intact.
- V7 had a sticky bit to keep a program's text in swap.
- Here the kernel keeps the bank without being asked.
- A stock IIe has no cache. Here the bank is the cache.
- Each launch is timed from RETURN to the first output.
- vi, first. Nothing has run it since boot.
- vi, the first time: 2.18 million cycles.
- Again. The kernel finds vi's bank waiting.
- vi again, warm: 0.65 million cycles.
- vi still starts up and draws its screen. The load is what went.
- Now nas, the assembler, copied to a new name.
- A kept bank is tagged with a path. A new path is a new file.
- asm, a new file, the first time: 2.01 million cycles.
- asm again, warm: 0.24 million cycles.
- Same program, same file: only the load is gone.
- The kernel still checks the file's date before it reuses a bank.
- Write to the file, and the kept bank is dropped.
- asm after the file was rewritten: 2.01 million cycles.
- Phil Karlton: two hard things, cache invalidation and naming things.
- A program that needs a bank can take a kept one back.
The three states
A cold launch opens the file, reads it from the disk and copies the
image into a bank. A warm launch finds a kept bank tagged with that
file, and skips all three. What it still pays for is the kernel's own work in
starting a process, one call to ask the disk for the file's date, a page of
zeros, and the program itself, which starts again from its first instruction:
vi still draws its screen, and nas still prints its usage.
So the saving is the load, and the more a program does after it starts, the
smaller a share the load is.
A kept bank is tagged with an absolute path, so a copy of a file is a new file.
It is also dropped when the file is written, and the kernel checks the file's
date before it reuses one; the last launch in the film comes after
cp rewrote the file, and is cold again. A kept bank is only kept
until some other program needs the memory: then the oldest one is taken back.
The film times each launch, in cycles of the emulated machine, from the moment RETURN is pressed to the moment the first output appears: vi the first time, 2.14 to 2.18 million, and again, 0.61 to 0.65 million; the copy of nas the first time, 1.97 to 2.01 million, again, 0.24 to 0.27 million, and after the file was rewritten, 2.01 million. Warm, vi starts about 3 to 4 times faster and the copy of nas about 7 to 8 times faster. (Each figure is to within one video frame, 34,016 cycles, rounded up, which is a large share of a warm nas launch; the ratios are worked out from the figures the stamp holds, and they can differ between the two displays.)
The gate that proves it
This film is made by site/demos/warmstart.py. Run as assertions, the
same file is make check-demo-warmstart, and it runs with the rest of the tests, on both displays. It requires
that vi comes up on a new file and the copy of nas prints its
usage line on every launch; that the second launch of vi takes fewer
cycles than the first; that the first launch of the copy costs more than twice its
second; and that the launch after cp rewrote the file costs more than twice
the warm one. Each figure in a caption is the
figure the run measured, and the stamp of the film is held to the caption that states it. The checks themselves were tested: a variant that rewrites the file just before the warm launch fails at the order of the figures, and with nas replaced by a copy of cat it fails at "asm, cold: the output never appeared".
Transcript
The screen at every checkpoint the test checks, as the recorder saved it:
== vi is up on a new file (t=46.5 s) ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "scratch" [MOD] == vi is up on a new file again (t=58.9 s) ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "scratch" [MOD] == cp prints nothing (t=81.7 s) $ cp /bin/nas /tmp/asm $ == nas with no arguments prints its usage (t=90.7 s) $ /tmp/asm usage: nas [-o obj] src $ == the same usage line, the second time (t=97.6 s) $ /tmp/asm usage: nas [-o obj] src $ == cp prints nothing (t=123.2 s) $ cp /bin/nas /tmp/asm $ == nas prints its usage after the rewrite (t=127.4 s) $ /tmp/asm usage: nas [-o obj] src $