

What does “asm.js optimizations” mean? - ndesaulniers
https://blog.mozilla.org/luke/2015/02/18/microsoft-announces-asm-js-optimizations/

======
jvilk
Are there any tutorials at all on how to use ASM.js's Foreign Function
Interface (FFI)? The specification lacks examples, and seems to be very
underspecified. :(

Note that the FFI is a _crucial_ component of ASM.js, as everything in the
browser is external to ASM.js (DOM/any strings/any objects/etc). It seems
weird that there's so much hype around ASM.js, yet there's very little written
about the FFI or how to use it. Any project that uses ASM.js is going to need
to interact with it.

Also, are there any projects besides Emscripten that use ASM.js, or is the
push to put ASM.js into these browsers due purely to Emscripten?

~~~
azakai
There is almost nothing to the FFI interface: Basically, the asm FFI can only
have asm types, as you would expect. Then you can just call normal JS methods
that were imported. So stuff like

    
    
        function asmFunc(import) {
          'use asm';
    
          var externFunc = import.externFunc;
    
          function asmFunc() {
            externFunc(123, -0.5);
          }
    
          return asmFunc;
        }
    

What extra info are you looking for?

~~~
jvilk
> What extra info are you looking for?

Essentially, a complete executable example of a simple ASM.js program that
involves a foreign function, including processing return values. (I imagine if
it's this simple, then return values are handled using the same annotations as
ASM.js functions?)

While I understand that specs don't necessarily _have_ to have examples, I
believe it would benefit the community to have one in the ASM.js spec.

Thanks for clarifying here, though! I now understand.

~~~
azakai
Yeah, ffis are just calls like any other, with the usual annotations and types
on parameters and return values, etc. They just happen to be to functions on
the outside of the asm module.

edit: btw, there is a full example here
[https://github.com/dherman/asm.js/#example](https://github.com/dherman/asm.js/#example)

~~~
jvilk
Ah, I wasn't aware there was an actual repo associated with ASM.js. The
example doesn't show how you call into the module with `consoleDotLog`
defined, though. I know that sounds like a triviality, but complete examples
are really useful to cut through any possible ambiguity!

I would definitely lobby to get something smaller and concise into the spec
under the "Putting it All Together" heading! You could modify the existing
example in the spec to include an external function definition and call.

------
CmonDev
I hope this means that Microsoft has given up on pushing JavaScript to
developers and we will get a proper C#/F#+XAML to whatever-legacy-highly-
compatible-language (HTML, CSS, JS) toolchain.

------
CJefferson
I am getting increasingly annoyed by the limitations of asm.js, in particular
the "allocate all the memory you'll ever need up front", which requires
compiling in your own malloc, and makes it hard not to massively waste memory
by overallocating a huge buffer.

Therefore I greatly prefer optimisations which speed up 'asm.js-like code',
rather than the all-or-nothing optimisation one gets from firefox.

~~~
andhow
asm.js now allows the heap to be resized (by replacing the heap's ArrayBuffer
with a newer, bigger ArrayBuffer that was either copied or produced via
ES7-proposed ArrayBuffer.transfer [1]). Heap resizing currently has to be
enabled in Emscripten (by passing -s ALLOW_MEMORY_GROWTH=1), but may become
the default in the future. The main asm.js spec page hasn't been updated yet,
but the extension was discussed publicly with comments from Microsoft
engineers [2].

[1] [https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Refe...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transfer) [2]
[http://discourse.specifiction.org/t/request-for-comments-
swi...](http://discourse.specifiction.org/t/request-for-comments-switching-
resizing-heaps-in-asm-js/641/18)

~~~
CJefferson
That is nice to hear. I don't keep enough track to see that this was being
discussed, but it does deal with the only major issue I had with C++ -> asm.js
code conversion, particularly for mobile.

