4 ms·
V8 did not have an interpreter in 2013.
by ruuda 4y ago
V8 did not have an interpreter in 2013.
- nickelpro 4y agoOf course it did, how do you think a JS engine runs code that it doesn't have any type information about without an interpreter?
- dchest 4y agoThe bytecode interpreter appeared in 2016 https://v8.dev/blog/ignition-interpreter https://v8.dev/blog/ignition-interpreter Before that, it compiled everything into machine code.
- ReadTheLicense 4y agoCould you please explain what do you mean by type information? I thought you never have type information in JavaScript... - except the base types (bool, number, string, object, array, function), but you always have this information...
- fenomas 4y agoBasically, modern JS engines are fast because they do a lot of inferring about types. They do this both statically (by inspecting the code at compile time) and dynamically (by watching the code as it executes), and they do it for objects as well as literals. For a hand-waving theoretical example, if you have code like: var a = { foo:1 } var b = { foo:2 } var c = { foo:3 } doSomething(a) doSomething(b) doSomething(c) then modern JS engines can see that the "doSomething" function always gets called with arguments of the same type signature, and if it's a hot function they might optimize the function around that information. If you want to know more, searching on "V8 hidden classes" should turn up relevant articles.
- Dylan16807 4y agoWith a bunch of dynamic dispatch. Interpreters and basic compilers are remarkably similar, and it's easy to transform between them if you do some extra busy work. Once you have your final AST, instead of outputting bytecode instructions, output the implementation of each instruction. Or vice versa. You have code that handles different types in the interpreter? Great, bake it into the compiled code. The transform from bytecode interpreter to compiler is always pretty simple, but the other way around gets more difficult the smarter the compiler is. So yes it is very possible to have a baseline javascript compiler and no interpreter.
- nickelpro 4y agoI guess I'm differing on this point. By "baking in" the dynamic dispatch the final compiled code is, in my mind, an interpreter. Which is how I always thought of the "Full Codegen" step of the old v8 architecture.
- Dylan16807 4y agoWell let's say you wrote C++ where most of your data is in a super union of float, string,*, bool, int, map*, etc, with gobs of repetitive dynamic dispatch whenever you touch one of those unions. That wouldn't mean your code is being interpreted, would it?
- ruuda 4y agoI guess the lines can be a bit blurry especially when the compiled code still relies heavily on dynamic dispatch, but V8 generated machine code for the target CPU, marked the page as executable, and then it would call that just generated code. I think most people would call that a JIT-compiler. It wasn’t a very advanced compiler in the sense that it generated very inefficient machine code. But it still ran circles around a tree-walking or bytecode interpreter. Over time the compiler became more advanced and multi-tiered, but at the cost of startup time, and mobile was taking off, where memory was more scarce than on desktop. x86 or ARM code is a lot bigger than a specialized bytecode that is optimized for space efficiency. So eventually the Ignition interpreter was added. But unlike some of the other Javascript engines that started as an interpreter and later added native code generation, V8 started out generating native code and only later added a bytecode interpreter.