13 ms·
Firefox's low-latency WebAssembly compiler
- saagarjha 7y ago> In SpiderMonkey there is no mechanism currently to tier down; if you need to debug WebAssembly code, you need to refresh the page, causing the wasm code to be compiled in debugging mode. Is there ever a reason to "tier down" WebAssembly besides debugging? In JavaScript functions will often get deoptimized back to the baseline interpreter if they side exit too often, but is this a thing that needs to be done in WebAssembly?
- dathinab 7y agoAs far as I know: Not really the initial "downgraded" code is normally worse in more or less any aspect. It's just emitted fastly. The only reason I could come up with is to downgrade to them upgrade again to a differently optimized code. But then WebAssembly doesn't need to do speculative optimizations like JS needs. So I guess it's not really relevant.
- sfink 7y agoIn theory, you could generate better code after having observed the actual arguments to various functions and recompile while treating them as constants. Basically profile-guided (re)optimization. But the observation would slow down the initial code, it would be a large implementation effort, and the gains would be dubious imho. Especially given how much information was already lost via the initial AOT (ahead of time) optimizing compile; you might need to keep the original source around or something. Unlikely to happen.
- dathinab 7y agoThanks. I considered this scenario as llvm does somewhat support profile-guided optimizations but it would be a fundamental change where we go from "unoptimized" to "optimized-with-tracing" instead of optimized, so that step could still have optimization upgrade hooks, so we still wouldn't need a downgrade to "base-line". I guess. I agree that it's anyway unlikely to happen, at least in the browser. I could imagine that some WebAssembly based cloud provider (or similar) would be potentially interested in generating profiles and then use that for better optimizations in other instances (so still no down-/upgrade ).
- mappu 7y ago> you could generate better code after having observed the actual arguments to various functions and recompile while treating them as constants This transformation is valid for pure functions; therefore you can mark it constexpr before compiling to WASM. There's the `constexpr-everything` project (among others) that uses libclang to apply constexpr to all legal functions/expressions in a codebase. That's for C++ - Rust is working on `const fn`.
- saagarjha 7y ago> This transformation is valid for pure functions; therefore you can mark it constexpr before compiling to WASM. It's also valid for non-pure functions at runtime.
- jmeyer2k 7y agoIsn't it because the compiled code makes some assumptions about the types of the arguments? So if the types of the arguments change, it would need to downgrade to interpreted code?
- steveklabnik 7y agoWebassembly is statically typed.
- hinkley 7y agoI'm trying to remember why, but the JVM backtracks for a couple of different scenarios. wasm can still lazily load code, right? "Static" in that situation is a little fuzzy. You can't make wholesale changes to the types already loaded, no, but the behavior out at the leaf-nodes can be altered by a new leaf showing up. Some assumption or inlined conditional may be wrong or simply ill-advised once new code arrives.
- madisp 7y agoIIRC the JVM optimizes very aggressively based on runtime profile, and just adds traps to the optimized-out paths to collect new runtime profile. Imagine code like this, where at runtime foo is always null if (foo != null) { foo.bar(); foo.baz(); } if foo is always null the compiler will just remove all of the statements inside and places a trap there. If foo happens to be non-null (say, due to external environment changes) then the it hits the trap and the whole method is deoptimized and goes through the profiler again. The optimizations in the JVM are quite fascinating - https://advancedweb.hu/profile-based-optimization-techniques-in-the-jvm/ https://advancedweb.hu/profile-based-optimization-techniques... I imagine javascript engines employ similar techniques
- chris1993 7y agoThe runtime optimisations are remarkable but it does look like runtime performance can change unpredictably. Has this been your experience?
- saagarjha 7y agoYes, the JVM tends to speed up over time as it applies these optimizations.
- MaxBarraclough 7y agoA fair question. In principle this can happen, and HotSpot can respond to changes in the program's execution patterns, but to my knowledge it's not a problem in practice with real-world Java servers. Other than a 'warming up' phase, real-world performance doesn't tend to vary wildly as time progresses. (Disclaimer: I've not done a great deal of real-world Java work.)
- snek 7y agoJS functions go back and forth because they're trying to provide optimized representation of functions based on guesses they make about the code. WASM plays an entirely different game. WASM bytecode is generally already extremely optimized (compiled with compiler -O flags, etc), the optimizations that a browsers apply are more about the machine the WASM running on (dealing with register allocation, simd instructions, etc). There is no guessing or opportunistic optimization like there is in JS.
- saagarjha 7y agoThere's still profile-guided optimization.
- davnicwil 7y agoWhat's the latest with WebAssembly? I must admit, I'm not fully sure I understand what its goals are and what we should expect its usecases to be. For a time there seemed to be a lot of hype around writing SPAs in whatever language you like, not just JavaScript, but that seems to have cooled off somewhat - at least that's my impression. Last time I checked in it seemed to be actually quite the opposite - good for low level stuff, data processing, graphics, things in other threads essentially, but if you want to touch the DOM at all just use JS seemed to still be the standard advice. I haven't looked again since. Have things progressed on that front, and if not was that ever really a goal of WebAssembly anyway?
- butterthebuddha 7y agoI would love an expert's thoughts on whether something like MathJax/KaTeX can take advantage of WebAssembly for faster rendering. Seems like a perfect use case to me.
- saagarjha 7y agoA better solution would be to improve MathML support in browsers.
- xmmrm 7y agoThere’s good news on that front: https://mathml-refresh.github.io/mathml-core/ https://mathml-refresh.github.io/mathml-core/
- klodolph 7y agoMathJax will probably not benefit much from WebAssembly. It doesn't actually render math itself. It just creates HTML + CSS (or MathML), and the browser renders it using the normal, highly-optimized rendering paths.
- elcritch 7y agoBut it does parse and compile latex expressions right?
- bluntfang 7y agowhat's the use case for WebAssembly? Why are people working on it and why should I use it? edit: why am i being downvoted? Is this a bad question?
- cbdumas 7y agoI have only played around with it, but to me the possibility of using a language other than JS to write web apps is really exciting. I don't hate JavaScript by any means, I've used it professionally off and on for years, but it still frustrates me that for webapps you're basically forced to use it.
- sim_card_map 7y agoits been more than 5 years, and nothing useful so far you can do "cool" stuff like running DOOM in your browser... but why?
- __s 7y agoI brought it into openEtG to speed up the engine's RNG: https://github.com/serprex/openEtG/blob/master/src/rng.wat https://github.com/serprex/openEtG/blob/master/src/rng.wat
- Yuioup 7y agoRAD tools like Delphi for WebAssembly or Microsoft .NET WinForms is what springs to mind.
- sfink 7y agoAs I understand it: (1) calling an optimized library written in another language for performance or size -- say you have a physics engine component in your game, or a graph traversal algorithm, or whatever; (2) calling an existing library that is available in another language with no JS equivalent; and (3) running a full existing application of some sort that is written in another language and giving it a Web interface. Oh, and (4) sandboxing untrusted code, and coming up or already present (I'm not keeping close track) (5) portability, where you can either run it on the Web or on a truly cross-platform runtime (WasmTime I think?). (And the downvotes are probably because your question could be interpreted as implying that Wasm is useless and people are wasting their time. Even though you're asking a totally fair question, it's easy to read the tone as negative and dismissive whether you meant it that way or not.)
- nojvek 7y agoAFAIK I know Figma uses a fair bit of webassembly for perf. Their editing core is C++ compiled to webassembly which is then bound to a canvas. I am pretty impressed at how silky smooth their UI is. (I could be wrong) If anyone from Figma is reading this, would love if you could shed more light on how you use webassembly and your learnings.
- dmitriid 7y agohttps://www.figma.com/blog/webassembly-cut-figmas-load-time-by-3x/ https://www.figma.com/blog/webassembly-cut-figmas-load-time-...
- jokoon 7y agoI'm still wondering why webassembly is not used more. I did not discover helloworlds tutorials, how to call js from WASM and vice versa... I sense WASM is still a little fastidious? I would love to have python released as a python WASM blob, there's pyiodine but it's not just python. I thought I would see more libraries compiled to WASM but it doesn't seem to be the case. I want to guess the c/c++ toolchains are lacking support and usage, or maybe C/C++ devs don't care about WASM? I know I do. WASM lets non-web-developers run their code in a browser, but web developers don't really care since they prefer writing JS. Maybe that's why WASM is not taking off. That's a shame.
- snuxoll 7y agoWASM doesn’t have access to the DOM which really limits use cases.
- _hardwaregeek 7y agoThere's not a lot of good reasons to use WebAssembly for most apps. You need code that is performance critical with light IO/DOM access, and can be written in the handful of languages that can be practically compiled to WASM, i.e. not just porting the runtime to WASM. Fact of the matter is that JS has evolved to be the best language for most front end development tasks. It's scriptable, somewhat performant due to JITs, quite good for single threaded async reactive programming and actually can be written in a nice pseudo-functional manner. And the libraries are quite good, even if NPM gets some hate.
- bilkow 7y agoI believe that its actually possible to run python in WASM, see https://wapm.io/package/python#shell https://wapm.io/package/python#shell
- thefounder 7y ago>> I'm still wondering why webassembly is not used more. Because it doesn't support direct DOM access(thus is slower than JS and more mem hungry) and lacks GC so many languages (i.e php, ruby, python etc) don't run (at all or just not efficiently) in the browser.
- 7y ago
- jnordwick 7y agoReal question. Most of the wasm instructions are very low level (eg, the test and branch are different, similar to assembly). But then when you get to the control flow constructs they went in the this weird direction. There is no goto, some of the instructions (br) are context dependent, and if the wasm JIT tries to inline anything it has to muck with the br operand (not even the worst of it). The looping constructs are just odd too. When I last checked you couldn't even do basic blocks with a control flow graph like most other compilers (so that also means no jump threading optimizations, and without goto you would have a hard time optimizing at the wasm generation level). It just seems like this part of the instruction set was developed by somebody who wanted some weird higher-level asm, but others wanted an asm close to the instruction set. Just seems like a really bad decitions. Is there a reason for this or has this been cleaned up at all?
- naikrovek 7y agoNo "goto" means no JUMP instruction, right? I mean I guess that's still Turing-complete, but it seems like that would make things a lot more difficult than necessary. I would love to know the answer to your question
- jnordwick 7y agoalso no conditional jumps (equivalent to jnz, jeq, etc...) instead of uses this weird br_if that can only go to places on the control flow stack.
- Rusky 7y agoThat was done entirely on purpose, to make validation simpler. It does make things harder for compilers that want to support irreducible control flow, but the workaround is a single "relooper" algorithm that doesn't really fundamentally change how those compilers work. It is possible that in the future this will be relaxed (without giving up on the benefits for validation) via something like this: https://github.com/WebAssembly/funclets https://github.com/WebAssembly/funclets
- deleted 7y ago
- barnyfried 7y agojava applet