14 ms·
WebAssembly 101: A developer’s first steps
- DamonHD 9y agoNice! With a BBC Micro (6502, 1MHz) I was able to achieve about the same FPS (I was genlocked for UK PAL TV transmission so either 25FPS or 50FPS) but only for a 40x25 grid, using 6502 assembler embedded in BASIC!
- kranner 9y agoMuch as I miss the Beeb, this is completely irrelevant.
- midgetjones 9y agoI'd be inclined to agree. Unless the point was not to show off, but that a 30 year-old computer is as fast as the bleeding edge in browser rendering technology.
- kranner 9y agoAn unoptimised implementation says nothing about what WASM is eventually capable of. This is an apples-to-oranges comparison unless the WASM app were optimised for speed. (Also parent mentions a 40x25 display on the BBC Micro). Game of Life implementations are ideal case studies for optimisation after Michael Abrash's graphics programming columns.
- flukus 9y agoUnoptimised code says a lot, it's an indication of what we can expect of real world apps that will be mostly or entirely unoptimised. It's like theoretical vs real world java performance, in limited and optimised benchmarks it might produce fast code, but in real projects it doesn't. And that's the best case, there are already people in this thread talking about compiling python to web assembly, any performance improvements over the current state of the web are going to be pissed away immediately.
- kranner 9y ago> Unoptimised code says a lot, it's an indication of what we can expect of real world apps that will be mostly or entirely unoptimised. Maybe but not in this kind of performance-sensitive application that spends most of its time in a tight inner loop. Then it really comes down to how optimised that inner loop is.
- Sharlin 9y agoGiven that this [1] runs easily at solid 60fps on Chrome, a pure software rasterizer written in pure JavaScript with some basic optimizations, I'm pretty sure that webasm is can do at least as well. The Beeb is not capable of anything remotely like that. https://jsfiddle.net/Sharlin/9w26necy/52/ https://jsfiddle.net/Sharlin/9w26necy/52/
- midgetjones 9y agoWell sure, but can you play Granny's Garden in Chrome?
- ABNWZ 9y ago... I don't share your sentiment at all. I find this relevant to the subject matter as he is, albeit indirectly, mentioning the performance of WASM. I would be surprised if you got the same performance using just plain old js on the hardware he is referencing.
- DamonHD 9y agoI'm not sure that you could load JS on the BBC Micro! The point as that for something running safely virtualised (and standardised) in a random browser and on a much larger canvas, the WASM implementation is very impressive.
- tomxor 9y agoThe JS/WASM may be limited to 60FPS if it is being forced to draw every frame, in which case it has to wait for the browser, it may be much faster. CA are great fun to optimise, i've found ways to make them very fast in JS alone, by using pure state machine method with TypedArrays and sliding window algorithm. When letting it skip frames (only drawing when a frame is available) it can go far beyond generations per second... when you get to larger grids the ultimate optimisation is to use FFT, but on smaller ones there is too much overhead and sliding window is way faster. Algorithmic optimisation is always better, but this demo works for the purpose of showing basic WASM compilation which was the authors intent, there may be an order of magnitude improvement if a different implementation was used.
- amelius 9y agoWhy? It gives a perspective on the resulting performance.
- vardump 9y agoLooks like the code is pretty slow. Lots of branches in the inner loop, like range check. https://github.com/blaze33/way-of-life/blob/master/src/js/wasm/engine.c https://github.com/blaze33/way-of-life/blob/master/src/js/wa...
- blaze33 9y agoI'll concede that I did not spend any time optimizing the C code, the focus was mainly on getting a working wasm module that integrated correctly with th js code. I'll probably have a look on this after work.
- vardump 9y agoYeah, my point was this example shouldn't be used to judge webasm performance. No point to benchmark "Hello, World!"s.
- blaze33 9y agoThanks for the comment, I added a small paragraph along this line: http://blog.openbloc.fr/webassembly-first-steps/#part4 http://blog.openbloc.fr/webassembly-first-steps/#part4
- rasz 9y agoI am actually impressed someone managed to write game of life that drops below 60 fps at ~200 points on screen.
- _pmf_ 9y agoWhy isn't the Emscripten SDK distributed via npm? Doesn't npm support host specific binary artifacts?
- almostdigital 9y agoCheck this out: https://github.com/dcodeIO/webassembly https://github.com/dcodeIO/webassembly Binaries are hosted on GitHub but will be downloaded for you
- _pmf_ 9y agoAh, thank you!
- almostdigital 9y agoI recommend anyone wanting to experiment with WASM to check out https://github.com/dcodeIO/webassembly https://github.com/dcodeIO/webassembly which takes a lot of pain out of setting up the toolchain and lets you produce much leaner binaries as well. Also keep an eye on https://github.com/dcodeIO/AssemblyScript https://github.com/dcodeIO/AssemblyScript :)
- blaze33 9y agoThanks for the link, I would probably have tried this had I stumbled on it before! I did an ask HN prior to writing this article but did not get that much answers: https://news.ycombinator.com/item?id=14483383 https://news.ycombinator.com/item?id=14483383
- reedlaw 9y agoHow about https://github.com/WebAssembly/binaryen https://github.com/WebAssembly/binaryen ?
- Matthias247 9y agoThis AssemblyScript thing looks like some nice idea for being able to work with a single language while still being able to compile some computationally-heavy but otherwise small modules down to WASM and using them later from JS/TS as an external module. I'm curious how this approach looks from an optimization point of view? I guess emscripten/LLVM can do more optimization than this the AssemblyScript/binaryen toolchain, and when one puts already the effort in for using WASM the goal should probably be to have as much optimization as possible.
- NiLSPACE 9y agoI also found this which I'd like to try out later today: https://github.com/anowell/quasar https://github.com/anowell/quasar
- Cakez0r 9y agoNot very reassuring that two of the first steps are "copy and paste this stuff from stack overflow to make it work", without any explanation as to why it's needed.
- RubenSandwich 9y agoAgreed. Setting up the environment with 0's seems like it should just be the sane default: 'env': { 'memoryBase': 0, 'tableBase': 0, 'memory': new WebAssembly.Memory({initial: 256}), 'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'}) } But it is really early on and if the Portable updating SDK is any indication it seems like they want to make it easy to get started.
- deleted 9y ago[deleted]
- pmontra 9y agoI hope they add support for garbage collected languages soon. I don't feel like going back to work with malloc and free after 20 years.
- jjnoakes 9y agoThere are more choices than malloc/free vs gc.
- pavlov 9y agoYou can use the Boehm garbage collector in Emscripten/WebAsm.
- CryZe 9y agoYou are not forced to use C or C++. Rust has automatic memory management via its Ownership System. Once you get used to it, it feels a lot like a garbage collected language.
- nathcd 9y agoMaybe https://nim-lang.org https://nim-lang.org would make sense for you? It's GC'd but compiles to C. I've been meaning to try it out and try compiling to WASM when I get a chance. This brings up a question I've been meaning to ask when the topic came up, because I know almost nothing about GC. How does nim (a GC'd language) compile to a non-GC'd language? And since this is a thing that's possible (unless I'm misunderstanding, which is likely), why does WASM need to add support for GC (http://webassembly.org/docs/gc/#native-gc http://webassembly.org/docs/gc/#native-gc)?
- ThePhysicist 9y agoI'm trying to use asm.js to port Python to JS as a shared library, which would allow us to load and run arbitrary CPython modules (compiled for JS) in the browser. I generate code using emscripten, which is also able to generate WASM (which I'm not using at the moment though). My experience so far: Emscripten is quite mature and compiles even very complex C/C++ code without complaining. Statically linking code works fine, while dynamically loading shared libraries is still a bit problematic it seems. Here's the Github project btw, help is very welcome: https://github.com/adewes/cpython-emscripten https://github.com/adewes/cpython-emscripten
- mbel 9y ago> Emscripten is quite mature and compiles even very complex C/C++ code without complaining. Because it's just Clang with WASM target.
- ThePhysicist 9y agoI think it's a bit more than that given that the project has amassed more than 16.000 commits so far. You must not forget that JS does not provide support for many of the things that are required to run a C program (e.g. printing), so emscripten needs to take care of providing alternative implementations for all these things.
- mbel 9y agoOh, alright. We are speaking about two different things. I was thinking about compilation and specifically supporting complex language features which are mostly handled by a compiler frontend which is this case is clang. And it seems that you are thinking about runtime libraries (or the whole tool chain).
- ThePhysicist 9y agoAh yes you're right, that part is luckily covered by LLVM.
- 9y ago
- jlebrech 9y agosomeone port cocoa or a xib library to webassembler or even xlib.
- dennykane 9y agoYou can play my favorite arcade game ever in a wasm gameboy emulator (credit goes to Ben Smith of Google: https://github.com/binji/binjgb https://github.com/binji/binjgb) in a web-based OS I've been working on for 5 years. This is known to work in current Chrome and Firefox. Keyboard game controls: 'w'=up 'a'=left 's'=down 'd'=right '.'=A ','=B space=start enter=select. Also, standard (17 button, 4 axis) USB gamepads should "just work" via plug'n'play. Go to this link to test it out: https://linuxontheweb.org/desk?desk_init=bHMgL3B1YjtvcGVuIEFyY2FkZSAtLWFwcGFyZ3M9ImZpbGU9L3B1Yi91c2Vycy9kZW5uaXMvZ2FsYWdhLmdiIg== https://linuxontheweb.org/desk?desk_init=bHMgL3B1YjtvcGVuIEF... The argument in the URL is the base64 encoding of an initialization script that is being passed to the desktop environment. Going to the link in current Edge should just open the "Arcade" app, with nothing else happening. You should be able to drag-n-drop ".gb" or ".nes" ROM game files from your native desktop right onto the Arcade app window, and it should just start playing. You can also just drop them onto the web desktop to save them there, then just double-click them when you want to play. That way, the file is kept in the site's local storage. Learn more about how to use the web-based OS here: https://www.youtube.com/channel/UCZkhPP7327fXcSsjD_lt67w https://www.youtube.com/channel/UCZkhPP7327fXcSsjD_lt67w If anybody has wasm-capable Edge or Safari, let me know if you can play the game.
- dennykane 9y agoCan anybody tell me if they can play the game, with a USB gamepad or without? Can anybody break 100,000? My highest score playing this on the site has been around 85,000 (my highest lifetime arcade score is probably around 300,000 or so). BTW, I actually like hanging out here on the bottom of the comments (I find the people that hang out at the top of popular HN threads to be boring, unoriginal, petty jerks... so please don't vote me up!).
- jordigh 9y agoRemember when you could hit "View Source" to see how the web was built? I don't like where this is going. Minification was bad enough, now we're going to be getting more non-free blobs shoved into our browsers and this is being touted as a great new feature for us. It will end up being a tool of control and surveillance like always. This guy says it a bit more eloquently than I can: https://lobste.rs/s/wjtu7c/webassembly_mozilla_won#c_yqsej7 https://lobste.rs/s/wjtu7c/webassembly_mozilla_won#c_yqsej7
- Cshelton 9y agoYes, it's not ideal but remember the ultimate goal, having universal apps that run at native speed and don't have to be constrained to the few large app stores. I'll give up view source for that, assuming the browser VM they run in is secure of course.
- Darthy 9y agoI think there is no technical hurdle that is stopping browser manufacturers from downloading the bytecode and showing a disassembled view of it. Plus nothing is stopping us from popularizing a trend where everybody puts a publicly discoverable debugging hints file (which would make the disassembly easier to read) with a fixed name next to all wasm files.
- Houshalter 9y agoBullshit. Wasm can't do anything js can't. Especially with asm.js. Don't try to argue that minified or transpiled js is somehow more readable than wasm. At least it's easier to decompile in theory. It's absolutely not a "tool for surveillance". How exactly are you going to surveil anything with it? It's not even a replacement for js, or at least it shouldn't be used that way. No GC for instance. And if you are doing any of the silly things mentioned in your link you have no one to blame but yourself. Like accepting binary blobs without source from contractors. The link eventually admits you can't do a lot of stuff with js and should use native code. Great, lets just have everyone download and execute untrusted and unsandboxed code. In a way that is completely incompatible across platforms. Maybe we can maked them locked behind "walled gardens" with a ridiculous tax while we're at it. Future generations will laugh at us from their completely wasm based PCs. Or we can always go back to the old way of doing anything interesting with a browser. By making proprietary, vulnerability ridden plugins. Don't you just miss the days of flash and java applets? Because surely that was a much better solution than wasm.
- MichaelBurge 9y agoIt looks like the spec is here: https://webassembly.github.io/spec/introduction/index.html https://webassembly.github.io/spec/introduction/index.html I feel like writing a mini-compiler after glancing through it. It looks closer to a binary AST than an instruction set, though.
- sidarape 9y ago> 32 bit integers also serve as Booleans and as memory addresses. I'm already looking forward the day that 32-bit pointers won't be enough for web applications.
- johngruber 9y agoThis can probably only be expected to get better and better. While JS engines are squeezing hard to get some extra performance, WASM is just beginning its life. It'd be great if someone would make (when it's technological feasible) a way to integrate this natively in node, something like: const wasm = require('wasm-native'); (async () => { const mymodule = await wasm('mymodule.c'); // use mymodule here })();
- smitherfield 9y agoWhy not just write a native extension?
- Matthias247 9y agoI think they have slightly different advantages: - WASM is platform independent, so it can easily be precompiled. Which means just the .wasm module needs to be distributed (e.g. inside a npm package). However WASM modules only have access to things that were already available to JS before (no random C/C++ libraries can be included which e.g. call into the OS, no multithreading, etc.). And the toolchain might be at the moment not yet the easiest to set up. - Node native extensions have quite good tooling support. Write a binding.gyp and the C++ files and everything works pretty smooth. The native code can also do everything that native code is allowed to do on the platform. However node native extensions are platform specific, which means either precompile them for all target plattforms (and each node release) or deliver C/C++ sources which are always compiled on the deployment plattform at npm install time. They also can't be shared between browser and node plattform. All in all I think they have slightly different usecases: - Node native extensions for accessing operating system APIs and other libraries which are not already exposed through nodes JS APIs. - WASM for making existing C/C++ code including full applications (like games) runnable in the browser - WASM for making modules which speed up pure algorithms (like audio processing) and which can be used on both platforms.
- steveklabnik 9y agoThis is a little old, but you can use wasm from node http://thecodebarbarian.com/getting-started-with-webassembly-in-node.js.html http://thecodebarbarian.com/getting-started-with-webassembly...
- mediocrejoker 9y agoI was disappointed that this doesn't really explain what webassembly is, and the link that says 'if you don't know what webassembly is, click here' just redirects to a tank game with no explanation. I thought maybe it was some way to write x86 assembly in the browser, but apparently it's a tank game.
- blaze33 9y agoThanks for the feedback, I should probably link to http://webassembly.org/ http://webassembly.org/ instead and link demos after this.
- pk2200 9y agoYou might be interested in this thread from a few weeks ago. https://news.ycombinator.com/item?id=14341623 https://news.ycombinator.com/item?id=14341623 For me, the most helpful comment in that discussion was a link to this paper, written by the WebAssembly designers. https://github.com/WebAssembly/spec/blob/master/papers/pldi2017.pdf https://github.com/WebAssembly/spec/blob/master/papers/pldi2...
- BenoitEssiambre 9y agoI wonder where the world is going with this. At first glance it looks like webassembly is a potential faster replacement for javascript in the browser. However, javascript is an increasingly popular language everywhere. I'm not sure people will want to move away from it for most development. The part that is actually causing performance problems with web application is the HTML/CSS/DOM layer which was not designed as a UI widget library but as a document rendering and styling framework. Webassembly/Webgl standardization enables the creation high performance replacements for HTML/CSS/dom. We might end up with applications that are still mostly written in javascript but that call into new webassembly/webgl ui frameworks for rendering instead of into the html/css/dom layer.
- slothtrop 9y agoWell writing in languages for native code is still just as popular, and we're moving in such a direction to allow for the same application to be available both on the web and on your machine. The pure-Javascript approach to offering the same is to package a browser with a web-app and disguise this as a standalone application, which is ass-backwards.
- lixquid 9y agoI'd wager a lot of this popularity is correlation, not causation. A lot of its popularity stems from being the dominant browser scripting language. This causes it to be popular with back-end frameworks for code re-use and shared developer skill. This causes it to be popular with, say, database engines that are used by the back-end, and so on and so forth. WebAssembly is the first steps to truly breaking that chokehold, and truly seeing how popular JavaScript the language really is.
- BenoitEssiambre 9y agoRight, however javascript will still be the only full featured language having its runtime and libraries included with the browser. That means that for other languages, only the ones with fairly light runtimes and libraries could be suitable for inclusion in most web apps. This will limit the choices for non javascript general development. It may be possible to do a webassembly/webgl rendering engine to replace html/css using a limited set of dependencies.
- flavio81 9y agoI am very excited by this project, it opens up a lot of possibilities. Finally we can use our users' web browsers to the maximum potential, in the language of choice. Consider: If a good javascript engine like V8 can go as far as being able to emulate hardware, how far can we go with something like webassembly? All the power to Webassembly!
- analognoise 9y agoNear native performance - what a joke.
- sp332 9y agoGot a specific benchmark or task you think will be much slower than native?
- analognoise 9y agoAnything? You're carting around an entire browser, it's so far from native that it's completely laughable. As a point of reference: In 1984: MacPaint, 5,822 lines of Apple Pascal, 3583 lines of assembly QuickDraw (dependency): 17,101 lines, all assembly Compiled Size: 0.05MB Today, Chrome: Number of lines of code: 16 MILLION Webkit (alone): 10.3 MILLION Install size: ~200MB We have a bunch of people who aren't skilled enough to deal with a real machine who think they're still "near native". Every single thing won't be as fast, or as small, as the underlying hardware can be using toolkits and platforms that have been available for 20+ years (cross platform toolkits and Java both). For god's sake, Java is a portable VM! wxWidgets is cross-platform, and open source! We accept an operating system in our way as it is, and now we accept not only an operating system, but an entire browser as well? Our computers are faster than supercomputers used to be - why does Photoshop (or ANY program) take so long to load? We have to go the other direction - start removing all the garbage from computing, keep the best bits, throw the rest out. If you can't hack on a real computer, that's fine - you have the Webassembly sandbox. But don't label it "near native" - it just shows you don't care enough about computers to read about them. I hear that and it sounds like my gradma talking about computers - that same level of understanding. Nana is amazing, but she's not so great at computers. And these people are supposed to be developers?
- sp332 9y agoWell I'm in favor of things running in a sandbox, especially random code delivered over the internet and run automatically in my browser. But aside from that, WebASM has a flat memory model and (so far) no built-in garbage collection. It has very few features, it's very fast to parse and it runs fast too. Even the asm.js version was fast back in 2013 https://hacks.mozilla.org/2013/12/gap-between-asm-js-and-native-performance-gets-even-narrower-with-float32-optimizations/ https://hacks.mozilla.org/2013/12/gap-between-asm-js-and-nat... As for the rest, I don't know how you're measuring. Photoshop runs a lot faster than programs on my Centris 610 did, and sure it's a bloated mess of different programs glommed together but guess what, it does a lot more than smaller programs do. Your comment counts the cost of everything and the value of nothing. You can't click a link and get a brand-new full-featured word processor running on an old Mac in 10 seconds like you can by clicking https://docs.google.com/ https://docs.google.com/ But I can run MacPaint in my browser thanks to asm.js https://archive.org/details/mac_MacOS_7.0.1_compilation https://archive.org/details/mac_MacOS_7.0.1_compilation
- madrafi 9y agoSo now I have to write C code ? XD
- madrafi 9y agoOne thing though How is taking the whole C in the browser approach ressource wise ?
- flavio81 9y agoIt is not C in the browser. It is a sort-of-virtual machine in the browsers. Browsers (i.e. Chrome) are already doing this to execute Javascript, for example. Webassembly, to put it in a simplified way, allows you to "bypass" the javascript layer and target directly the virtual-machine. And done in a compatible, well-documented way. So you can gain in performance, and flexiblity.
- flavio81 9y agoNo, you can write in many other languages. For example there are many compilers out there that will compile <insert your favorite programming language> into LLVM bytecode. And then you can translate such LLVM bytecode into Webassembly using a tool like Emscripten. Thus you can compile <insert your favorite programming language> to Webassembly!
- AriaMinaei 9y agoI wonder how it would be, had capability-based system architectures taken off and become mainstream. I guess we wouldn't need WebAssembly to run untrusted code safely, because in such a system, all objects – from whole programs to an object as small as the number 4 – would be safe and sealed off from each other on a hardware level. I'm reading Capability-Based Computer Systems [0] by Henry M. Levy (1984), motivated by wanting to learn about the Burroughs B5000 that Alan Kay has praised multiple times. I've only started to learn about these things and I don't understand the implications, but if I'm reading it correctly, such architectures would obviate the need for web-style safety measures such as process-sandboxing ala Chrome, shader sanitizing ala WebGL, etc, because everything in the system would be safe that way. [0] https://books.google.de/books/about/Capability_Based_Computer_Systems.html?id=eT6eBQAAQBAJ&redir_esc=y&hl=en https://books.google.de/books/about/Capability_Based_Compute... More about this: https://en.wikipedia.org/wiki/Capability-based_security https://en.wikipedia.org/wiki/Capability-based_security and https://en.wikipedia.org/wiki/Capability-based_addressing https://en.wikipedia.org/wiki/Capability-based_addressing
- akavel 9y agoYou may like browsing through comments by @nickpsecurity (e.g. https://news.ycombinator.com/threads?id=nickpsecurity&next=14474149 https://news.ycombinator.com/threads?id=nickpsecurity&next=1..., etc.). He's really knowledgeable about this stuff, and tries to popularize it as much as he can.
- thingification 9y agoOn display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying ‘Beware of the Leopard.”, is this mailing list: https://groups.google.com/forum/#!forum/cap-talk https://groups.google.com/forum/#!forum/cap-talk
- AriaMinaei 9y agoThere is a trove of very interesting information here. Thanks akavel and thingification :)
- radarsat1 9y agoI actually went through all the steps of getting rustc and emscripten working, got it set up in Docker. It actually works, I was able to compile a rust program to wasm. But.. because emscripten uses a custom version of LLVM and clang, the image took up 25 GB of my harddrive! I just don't have that kind of space to leave it lying around, so I'll have to wait until they integrate into upstream I guess. I might work a bit in Rust by itself and compile using emscripten later on the server, but kind of hard to do that if I want to access DOM/canvas/webGL, etc.
- baq 9y agoi've seen one blogpost where the author tried and apparently successfully managed to run a QT app compiled to wasm. has anyone here tried that?
- toni 9y agoIs there any about:config flag in Firefox to disable WASM all together?
- livingparadox 9y agoWhy do you want to do that? afict, this shouldn't be increasing the attack surface any more than almost any new language feature. Its spec-ed to run in the same VM as JS is currently.
- drumttocs8 9y agoWill this eventually let us use jQuery and have it perform just as well as the latest framework? ;)
- wcummings 9y agoI'm sorta afraid of what people will do with wasm. I'm fairly sure 99% of web programmers won't need this but will use it to ship all kinds of stupid garbage that will inevitably wind up executing on some page I'm debugging.
- dandare 9y agoSurprisingly I am struggling to find such essential information as where exactly is wasm supported right now. In latest Chrome? How about Edge? how about the browser on my smartphone?
- blaze33 9y agoCanIuse is very useful for this: https://caniuse.com/#search=wasm https://caniuse.com/#search=wasm