3 ms·
> Knowing which paths are hot in any Javascript bytecode is not enough to produce optimized code; you also need to know the types. And you only know the types a
by eru 2y ago
> Knowing which paths are hot in any Javascript bytecode is not enough to produce optimized code; you also need to know the types. And you only know the types at runtime. This is the main reason why JIT compilers compile code at runtime.
You can often make good guesses statically. Especially if your JavaScript was produced from something like a TypeScript source.
- baziotis 2y agoWell, the truth is there's a lot more to JITing than what I wrote in the article, just because JITing didn't fit the vibe of the rest of the article that well. Anyway, yes theoretically you can. Recently folks creating Codon (https://github.com/exaloop/codon https://github.com/exaloop/codon) did it for Python using a cool form of type inference. That said, I don't think any mainstream JS VMs do it. Most recent work I know in AOT compilation for JS seems to use some profile-guided optimization and not static type inference. For example, look at the architecture at 6:38 and also discussion about types here: https://youtu.be/pVhcyKf3efM?t=469 https://youtu.be/pVhcyKf3efM?t=469). Something similar seems to be true for the project described here: https://www.youtube.com/watch?v=_T3s6-C38JI https://www.youtube.com/watch?v=_T3s6-C38JI The story seems to be a bit different for hopc: https://www.youtube.com/watch?v=iY1EXHQ6IeQ https://www.youtube.com/watch?v=iY1EXHQ6IeQ The video starts by giving good examples of why it is very hard to have good type inference for JS (which agrees with my intuition) but continues to talk about using type inference. However, the benchmark in this talk and in their paper are not super convincing. All that said, JS is not my expertise so I would be interested to know more about what mainstream VMs do in practice and any latest research.
- eru 2y agoThanks! I'm friends with someone working on the 'Faster CPython' project. There's lots of interesting stuff happening there. Both in what makes it into CPython, and even more that they are experimenting with.
- baziotis 2y agoOh, that's cool! I think these are the folks that made the Specializing Adaptive Interpreter which does type specialization at runtime. If these folks have any thoughts on ahead-of-time type inference and/or Codon, please let us know! :)
- brabel 2y agoI wonder if anyone has made a compiler that goes from TypeScript sources to binaries directly, taking advantage of types to create something much faster than Node could? Also, would it be possible to keep types in JS bytecode so that an JIT can take advantage of that (similar to JVM bytecode)?
- baziotis 2y agoFor the first question, check this: https://youtu.be/pVhcyKf3efM?t=447 https://youtu.be/pVhcyKf3efM?t=447 at 7:27. I don't know any details about Porforr but you may want to look into that.
- ape4 2y agoI thought the same. For example `for (let i=0; i <1000; i++)`
- pizlonator 2y agoGreat example! Should you represent the `i` as an int or should you represent it as a double? Depends on what you do with it! Say that the body of the loop is just `foo(i)` and you don't know what `foo` is at compile time. Whether it's better for `i` to be an int or a double depends on what `foo` does: - If `foo` uses `i` as an array index, then you absolutely want `i` to be an int. - If `foo` uses `i` for float math (say, multiplies it by 1.5), then you absolutely want `i` to be a double. - If `foo` does a mix of those two things, then who knows. It depends on what happens more. So, even in your simple example, it's not obvious what types to use when compiling JavaScript.
- pizlonator 2y ago> You can often make good guesses statically. Especially if your JavaScript was produced from something like a TypeScript source. No, you really can't. TypeScript types don't give you the sort of data you want for a compiler to make optimization decisions.