News for hackers. Stuff that matters.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by JasonSage on Wednesday September 06, 2017 @07:59PM (#15188120)
I don't think so.

From what I understand, calling WebAssembly functions from JavaScript (and vice-versa) incur a runtime penalty that's way worse than calling JavaScript functions from JavaScript. As such, WebAssembly makes sense for entire applications that would be compiled—think game engines targeting WebGL, or a font rasterizer targeting canvas, etc.

Since this only supports a subset of TypeScript, you won't even be able to compile your dependencies to WebAssembly, meaning that literally any code you interact with that you did not write has this performance setback.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by trgv on Wednesday September 06, 2017 @09:32PM (#15188514)
> From what I understand, calling WebAssembly functions from JavaScript (and vice-versa) incur a runtime penalty that's way worse than calling JavaScript functions from JavaScript.

Can you provide a source for this? I've never heard it and, though it may be correct, this would make wasm much less useful to me.

I have heard that accessing the DOM from wasm will be quite expensive, but that's different from just calling a function.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by disordinary on Wednesday September 06, 2017 @10:10PM (#15188655)
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.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by geofft on Wednesday September 06, 2017 @10:28PM (#15188734)
Do 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.)

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by kannanvijayan on Wednesday September 06, 2017 @10:43PM (#15188794)
No, 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..

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by jamii on Thursday September 07, 2017 @04:05AM (#15189911)
It'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 }
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by JSChoi on Sunday September 10, 2017 @11:44AM (#15212685)
There is a Mozilla bug from last February [https://bugzilla.mozilla.org/show_bug.cgi?id=1336740] that proposes that MDN document that particular TypedObject API, since it was experimentally implemented by Firefox. Florian Scholz of Mozilla then commented:

> The TypedObject proposal predates ES2015 and the yearly release model ECMAScript is on now. That said, the Firefox experimental implementation is not based on current standards work. I'm not sure how useful it is to further advertise or to document it in this case.

Lars T. Hansen of Mozilla added:

> IMO it's premature to document TypedObjects. I think TypedObjects are coming back, but initially to support WebAssembly's interaction with JS, and I would expect the TypedObjects' form to be slightly different from what we have now, more suited to that purpose.

So we may look forward to an eventual revised TC39 proposal for TypedObject, one that explicitly considers interoperability with WebAssembly. But that will probably take a long time. Hopefully someone will write a strawman proposal/polyfill soon.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by jamii on Sunday September 10, 2017 @12:00PM (#15212753)
Thanks, that's really useful info :)
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by kannanvijayan on Thursday September 07, 2017 @08:26AM (#15191169)
Yeah it's weird. I remember hearing a lot about it a couple years back, and when SpiderMonkey implemented in Firefox it was a noticeable change, largely because we used to call one our hidden type structures "TypeObjects", and that was super confusing when there was a language construct called "TypedObjects", so we renamed our TypeObjects to "ObjectGroup".

Seemed like a really nice proposal for procedural construction of typed views into ArrayBuffers, and perplexing that talk of it seems to have died down...

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by disordinary on Wednesday September 06, 2017 @10:43PM (#15188791)
I haven't looked into it too much, my only experiments were really around DOM manipulation which incurred a huge slowdown.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by geofft on Wednesday September 06, 2017 @10:53PM (#15188838)
Sure, 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.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by disordinary on Wednesday September 06, 2017 @11:27PM (#15188959)
That'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.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by trgv on Wednesday September 06, 2017 @10:21PM (#15188698)
Right 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?
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by kannanvijayan on Wednesday September 06, 2017 @10:52PM (#15188835)
It'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.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by trgv on Wednesday September 06, 2017 @11:52PM (#15189030)
Thanks for the advice, very helpful.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by jononor on Thursday September 07, 2017 @07:41AM (#15190857)
You should ask the profiler.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by gsnedders on Wednesday September 06, 2017 @10:02PM (#15188629)
I've not been that involved with WASM (I've been more or less out of the JS game since around the time asm.js became a thing), but AFAIK the only thing notable is JS -> WASM is more expensive than WASM -> WASM (but should be no worse than JS -> JS); WASM -> JS should be the same as JS -> JS.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by Matthias247 on Thursday September 07, 2017 @07:58AM (#15190967)
One thing is pure call performance (time to invoke a void() function). I don't know where that is, but I guess for WASM<->JS it's slower than WASM<->WASM and JS<->JS, since it can't be inlined.

The more important thing however is the cost of parameter passing. Let's keep in mind that WASM only support numeric types and typed arrays. So in order to make a function call from JS to WASM or the other way around which passes a string as a parameter the string needs to be converted into a format that the other side understands (Javascript string object <-> WASM typed array which contains string in UTFx format). So basically for all more complex data types crossing the boundary costs the [de]serialization of the parameters, which may be huge (and even causes allocations).

If you also only work with integers and typed arrays on the JS side that does not apply. But I guess most people won't be comfortable with that.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by gsnedders on Thursday September 07, 2017 @10:48AM (#15192322)
> One thing is pure call performance (time to invoke a void() function). I don't know where that is, but I guess for WASM<->JS it's slower than WASM<->WASM and JS<->JS, since it can't be inlined.

…why can't it be inlined? There's no good reason why it can't be inlined.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by Matthias247 on Thursday September 07, 2017 @05:28PM (#15195913)
When I wrote about that I thought about static resolving of where inlining is possible, which would be hard on a boundary to a dynamic language.

But yeah, assuming both WASM and JS run inside the same JITting VM it is probably happening.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by forgotpw1123 on Wednesday September 06, 2017 @08:36PM (#15188287)
... Until the first person ports QT or GTK and suddenly WASM is just pushing 60 PNG frames a second into a canvas.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by aeleos on Wednesday September 06, 2017 @10:19PM (#15188688)
https://www.destroyallsoftware.com/talks/the-birth-and-death...

Your comment immediately made me think of this. Highly recommended talk for anyone that hasn't seen it. It goes through a "fictional" (maybe not so much anymore) history of javascript until 2035. We are getting pretty close to javascript all the way down.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by pjmlp on Thursday September 07, 2017 @01:15AM (#15189287)
I am betting WASM will be the revenge of plugins.

And then, just like the ad spams using JS, its advocates will be getting more than they asked for.

Get ready for WASM blockers.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by slackingoff2017 on Thursday September 07, 2017 @09:24AM (#15191562)
WASM is going to kill spidering. We can't leave HTML if we want google to be able to read the page.

I think Google is very afraid of this. Hence their push for Angular/Polymer/AMP. If they can build a good enough platform in JS they can stall the inevitable.

In the future only marketing parts of the site will use JS/HTML. The "web app" part will be some language compiled to WASM throwing frames to the canvas

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by smrq on Thursday September 07, 2017 @11:10AM (#15192537)
In a world where SEO consultants exist, you foresee an inevitable future where Google is killed by a massive wave of companies rewriting their websites to be un-Googleable?
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by slackingoff2017 on Thursday September 07, 2017 @01:55PM (#15194193)
The big players are increasingly doing it already. LinkedIn and Facebook only have very limited spidering.

What Google has liked to do recently is take all your data and replace your service. Search for recipes, jobs, music, or weather for examples.

They're trying to make leaving the homepage irrelevant by using sites data in their search results. Eventually the web will rebel.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by flukus on Thursday September 07, 2017 @02:03AM (#15189457)
But management won't pay to redevelop the whole site, so they'll just embed the content in a QtWebKit widget. We can still block ads at the network level.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by pjmlp on Thursday September 07, 2017 @03:00AM (#15189663)
Sure they will.

Instead of "why I moved from Angular/React/.... to Vue.js", it will be "why I ported into WASM".

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by tyingq on Wednesday September 06, 2017 @08:52PM (#15188369)
It may get there eventually, but currently, having to pass the JavaScript/wasm chasm for DOM manipulation is really slow. https://github.com/rust-webplatform/rust-webplatform/issues/...
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by paulddraper on Wednesday September 06, 2017 @09:09PM (#15188423)
But canvas is faster.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by orange8 on Wednesday September 06, 2017 @10:11PM (#15188657)
Canvas is part of the DOM. It is a DOM element that JS can draw graphics on.
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by abecedarius on Wednesday September 06, 2017 @11:57PM (#15189045)
Not a problem: you can ask the canvas for an arraybuffer to draw on, and pass that to the wasm code. I did this with asm.js in place of wasm (which didn't exist yet) in http://wry.me/hacking/Turing-Drawings/ and you can judge the speed yourself. (As that page describes, there was some overhead from calling from JS to asm.js at least when asm.js first came out. I don't know the current figures.)
Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by orange8 on Thursday September 07, 2017 @05:30AM (#15190222)
asm.js is valid Javascript. It is a valid subset of js that the browser js engines understand, even for the older ones like IE 8. JavaScript has direct access to the DOM, with webassembly, not yet. It may in the future, its still in the proposal phrase (https://github.com/WebAssembly/gc/blob/master/proposals/gc/O...)

So the way to do it today would be WebAssembly -> asm.js/js -> canvas

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by abecedarius on Thursday September 07, 2017 @02:52PM (#15194704)
Good point about the proposal -- that'll be the answer in the end.

asmj.js as JS is irrelevant to the point I was making, because if you access the DOM directly, your code won't verify as the asm.js subset, and so won't run via the asm.js engine.

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by orange8 on Saturday September 09, 2017 @02:50AM (#15205866)
The asm.js engine is the javascript engine.

asm.js is javascript.

asm.js === javscript

Re:AssemblyScript: A Subset of TypeScript That Compiles to W... by abecedarius on Saturday September 09, 2017 @11:36AM (#15207566)
No: that's a choice that Chrome made and Firefox didn't, for example.