Week 16 of 125

Crafting Interpreters, chapters 29 and 30

Sign up or sign in to tick these off and give this week a due date.

  • Build Ch 29: Superclasses Verified by tests

    Inheritance in clox with copy-down inheritance (copy superclass methods into the subclass at definition time), and super calls, including an optimized super invoke instruction.

    Time About 4 hours this week

    Why it matters

    Copy-down is a neat trick that trades flexibility for speed, a classic VM design trade-off.

    How to approach it

    Short chapter; run the full clox test suite at the end.

    Challenges

    3 challenges at the end of the chapter.

    Challenge 3 (BETA-style inner, again, now in C) is satisfying if you did it in jlox. Do challenges that change Lox's behavior on a separate git branch. The cracked.guide check runs the official test suite for this chapter, and a challenge that changes semantics (new syntax, different errors) can make tests fail on main.

    Traps

    • A class inheriting from a non-class value must be a runtime error.

    Reading guide

  • Build Ch 30: Optimization Verified 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.

    Reading guide

  • Two problems from the NeetCode 150, in order. One box for the pair.