I do understand the technical concepts behind the two designs, but what makes a bytecode interpreter generally that much faster? Is there a good book, someone can point me to?
The most obvious reason is that AST is usually still too high level, whereas bytecode semantics can be trivial for execution. The slowest thing in an AST-walking interpreter is normally a context lookup: all the variables, arguments, etc. are referenced by their names, whereas in a bytecode they would normally be stripped off and register numbers or stack operations would be used instead.
Of course, a bytecode can be considered a special case of an AST walking - with a flat, simple "AST" and, possibly, an optimised "walker" (e.g., using a threaded code transformation). There are many possible states in between an ad hoc AST and highly specialised bytecode - e.g., for interpreting a functional language one may keep an AST structure but replace the variables names with De Bruijn indices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With