Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes a bytecode-interpreter faster than an ast-walking interpreter? [closed]

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?

like image 978
AME Avatar asked Feb 22 '23 01:02

AME


1 Answers

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.

like image 139
SK-logic Avatar answered Apr 27 '23 06:04

SK-logic