Live data from Hacker News

JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

hacks.mozilla.org

51–60 of 79 posts

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#51
post #30

Earlier quoted context omitted.

Java and .NET also have linkers, which can help bring down that size, even if not as much as Rust.

I don’t doubt they’ll get some reduction, but it will always be more than zero.

Yep, as I stated it is never going to be as small as Rust.

Still, it might be small enough.

Check the new version of Unity for browser deployment.

https://youtu.be/EWVU6cFdmr0

796 KB for a Flash like game, with the productivity of all Unity's tooling, is already more than good.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#52
post #45

Earlier quoted context omitted.

Rust has some significant advantages compared to languages like Kotlin, Java, and C# here. A really big one is binary sizes. To get those languages to work, you have to ship the entire runtime. For example, see https://www.infoq.com/news/2018/01/mono-cs-webassembly > (the "hello world" example is 10 megabytes) The "hello world" example in Rust is ~100 bytes . Some applications and some people can pay these costs, it'…

C# etc. shipping a runtime is a current limitation of wasm and those ports to wasm. But in principle once wasm has GC support they can also compile to tiny binaries. Not for everything, of course - if you use certain C#/Java/Kotlin features you'll need bundled runtime support. But if you avoid them, you don't. For comparison, Rust can't emit tiny binaries if you use malloc/free, because it needs to bundle those. This…

Is it really on the roadmap to support GC before a simple API level malloc/free? It seems like the sane progression path would be to have WASM support an API for malloc/free first (which would be useful any system that supports a pluggable memory allocator, as Rust is getting), them an API for requesting garbage collected bytes of memory (which I assume, with fairly little prior knowledge, malloc/free would be useful for).

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#53
post #40

Earlier quoted context omitted.

> Doesn't the small size only apply if you compile to assembly? If you compile to webasm shouldn't things be different? I'm not sure what you mean, could you re-phrase maybe? The article is talking about compiling to WebAssembly already.

I think what he means is that C# apps would have a smaller size if AOT compiled to wasm instead of shipping the mono runtime with .NET assemblies. (sorry for my English)

C# has been AOT compiled since the early days.

Just that only dynamic linking was supported on the regular .NET Framework.

Unity(with IL2CPP), Mono and Windows 8 store apps also support static linking.

So the approach is the same as Xamarin for iOS or .NETbon the Windows store.

There is an additional phase that generates the native code, using MSIL as just yet another compiler phase.

As for the runtime, yes there needs to be a minimal set of services.

But even C, C++, Go, ... need to have some kind of runtime if you want to use all language features.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#54
post #40

Earlier quoted context omitted.

I think what he means is that C# apps would have a smaller size if AOT compiled to wasm instead of shipping the mono runtime with .NET assemblies. (sorry for my English)

Your English is fine! I don’t know enough about how C# is AOTd to really answer that. I mean, some runtime code would still need to be in there, I assume?

Yes, the approach is similar to all major AOT compilers on .NET space.

NGEN, .NET Native, CoreRT, Mono, Xamarin, IL2CPP, HPC#.

A minimal runtime is always needed, for some of the language semantics, FFI and memory management.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#55
post #45

Earlier quoted context omitted.

Rust has some significant advantages compared to languages like Kotlin, Java, and C# here. A really big one is binary sizes. To get those languages to work, you have to ship the entire runtime. For example, see https://www.infoq.com/news/2018/01/mono-cs-webassembly > (the "hello world" example is 10 megabytes) The "hello world" example in Rust is ~100 bytes . Some applications and some people can pay these costs, it'…

C# etc. shipping a runtime is a current limitation of wasm and those ports to wasm. But in principle once wasm has GC support they can also compile to tiny binaries. Not for everything, of course - if you use certain C#/Java/Kotlin features you'll need bundled runtime support. But if you avoid them, you don't. For comparison, Rust can't emit tiny binaries if you use malloc/free, because it needs to bundle those. This…

>GC will be "free" while malloc/free won't be

malloc is several magnitudes simpler than GC though. Then there's the question when (if ever) wasm will have GC and what kind of GC will it be.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#56
post #7
post #3

> Faster-than-JS DOM performance ... > The wasm-bindgen code generation has been designed with the future host bindings proposal in mind from day 1. As soon as that’s a feature available in WebAssembly, we’ll be able to directly invoke imported functions without any of wasm-bindgen‘s JS shims. Sweet. Once again, Rust team knocking it out of the park.

"Faster-than-JS DOM performance" That will be when the clock starts ticking on Javascript. Who'da thunk one language might eventually eat both a significant chunk of C and a significant chunk of Javascript someday? I'm not saying Rust will completely eat Javascript; there is no chance that all web developers would or even necessarily could switch to Rust. But the framework war will take a very interesting turn if Rus…

I don't really mind Javascript that much, although I do agree that there are definitely nicer languages, and that Rust is probably one (I haven't worked with Rust yet).

That said, one really nice thing that I fear we'd lose is the interoperability of the ecosystem. There are a lot of good libraries available for Javascript that help doing webdev, and I wonder what effect fragmentation of languages could have on that. Will we trade having nicer languages for availability of useful libraries in your language of choice?

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#57
post #7

Earlier quoted context omitted.

"Faster-than-JS DOM performance" That will be when the clock starts ticking on Javascript. Who'da thunk one language might eventually eat both a significant chunk of C and a significant chunk of Javascript someday? I'm not saying Rust will completely eat Javascript; there is no chance that all web developers would or even necessarily could switch to Rust. But the framework war will take a very interesting turn if Rus…

> there is no chance that all web developers would or even necessarily could switch to Rust. But the framework war will take a very interesting turn if Rust gets faster access to the DOM than JS can provide, and is also a faster language than JS, Maybe a good way of kicking this off is if someone writes a DSL/hosted language that compiles/transpiles/whatever down into Rust. Have this language be nice and easy to writ…

If this was implemented correctly, it could go viral.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#58
post #12

Earlier quoted context omitted.

Do you see any reason why it won't eventually do so?

It's still too early. There's tons of good reasons to write C code today for many projects. Furthermore, there's so much C code out there that even if nobody wrote a single new line of C today, it'd be decades before it would go away. It's really about growing the pie rather than replacement anyway, in my opinion.

>even if nobody wrote a single new line of C today, it'd be decades before it would go away.

But then we can get sweet sweet COBOL level salaries in 40 years.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#59
post #16

Earlier quoted context omitted.

Rust isn't the only player here. Kotlin I think does webasm already. And I think Java and C# will also jump on the bandwagon eventually.

Rust has some significant advantages compared to languages like Kotlin, Java, and C# here. A really big one is binary sizes. To get those languages to work, you have to ship the entire runtime. For example, see https://www.infoq.com/news/2018/01/mono-cs-webassembly > (the "hello world" example is 10 megabytes) The "hello world" example in Rust is ~100 bytes . Some applications and some people can pay these costs, it'…

Kotlin specifically has a "native mode" where it doesn't use the JVM and ships its own small runtime with a ref-counting cycle detecting gc, C interop and not much else.

The binaries aren't 100 bytes but I suppose with more optimisation they could be. And although Kotlin/Native isn't actually exactly the same language as Kotlin/JVM it's got very good usability and IDE support already. So I think it can be quite a strong competitor for Rust in many areas.

Re: JavaScript to Rust and Back Again: A Wasm-Bindgen Tale

#60

Earlier quoted context omitted.

IIRC wasm has no GC capabilities, so Python is an unlikely source language.

In cases like that, you also compile the Python GC to wasm and use it. It ships with your app, adding bloat to your binary though. It does work!

Ah gotcha, have also been pretty impressed by pypyjs http://pypyjs.org/
Post reply on HN