News for hackers. Stuff that matters.
Re:TypeScript as fast as Rust: TypeScript++ by janpaul123 on Thursday April 07, 2022 @01:36PM (#30947766)
Hi! Author here. Really curious what you all think. It's a pretty crazy idea but maybe there's something to it! Hacker News is always a special place for ideas like this — you never know what you're gonna get ;) — so I'm looking forward to discussing this with y'all!
Re:TypeScript as fast as Rust: TypeScript++ by mattgreenrocks on Thursday April 07, 2022 @02:00PM (#30948107)
The TypeScript++ example looks like the type of code that JITs typically like. Python has Numba, where users affix a @jit annotation to functions to request that. It works pretty well in my experience.

However, at this point, I'm mostly of the opinion that langs need some notion of mechanical sympathy if they want to be used for high-performance. JS engines probably have tens of millions of dollars of dev effort poured into them by now, yet they get soundly beaten by WASM, which is much younger. The conceptual foundation matters a lot.

That said, there's probably a nice space for an ergonomic, reasonably fast PL that sits between Rust and JS.

Re:TypeScript as fast as Rust: TypeScript++ by notriddle on Thursday April 07, 2022 @06:03PM (#30950870)
> That said, there's probably a nice space for an ergonomic, reasonably fast PL that sits between Rust and JS.

Other than not running natively in web browsers, isn't that language called Ocaml?

Re:TypeScript as fast as Rust: TypeScript++ by scns on Friday April 08, 2022 @07:44AM (#30955751)
Is it still possible to use the ReasonML syntax for native binaries?
Re:TypeScript as fast as Rust: TypeScript++ by discreteevent on Thursday April 07, 2022 @05:46PM (#30950725)
That was the main motivation behind the inventors of dart. They got frustrated with all the hacks they had to put into V8 just because JavaScript was so mechanically unsympathetic. You can hardly blame then because they originally came from a background of developing smalltalk and self VMs.
Re:TypeScript as fast as Rust: TypeScript++ by emteycz on Thursday April 07, 2022 @06:06PM (#30950892)
Isn't JS much more Smalltalk-ish than Dart? I don't have much experience with Dart, but it always seemed like a very OOP language to me (and not in the "message passing" meaning).
Re:TypeScript as fast as Rust: TypeScript++ by mattgreenrocks on Friday April 08, 2022 @03:26PM (#30961441)
This is super interesting to me as a VM geek.

I'd written off Dart as "Google's Swift" for the most part, but this makes a lot more sense. Thanks for the comment.

Re:TypeScript as fast as Rust: TypeScript++ by isaacimagine on Thursday April 07, 2022 @02:51PM (#30948778)
> JS engines probably have tens of millions of dollars of dev effort poured into them by now

And JS engine JIT codegen modules are put to good use compiling wasm.

Re:TypeScript as fast as Rust: TypeScript++ by janpaul123 on Thursday April 07, 2022 @03:02PM (#30948928)
Good points. I agree that choosing the abstractions wisely is key here.
Re:TypeScript as fast as Rust: TypeScript++ by tomxor on Thursday April 07, 2022 @02:49PM (#30948752)
Your example really clicked with me, having written a substantial quantity of JS in a "memory managed" way to avoid GC, and avoiding memcpy by passing buffers in place of pointers.

My initial instinct is that putting such considerations behind a preprocessor syntax has the disadvantage of making them more opaque and magic, and that I personally would continue to do such things more explicitly - which I realise is an ironic thing to say about an untyped, interpreted language, it's probably just the little pseudo "grey beard" on my shoulder which occasionally whispers silly things about "building their own computer out of rocks and twigs".

I guess the point is that there's a balance to the cost vs benefit: making it more convenient (what you call ergonomics), making it more accessible to those that were less likely to manually use that technique, and perhaps resulting in an average of more performant code; but at the cost of more obscurity, and more "magic", perhaps resulting in less educated programmers.

I'm probably overthinking it. Ultimately this argument even applies to for loops, but it's worth being conscious of that cost - it's likely a good idea.

Re:TypeScript as fast as Rust: TypeScript++ by janpaul123 on Thursday April 07, 2022 @03:04PM (#30948968)
Yeah I hear you; and even allude to that in some parts of the article, like this part:

> Another idea to make Typescript–– a bit less restrictive and more ergonomic, would be to use reference-counting of objects, and then try to optimize most away using compile-time reference counting as pioneered by Lobster. This gives most of the advantages of manually managed memory, but makes the ownership model much easier to reason about. It is however slightly less performant, and more importantly, makes performance less predictable, since it becomes more reliant on compiler cleverness.

Even Rust can hide actual performance behind abstractions, and I've already been bitten by that a few times.. Not sure what the right solution is here, but I agree that it's super important to think about this when designing a language like this.

Re:TypeScript as fast as Rust: TypeScript++ by triyambakam on Thursday April 07, 2022 @04:57PM (#30950260)
> having written a substantial quantity of JS in a "memory managed" way to avoid GC, and avoiding memcpy by passing buffers in place of pointers.

What projects were you working on that required that? It sounds interesting

Re:TypeScript as fast as Rust: TypeScript++ by tomxor on Thursday April 07, 2022 @05:04PM (#30950333)
Mostly a variety of web based science education simulations. Usually boiling down to some kind of miniature specialist physics engine or procedural animation.

Beyond that, it's more of a personal philosophy than a requirement (I could easily have done all those things more carelessly, but the result wouldn't be so nice, some people's laps would be hotter, and some people would end up with a slow or jerky simulation). I like things to be efficient, I enjoy finding a balance between "efficient" and "minimal". That doesn't mean I avoid GC all over the place adding unnecessary complexity and over optimizing, it just means being considerate where it matters and "idiomatic" where it doesn't - in a physics engine (or any kind of posteriori simulation) it matters because it's running on an interval and will constantly cause perceptible GC hiccups if you don't be considerate with memory.

Re:TypeScript as fast as Rust: TypeScript++ by fancy_hammer on Friday April 08, 2022 @10:34AM (#30957341)
Would love to read a blog post about what this kind of code looks like!
Re:TypeScript as fast as Rust: TypeScript++ by Jasper_ on Thursday April 07, 2022 @06:08PM (#30950917)
I've done this before for plenty of tight loop code. Nothing irks me than staring a profile and seeing "Major GC" multiple times in the middle of a big algorithm.

It's certainly not helped by the latest language fads just completely flooring the GC gas pedal; special mention should go to for...of creating a brand new object for each loop iteration. That's usually the first thing I rip out of tight loops.

A bit of care can make this stuff go 3x fast on PC, and I've gotten completely unusable experiences on mobile (most websites) to be 30-40fps.

Re:TypeScript as fast as Rust: TypeScript++ by triyambakam on Thursday April 07, 2022 @07:15PM (#30951473)
Do you have any resources you like for diving deeper into profiling and learning the nature of the JS GC?
Re:TypeScript as fast as Rust: TypeScript++ by transitivebs on Thursday April 07, 2022 @04:31PM (#30949971)
Really interesting article.

I was doing some high-level research on a related question the other day: https://transitivebullsh.it/webassembly-research-9e231b80e6d...

I know you've responded to a few people on here that Assemblyscript doesn't address the main issues you're talking about in the article, but honestly it does have a lot of things going for it.

I'd love to see a more direct breakdown of this space that explains why prior works like assemblyscript, nectarjs, walt, etc all fall short, and where an ideal solution would need to do better.

^^ this is the type of thing I'd love to chat about if you're interested btw

Re:TypeScript as fast as Rust: TypeScript++ by janpaul123 on Thursday April 07, 2022 @06:27PM (#30951083)
Nectarjs is interesting; I wasn't aware of that. How did it manage to avoid (for example) a garbage collector without making changes to the language?
Re:TypeScript as fast as Rust: TypeScript++ by mc4ndr3 on Thursday April 07, 2022 @01:40PM (#30947828)
Why not write Rust directly, and target WASM for client side environments?

I support efforts to tune existing tech stacks. But there is such a more direct path to performance, reliability, etc etc by dropping (alt)JS entirely now that anything can compile to WASM.

Re:TypeScript as fast as Rust: TypeScript++ by janpaul123 on Thursday April 07, 2022 @01:44PM (#30947885)
I agree; that is what we built the Zaplib framework for. But the reality is that there are tons and tons of existing JS/TS codebases out there, and it'd be nice to have an incremental speedup path for them too.
Re:TypeScript as fast as Rust: TypeScript++ by michael_j_ward on Thursday April 07, 2022 @01:44PM (#30947886)
Re:TypeScript as fast as Rust: TypeScript++ by vanderZwan on Friday April 08, 2022 @05:54AM (#30955158)
"Why can't we have a language that compiles to highly optimized JS?" is definitely something that has crossed my mind, especially in situations where I reached for faking structs with TypedArrays. As soon as that gets more complicated than something like an array of x,y,z points things get difficult (not to mention almost impossible to debug) really fast.

I suspect that if you could come up with an easy way to express a large tree really efficiently in a TypedArray in a relatively generic way, that you already will do better than most attempted solutions here.

Re:TypeScript as fast as Rust: TypeScript++ by pjmlp on Thursday April 07, 2022 @03:45PM (#30949487)
Love it, you should check the Typescript compiler used by MakeCode as well.

Maybe there are some ideas there.

Re:TypeScript as fast as Rust: TypeScript++ by novocantico on Thursday April 07, 2022 @02:01PM (#30948123)
The main feedback I have is that I wanted to read this near the end of my lunch break, because I've been interested for years in the idea that a reasonably-restricted subset of TypeScript (who needs all that dynamism) can almost definitely be compiled to machine code and be made super fast, faster than V8 -- so I was looking for a TLDR in this article and spent a couple minutes looking for it and then just reading whole paragraphs and still don't really know what you've got here.
Re:TypeScript as fast as Rust: TypeScript++ by mattdeboard on Thursday April 07, 2022 @04:33PM (#30949986)
dang imagine how much more leisurely you could've read the article if you didn't also take some of your lunch break to write this comment!
Re:TypeScript as fast as Rust: TypeScript++ by janpaul123 on Thursday April 07, 2022 @03:00PM (#30948915)
Good feedback; I should add a TL;DR. Basically this is a proposal to create a language that sits somewhere between Typescript and Rust, and which you can incrementally adopt if you already use Typescript.
Re:TypeScript as fast as Rust: TypeScript++ by Syzygies on Thursday April 07, 2022 @05:26PM (#30950536)
You might want to reconsider the ++ naming convention. Many people capable of a critical appraisal of C++ wouldn't want an association with C++. The prejudice that your name triggers is that you don't understand what others think C++ got wrong, and you're likely to be making the same mistakes.

It doesn't look like you're making the same mistakes, but why trigger that prejudice?