News for hackers. Stuff that matters.
Re:Common Misconceptions about Compilers by eru on Tuesday December 10, 2024 @01:11AM (#42374246)
> 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.

Re:Common Misconceptions about Compilers by baziotis on Tuesday December 10, 2024 @01:47AM (#42374432)
Well, 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) 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). Something similar seems to be true for the project described here: 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 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.

Re:Common Misconceptions about Compilers by eru on Tuesday December 10, 2024 @02:43AM (#42374723)
Thanks!

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.

Re:Common Misconceptions about Compilers by baziotis on Tuesday December 10, 2024 @03:02AM (#42374829)
Oh, 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! :)
Re:Common Misconceptions about Compilers by brabel on Tuesday December 10, 2024 @02:27AM (#42374643)
I 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)?

Re:Common Misconceptions about Compilers by baziotis on Tuesday December 10, 2024 @02:42AM (#42374718)
For the first question, check this: 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.
Re:Common Misconceptions about Compilers by ape4 on Tuesday December 10, 2024 @08:18AM (#42376546)
I thought the same. For example `for (let i=0; i <1000; i++)`
Re:Common Misconceptions about Compilers by pizlonator on Tuesday December 10, 2024 @11:38AM (#42378452)
Great 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.

Re:Common Misconceptions about Compilers by pizlonator on Tuesday December 10, 2024 @11:36AM (#42378424)
> 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.