Crafting Interpreters · Week 16

Crafting Interpreters 30: Optimization: reading guide

Read the chapterVerified by tests

Profiling and optimization: replacing modulo with bit masking in the hash table, then NaN boxing to pack every Lox value into 64 bits, with benchmarks showing the speedup.

Time About 6 hours this week

Why it matters

Measuring before optimizing is the most important performance habit. NaN boxing is used in real engines like LuaJIT and some JavaScript engines.

How to approach it

Use a real profiler (perf, Instruments or VTune) on the benchmarks. Keep NaN boxing behind the compile-time flag the book uses so you can switch back to debug.

Challenges

3 challenges at the end of the chapter.

Challenge 1 (find and fix another hotspot with a profiler) is the most valuable. Challenge 2 (small string optimization) is a nice C exercise. Challenge 3 asks you to reflect on the book; actually write it down.

Traps

  • NaN boxing assumes 64-bit pointers fit in 48 bits; this is true on mainstream platforms today.
  • Optimizing without benchmarks.

Verified by the book's chap30_optimization tests when you push your repo.

Links

Back to week 16