477 points · by bpierre · 2y ago · 138 comments
The really cool optimizations come from compiling TypeScript, or something close to it. You could use types to get enormous gains. Anything without typing gets the default slow JS calls. Interfaces can get reduced to vtables or maybe even straight calls, possibly on structs instead of maps. You could have an Int and Float type that degrade into Number that just sit inside registers.
The main problem is that both TS and V8 are fast-moving, non-standard targets. You could only really do such a project with a big team. Maintaining compatibility would be a job by itself.
As a simple example - TypeScript doesn’t distinguish between integers and floats; they’re all just numbers. So all array accesses need casting. A TypeScript designed to aid static compilation likely would have that distinction.
But the big elephant in the room is TypeScript’s structural subtyping. The nature of this makes it effectively impossible for the compiler to statically determine the physical structure of any non-primitive argument passed into a function. This gives you worse-than-JIT performance on all field access, since JITs can perform dynamic shape analysis.
AssemblyScript (https://www.assemblyscript.org/) is a TypeScript dialect with that distinction
Microsoft's AOT compiler for MakeCode, via C++.
[1]: https://www.microsoft.com/en-us/research/publication/static-...
This isn't because of `any` either. The type system itself deliberately has holes in it. So any language that uses TypeScript type annotations to generate faster/smaller code is opening itself to miscompiling code and segfaults, etc.
Do you have any examples off the top of your head?
Can you name a single language that is used for high-performance software and whose type system is sound? To speed up the process, note that none of the obvious candidates have sound type systems.
Suppose you have some interface with fields a and c. If your function takes in an object with that interface and operates on the c field, what you want is to be able to do is compile that function to access c at “the address pointed to by the pointer to the object, plus 8” (assuming 64-bit fields). Your CPU supports such addressing directly.
Because of structural subtyping, you can’t do that. It’s not unrecognized member. But your caller might pass in an object with fields a, b, and c. This is entirely idiomatic. Now c is at offset 16, not 8. Because the physical layout of the object is different, you no longer have a statically known offset to the known field.
I wonder how much performance gain you expect to achieve. For simple CPU-bounded tasks, C/Rust/etc is roughly three times as fast as v8 and Julia, which compiles full scripts and has good type analysis, is about twice as fast. There is not much room left. C/Rust/etc can be much faster with SIMD, multi-threading and fine control of memory layout but an AOT JS compiler might not gain much from these.
It'd be nice of TS at least allowed for specifying types like integer, allowing some of the newer TS aware runtimes could take advantage of the additional info, even if the main TS->JS compilation just treated `const val: int` the same as `const val: number`.
I wonder if a syntax like
const counter: Number<int>
would be acceptable. const Integer = {
[Symbol.customMatcher]: (value) => [Number.parseInt(value)]
}
const Integer(counter) = 42.56;
// counter === 42
[1] https://github.com/tc39/proposal-extractors const counter = UInt64(42);
The current state of the art is const counter = BigInt.asUintN(64, 42);Because JS code can arbitrarily modify a type, any language trying to specify what the outputs of a function can be also has to be Turing complete.
There are of course still plenty of types that TS doesn't bother trying to model, but it does try to cover even funny cases like field names going from kebab-case to camelCase.
Did the site change? Or am I missing something? :)
If we could bundle everything to native that would completely change the game since as good as bun's cold start is, you can't beat running straight native with a small binary.
I've spent a bit of time trying to review each, so hopefully this analysis will be useful for some readers. What are the main commonalities and differences between Static Hermes and Porffor?
* They both aim for JS test262 conformance [1]
* Porffor supports both Native and Wasm outputs while Static Hermes is mainly focused on Native outputs for now
* Porffor is self-hosted (Porffor is written in pure JS and can compile itself), while Static Hermes relies on LLVM
* Porffor currently doesn't support async/promise/await while Static Hermes does (with some limitations)
* Static Hermes is written in C++ while Porffor is mainly JS
* They both support TypeScript (although Static Hermes does it through transpiling the TS AST to Flow, while Porffor supports it natively)
* Static Hermes has a fallback interpreter (to support `eval` and other hard-to-compile JS scenarios), while Porffor only supports AOT compiling (although, as I commented in other thread here, it maybe be possible to support `eval` in Porffor as well)
In general, I'm excited to see if this project can gain some traction so we can speed-up Javascript engines one the Edge!
Context: I'm Syrus, from Wasmer [3][1] https://github.com/facebook/hermes/discussions/1137
Admittedly, it is not our focus, we are focusing mainly on React Native, where WASM doesn't make sense.
The most important feature of Static Hermes is our type checker, which guarantees runtime soundness.
Porffor is very interesting, I have been watching it for some time and I am rooting for it.
Eg.
Promise.then(() => console.log(“a”));
console.log(“b”)
must log [“b”, “a”] and not [“a”, “b”].Is there a technical reason why?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
If some change were required which introduced a regression on some Test262 tests, it could cause the version number to regress as well. This means Porffor cannot have both a version number which increases monotonically and the ability to introduce necessary changes which cause Test262 regressions
[0] https://github.com/CanadaHonk/porffor?tab=readme-ov-file#ver...
I have been working on providing quickjs with more node compatible API through llrt [1] for embedding into applications for plugins.
> 1+1
2
> help
Uncaught ReferenceError: help is not defined at exports.<computed> [as main] (file:///opt/homebrew/lib/node_modules/porffor/compiler/wrap.js:494:19) at REPLServer.run (file:///opt/homebrew/lib/node_modules/porffor/runner/repl.js:98:27) at bound (node:domain:432:15) at REPLServer.runBound [as eval] (node:domain:443:12) at REPLServer.onLine (node:repl:927:10) at REPLServer.emit (node:events:532:35) at REPLServer.emit (node:domain:488:12) at [_onLine] [as _onLine] (node:internal/readline/interface:416:12) at [_line] [as _line] (node:internal/readline/interface:887:18)
Any language that allows generating and interpreting its own code at runtime will have the "eval problem". From some other comments here, it sounds like Porffor's solution is to simply ignore it.
It was called hiphop-php, then they eventually gave up, before creating hhvm on a complete new concept.
After HHVM proved that its JIT compilation engine was faster than their HipHop AOT attempt, did they decided to focus only on HHVM going forward.
I wont be surprised to see a SPA framework that uses Porffor once it is more mature, or even the major ones using it as part of their tooling.
WASM is the next step after SPA's essentially.
If you have never touched Blazor, I recommend you check it out via youtube video if you don't do any C#, it is impressive. Kudos to Microsoft for it. I have had 0 need or use for JavaScript since using it.
[0] https://bun.sh/
import * as squint_core from 'squint-cljs/core.js'; console.log("hello");
*or at least, in theory
And most devs stay away from eval for well-deserved security reasons.
AFAIK the only option is to include an interpreter.
I'd recommend checking the work on making SpiderMonkey emit Wasm as a backend