312 points · by 314testing · 9y ago · 107 comments
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.
[1] https://medium.com/gopherjs/surprises-in-gopherjs-performanc...
It's used for multiplying integers in SunSpider: https://github.com/adobe/chromium/blob/master/chrome/test/da...
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.
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.
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.
You, too, believe in the magic compiler that doesn't exist. You don't get good code from a high-level specification. You really have to understand what the compiler actually can do for you and write your code accordingly, either way - except the predictable case is more straightforward (though maybe not as pleasing).
With dumb compilers we are putting the need to understand every machine on every developer. Isolating what knowledge is required in different locations is exactly why we have abstractions. The compiler is just one more abstraction to help this kind of specialization and basic division of labor.
The abstraction is fundamentally leaky. As someone who worked on a compiler for years, cost models are important, and benchmarks and perceptions are very important -- but they are often non-intuitive and not totally "free", as TINSTAAFL says.
That said, optimizing something like a self-pow call to a square is probably not overreaching as an optimization or whatever. But there is certainly something to be said about over doing it and before you know it, you've kicked the can down to <person in the hallway who's really good at optimizing on platform X> because they had to do it anyway because only they know why the JVM or whatever behaves that way on a Friday with 17 inner class methods or something and you're really sure it can be faster but you've got a deadline in like a week and holy shit you have 3 other things to do.
As an aside, I think if any compiler gets the "most sane cost model award", it would probably be Chez Scheme, since it is both unbelievably fast, and based entirely around a practical metric: basically no optimization is added if it cannot speed up the compiler itself. Performance should be predictable both in terms of runtime and compile time. Since the compiler is a rather general purpose application -- if the optimization isn't worth it there, it's probably too overly specific, and likely isn't carrying its own weight. After 30 years of following this rule -- Chez is very, very fast.
Regarding the second statement: These "smart" optimizations happen before architecture specialization, so that's wrong too. But even if it was true, you now shift the burden to understanding every compiler in order to get the optimization you need.
If people expect that pow(x,2) transforms to x * x, then every compiler would have to implement that. This is a trivial example, but you in reality you always have to structure your code appropriately for some optimization to kick in, what if one of your target compilers needs a different structure?
This is what gave us C, while the rest of the mainframe world was busy using Algol, PL/I, Fortran and Lisp dialects. Where many of the compilers already were capable of doing bounds checking elision.
I'm guessing he thought using a temporary variable would look less pretty, and had no idea of the relative costs of imul and ipow.
> Compilers should be dumb, fast and predictable instead.
I don't care too much about dumb or fast, but they definitely need to be predictable. Even with something as low-level as C++ or Fortran, you often have to profile and disassemble to see if the compiler is doing the optimizations you want (e.g. unrolling loops, inlining functions, propagating constants, using SSE), or if you have to hold its hand. Trying to predict whether something as high-level as GHC will do what you want, or to figure out how to make it do so, is an absolute nightmare.
GHC also happens to be one of the slowest compilers you could possibly use. It all goes together.
One cannot realistic expect that any compiler for a given language behaves the same way.
That's a good way to get pummeled in benchmarks. Benchmark scores matter a lot to perception: just look at any HN thread about programming language or browser performance, or any review of browsers in the tech press.
We may not like it, but the benchmark-dominated world is the world we live in.
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
And this is a computation benchmark, it's not even hitting sand-boxed API overheads.
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.
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. :)
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):
I'm not aware of any specifically, but I do know at least one Rustacean who has been playing with this tweeted something like "I'm surprised but these 80 crates i've been using for advent of code work out of the box with wasm". So we'll see!
It's a bit spare, and certainly not comprehensive. I've personally gotten ICU and jsoncpp working with minimal effort. Basically if the library has good cross platform support it should work.
The opposite is also true - you have a html5.h header file you can reference to do DOM/browser manipulations from your native code.
It's pretty fascinating.
Does anyone have benchmarks that tell a different story?
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/
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).
It's worth checking the single thread performance of both your CPU models (see www.cpubenchmark.net), sometimes the higher end models actually have slower single thread performance.
Best thing since sliced bread if I may say so :)
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.
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.
Knowing nothing of rust development or this particular tool, could a potential benefit be that the outgoing javascript is structured ideologically similar to the rust code that went in? I'm not saying that this would be a primary reason to make this switch but surely this approach isn't without benefit or reason?
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!
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.
There is no way to "fix" Javascript without breaking compatibility. It will always suffer from its poor initial design decisions, or it will stop actually being Javascript.
I am not javascript expert, but it seems that the the choices in the newer versions of Javascript are similar to the new changes in C++. No language can make it impossible to prevent bad decisions, but the newer versions of these languages can make good decisions easier.
For instance:
- javascript has just one datatype for numbers, HOW CAN YOU WIRTE A NUMBERS APP WHITH PURE JAVASCRIPT SHIT, WTF?. - Debugging is a hell - No generic language - Slow as hell - Double meaning of many this like undefiend - etc - etc
All what you describe above is syntax sugar.
I have learning 6 programming languages (c, go, javascript, python, java and php)