4 ms·
There is a significant penalty when converting types from ASM.JS or WASM to Javascript. If you stick entirely within ASM then it is much faster but the cost of
by disordinary 9y ago
There is a significant penalty when converting types from ASM.JS or WASM to Javascript. If you stick entirely within ASM then it is much faster but the cost of interacting with JS means that it can work out being quite a lot slower.
Unfortunately you need to interop with JS at the moment in order to interact with the DOM, if you do things entirely in WebGL then ASM is a lot faster.
- trgv 9y agoRight now, one of the projects I'm working on calls a wasm function which returns a pointer. I then do a lookup in the wasm buffer at the address specified by the pointer and pull out a few numbers with a float64array constructor. Is that an expensive operation?
- kannanvijayan 9y agoIt's probably best to keep a Float64Array view into the ArrayBuffer around, and use that repeatedly instead of constructing a fresh one each time. The interpreters and baseline JITs will always construct the object because they can't inline and they don't do whole-method analysis.. and the heavyweight JITs _probably would_ inline the constructor, then notice that the constructed array does not escape, then scalarize the whole thing down to a single read... but hey, why work the optimizer so hard when you can just make life easy for it and do a single allocation up front, and it runs fast in all performance tiers, and performance isn't predicated on a particular optimization strategy being utilized.
- trgv 9y agoThanks for the advice, very helpful.
- jononor 9y agoYou should ask the profiler.
- geofft 9y agoDo you still have a penalty when e.g. manipulating a TypedArray from JS? (If so, this seems like a regression compared to unoptimized asm.js.)
- disordinary 9y agoI haven't looked into it too much, my only experiments were really around DOM manipulation which incurred a huge slowdown.
- geofft 9y agoSure, but you can't avoid that slowdown by compiling your TypeScript to wasm, can you? DOM is a platform API, and you only get to interact with it in the way the platform allows you to.
- disordinary 9y agoThat's right. So WASM is only a benefit if you're not doing anything with the DOM. I see the main use cases outside of games will be intensive computing tasks. For instance Glimmer/Ember could run it's VM on a different thread using WebWorkers and WASM, the same could go for React and it's virtual dom.
- kannanvijayan 9y agoNo, these have very well optimized fastpaths in all engines. The method I'd expect for high-throughput communication between js and wasm would be for the wasm code to build structures directly in memory and for JS to traverse those directly using the memory buffer (TypedArray, but really ArrayBuffer). TypedObjects in ES* will let you directly build lightweight JS objects that are just pointers into an ArrayBuffer. Edit: Updated to note the fact that I really don't know what the status of TypedObjects is currently..
- jamii 9y agoIt's hard to find out. Noone really seems to be talking about it. It is enabled in Firefox nightly, though I don't know if the API is stable yet: > var Point = new TypedObject.StructType({x:TypedObject.int32, y:TypedObject.int32}) undefined > new Point({x:1, y:2}) InlineTransparentTypedObject { x: 1, y: 2 }