Interpreters execute code directly without generating machine code, enabling portability and dynamic behavior but at the cost of speed. Design choices span tree-walking (simplest, slowest), bytecode (intermediate), and JIT (adaptive compilation), each balancing complexity, flexibility, and performance.
Implement a tree-walking interpreter for a simple language, add bytecode, then measure performance differences between them.
From your work with tree-walking interpreters, you know the basic execution model: parse source code into an AST, then walk the tree and evaluate each node directly. This is the simplest interpreter architecture, and it works — but it is also the slowest. Every time a loop body executes, the interpreter re-traverses the same tree nodes, performs the same pattern matching on node types, and chases the same pointers through a heap-allocated tree structure. The overhead is not in the computation itself but in the interpretation machinery surrounding it.
Bytecode interpreters eliminate this overhead by adding a compilation step between parsing and execution. Instead of walking the AST directly, the interpreter first compiles it into a flat sequence of bytecode instructions — simple numeric opcodes like LOAD_CONST, ADD, JUMP_IF_FALSE. Execution then becomes a tight loop: fetch the next bytecode, dispatch to its handler, repeat. This is dramatically faster than tree-walking because bytecode lives in a contiguous array (good cache behavior), dispatch is a simple switch or computed goto, and there are no pointer-chasing costs. Python, Ruby, and Lua all use this approach. The tradeoff is implementation complexity — you now have two phases (compile-to-bytecode and execute-bytecode) instead of one.
Just-in-time (JIT) compilation takes this further by translating hot bytecode sequences into native machine code at runtime. A JIT compiler monitors which functions or loops execute frequently, compiles those to optimized machine code, and patches the execution to call the compiled version directly. This is how the Java HotSpot VM and JavaScript engines like V8 achieve near-native performance. The tradeoff is significant engineering complexity: the JIT must generate correct machine code, handle garbage collection interactions, and manage the transition between interpreted and compiled code. JIT compilers also introduce warmup time — the program runs slowly at first while the JIT identifies and compiles hot paths.
The choice between these models depends on your goals. Tree-walking suits educational interpreters and languages where simplicity matters more than speed. Bytecode interpretation is the sweet spot for most production dynamic languages — fast enough for general use, portable across platforms, and much simpler than a JIT. JIT compilation is warranted when performance is critical and you can invest the engineering effort. Many real systems use a hybrid: start with bytecode interpretation and selectively JIT-compile only the hottest code paths, getting the best of both worlds.
No topics depend on this one yet.