
The Rusty Web: Targeting the Web with Rust - 314testing
https://davidmcneil.github.io/the-rusty-web/
======
sunfish
It looks like the slowdown on wasm is related to the powi call in the distance
function (which the benchmark page mentions as a likely suspect). Emscripten
currently compiles @llvm.powi into a JS Math.pow call, however this powi call
is just being used to multiply a number by itself. I filed this issue:

    
    
      https://github.com/kripken/emscripten-fastcomp/issues/171
    

to track this issue in Emscripten.

In JS, the assumption is that code may be written by humans, so engines are
expected to optimize things like Math.pow calls with small integer exponents
implicitly, which is likely why this code is faster in JS. And in the asm.js
case, the page mentions that it's using "almost asm", which is not actually
asm.js, so it's using the JS optimizations.

This is one of the characteristic differences between JS and WebAssembly: in
WebAssembly, the compiler producing the code has a much bigger optimization
role to play.

~~~
galacticpony
The whole code is horribly over-engineered, at least for a performance
benchmark.

Anybody who uses a pow call to square a number must believe compilers are
magic. Worse yet, some compilers might actually optimize for that case,
reinforcing that belief.

Compilers should be dumb, fast and predictable instead. That way, everyone
gets what they deserve.

~~~
jbclements
Wow; I really disagree with this viewpoint. I want clever compilers that
produce good code from high-level specifications. I fully accept the lack of
predictability.

Also, your comment that "everyone gets what they deserve" suggests that those
who write high-level code (e.g. using pow() to square a number) "deserve" to
have slow code. That's a strange moral outlook.

~~~
shurcooL
You have to keep in mind that adding support for special cases makes the
general case a little bit slower (because it has to always check if it's the
special case or not). It's not completely free to make pow work fast on small
integers.

So, it's a trade-off. I would also prefer less special cases optimized for
naive use, but only if it's accompanied by documentation that says "prefer X
instead of pow(x, 2), prefer Y instead of pow(-1, k), etc." It shouldn't be
guesswork as to what's best to do for common cases.

~~~
lomnakkus
That's not necessarily true. I'd wager that _most_ cases where pow(x, 2) is
used as a way to square x, the "2" is actually a constant. That's trivially
statically optimizable at compile time.

~~~
dllthomas
In fact, if you are using pow _specifically_ to "square a number" in the sense
where you _could_ replace it with x*x, it is guaranteed to be something you
can determine statically (and probably pretty easily) - or you're already
doing a lot of unnecessary work.

------
markdog12
Unreal Engine's Zen Garden using WebAssembly:
[https://news.ycombinator.com/item?id=13820633](https://news.ycombinator.com/item?id=13820633)

~~~
dmix
Couldn't get this to work with Firefox 52, or google-chrome-unstable or
chromium on Linux. Even with the various about://flags turned on for webgl 2.0
and wasm

~~~
calcifer
Works fine for me with Firefox 52 on Linux, using nVidia 378.13 drivers.

On Chrome 56 with default flags, the tab immediately crashes upon page load.
I've tried enabling the "Experimental WebAssembly" flag, which prevents the
crash but the demo fails to load with:

WebAssembly instantiation failed: TypeError: WebAssembly.instantiate is not a
function

------
rubber_duck
Interesting how when you read the articles about WASM and ASMJS you see stuff
like "1.5-2x peformance loss over native code" but here it's 35-3x.

And this is a computation benchmark, it's not even hitting sand-boxed API
overheads.

~~~
steveklabnik
They didn't document what version of Rust they used, but our support is still
fairly preliminary. For example, it wasn't until very recently that we even
passed in flags to optimize the output at all. My understanding is that
there's still a lot of low-hanging fruit with the integration generally.

~~~
TurboHaskal
If it's a blog post, it's probably nightly.

~~~
steveklabnik
That doesn't answer the question of "which nightly". Is this a new post? I
don't see a date anywhere.

But regardless, my ultimate point is mostly "hey this stuff is brand new so
some stuff not being great is to be expected." The exact details aren't very
important.

------
makmanalp
As a sidenote, does anyone know how this emscripten stuff works? I understand
how you can compile down to a different language, but is there a shim layer
that converts OS level API calls to browser APIs? Or is it lower level than
that? Either way, that sounds like a massive undertaking and it blows my mind
that any of this is even possible.

~~~
ndarilek
Came here to ask exactly this question.

Further, and I don't even know if this question is a meaningful one, but are
there any curated lists of libraries known to work with wasm, or do you just
compile something and hope for the best? I'm curious if there are any gaming-
oriented sound libraries known to work well, and with bindings to Rust? I'm a
blind developer with a casual interest in building audio games, a niche gaming
genre that's basically what it (har har) sounds like. Unfortunately, much of
what is available is Windows-only, and I'm a Linux user. But increasingly over
the past few years, the web has struck me as a great platform for building
these sorts of games. You don't need a powerful CPU for one thing, and the
browser's built-in TTS APIs abstract away one of the biggest platform hurdles.
There really isn't a good cross-platform hook into text-to-speech systems, and
often you need speech presented in ways that clash with how screen readers
work.

Anyhow, I've been wanting to experiment with multi-player gaming, and Rust's
new wasm support along with Firefox 52's inclusion seems like a nice bit of
synchronicity. If only I could find a good audio library known to work,
particularly something with spatialization.

And yes, I do know about the Web Audio API, but was disappointed to learn that
its panner nodes don't support more than 2 speakers. That said, I don't know
if there are any wasm-compatible libraries that'd work with Rust and support
5.1. I'm also looking for a fun project to use Rust with, and an audio game
that uses the strengths of both the web and Rust might be just the thing.

Pardon the diversion, but I'm stupidly excited about using the browser as a
cross-platform runtime via a language more sane than JavaScript. :)

~~~
flohofwoe
I've written a blog post about some (C/C++) libraries I found working well in
asm.js/wasm:

[http://floooh.github.io/2016/04/09/oryol-3rd-party-
code.html](http://floooh.github.io/2016/04/09/oryol-3rd-party-code.html)

All the C libraries in there should be trivial to integrate with Rust, the C++
libs might be a bit more complicated though.

For audio, I can recommend SoLoud, it's multiplatform and also works well in
asm.js/wasm (and doesn't add much size):

[http://sol.gfxile.net/soloud/](http://sol.gfxile.net/soloud/)

------
floatboth
"Native WebAssembly support is not available in this browser" Firefox Nightly.
Something's wrong with the detection

~~~
callahad
The binary has version 0xD, the last pre-release version of WebAssembly.
Firefox and Chrome are now looking for version 0x1.

------
bfrog
How does rust deal with numbers in JS given that a u64 is unrepresentable in a
native JS number type? Does it simply keep an array of byte values around
instead?

~~~
steveklabnik
wasm != JS, basically.

------
CorySimmons
A few comments are mentioning the implementation is probably dated.

Does anyone have benchmarks that tell a different story?

~~~
flohofwoe
Not a benchmark, but my 8-bit emulator let's you compare asm.js and wasm
performance between browsers and platforms somewhat, and (if compiled
natively) also for native x64 or ARM performance:

[http://floooh.github.io/virtualkc/](http://floooh.github.io/virtualkc/)

It's implemented in a C-with-classes style C++ though, not Rust.

When you press the 'UI' button, there's a little millisecond timer in the top-
right corner which tells you how much time is spent in the 'emulator frame',
which is pure asm.js/wasm code which doesn't call out into JS APIs.

You can switch between the asm.js and wasm version through the hamburger-menu-
icon at the top-left corner.

I'm seeing the "1.2..1.5x slower than native" performance there.

Some of my other demos are also fairly CPU heavy, for instance the two Bullet-
Physics demos here:

[http://floooh.github.io/oryol-samples/](http://floooh.github.io/oryol-
samples/)

These demos have asm.js, wasm and pnacl versions and also have fairly detailed
timing information which makes it possible to compare performance to the
natively compiled versions.

I _do_ currently see some mysterious performance differences between Mac and
Windows though, not sure what's up with that (e.g. my mid-2014 MBP with a 2.8
GHz i5 is giving better performance than the 3.5 GHz i7-4770K in my work PC).

~~~
hansjorg
Great demos! What is the UI you're using in the mod and sid players?

~~~
flohofwoe
Dear Imgui:
[https://github.com/ocornut/imgui](https://github.com/ocornut/imgui)

Best thing since sliced bread if I may say so :)

------
Animats
_This is accomplished by compiling Rust to asm.js or WebAssembly._

This seems an overly complex approach. Just because you can do it doesn't mean
you should. Most of the advantages of Rust don't apply when you're targeting
the Javascript engine of a browser.

Modern Javascript, as a language, isn't that bad. Use the right tool for the
job.

~~~
wtetzner
I can imaging cases where you'd want code to run in a browser without the
unpredictability of garbage collection.

Also, modern Javascript has been the _only_ available tool in browsers, but
that doesn't mean it's a good one. asm.js and WebAssembly are making it easier
to introduce new, and in many cases, better tools.

------
killin_dan
Why is rust still not standardized?

~~~
andrewflnr
It's still growing and changing pretty quickly. Even if it was going to be
standardized, it's too early now.

~~~
killin_dan
Well they've had several years already

------
loppers92
That sounds so good. I don't like javascript at all because it's a one of the
worst programming language on our tiny world!

Almost every programming languages is better than javascript. The roots of
javascript are for tiny special effects and not for huge well running
applications. Typescript and all other frameworks for javascript are just
workarounds for a buggy language.

In my opinion the final step is to replace completely javascript with
something else!

~~~
Meegul
I disagree. JavaScript is far, far from a perfect language, but if you think
it's the worst one out there, you must've not payed attention to it in the
past few years. I - as well as most non-self loathing JS hackers - use the
ECMAScript 2015 (or later) standard, where the language is relatively feature
complete. This allows JS to become a relatively complete OO language should
you desire, and it also allows it to act (mostly) functionally. `const` and
`let` alone fix many of the ambiguous gripes that people had with JS, and if
you truly believe it's still the worst language out there, I encourage you to
look at these new developments.

~~~
nilved
He said it's "one of the worst." And even people who write JavaScript admit
that.

~~~
Meegul
Perhaps it is. PHP is also typically ragged on for being "one of the worst",
yet it runs the majority of the web. The common theme between JavaScript and
PHP is that it's possible to write some pretty awful code, but that doesn't
make it impossible to write _good_ code in either of them. Do we measure
languages by the worst we can create with them, or the best we can create with
them?

It's also easy to be "one of the worst" when there are only a handful of
languages that see widespread use. You only need to be in the bottom 5 of 15
or so languages, but my point is that that may not imply that it's a bad
language.

~~~
loppers92
you need to learn at least 3 languages that you know exactly what means
writing good or bad code with a language. After the time you don't want write
code with a poor language. If you have learned a better language.

A Software engineer uses always the best tool and not the worst.

~~~
oldsj
The best(only) tool available may be a bad one

~~~
loppers92
Dart is a alternative

