Skip to content

Asm.js support? #167

Closed
tracend opened this Issue Mar 28, 2013 · 20 comments

8 participants

@tracend
tracend commented Mar 28, 2013

Can you foresee any big obstacles trying to minify while (optionally) applying optimizations based on the asm.js spec?

http://asmjs.org/spec/latest/

@mishoo
Owner
mishoo commented Mar 28, 2013

That's a subset of JS — I can't see what wouldn't work. Try it and please report if you encounter any problems.

@mishoo mishoo closed this Mar 28, 2013
@timmutton

Hey, I gave it a try, and it mostly works however the compressor appears to reorder the bitwise operations, specifically "| 0", which needs to be in a particular order to validate

@tracend
tracend commented Jun 1, 2013

That's great.

Creating the variable fallbacks seems a straightforward problem to solve. My main concern is in constructing the object matrix of all the closures.

@timmutton
@tracend
tracend commented Jun 2, 2013

I saw LLJS in your repos - really awesome stuff.

Although my aptitude is in dynamic programming I do believe that the bridge for static typing in Javascript, and thus before runtime compilation, can be automated with middleware.

@timmutton
@tracend
tracend commented Jun 2, 2013

I agree with your first comment - it makes more sense (for now) to use it in contained routines, like the scripts we pass onto web workers which have a specific io.

UglifyJS minification to asm.js would be my preference as well.

@timmutton

You said before that fixing up the reordering of bitwise statements wouldn't be too hard, would you be able to point me in the right direction for fixing it up? Id like to have a crack at fixing it up, as I need it to get my other projects working

@tracend
tracend commented Jun 2, 2013

Oh, I'm not aware of the specifics why the compressor might be reordering the bitwise operations but I'm sure someone from the core team of UglifyJS can answer that.

But I had fun working with bitwise operators recently on three-obj (to convert OBJ to binary JSON)
https://github.com/makesites/three-obj

For me the MDN page was quite helpful (if you need a basic reference):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

@mishoo
Owner
mishoo commented Jun 2, 2013

Oh, I'm not aware of the specifics why the compressor might be reordering the bitwise operations but I'm sure someone from the core team of UglifyJS can answer that.

If this is the only problem, I can fix it. Will do.

@timmutton

@tracend The bitwise operators are fine, its just asm requires them to be after the statement (eg a = b | 0 validates, but a = 0 | b doesnt)

@mishoo Thank you, that would be greatly appreciated

@tracend
tracend commented Jun 2, 2013

awesome, got it. a = 0 | b does look weird.

@mishoo
Owner
mishoo commented Jun 7, 2013

Sorry for the delay. Can you verify now and see if anything else is needed?

Note that statements like x = x | 0 will now end up x |= 0 — is asm mode happy with that, or should we disable this optimization too?

@timmutton

Thanks for the update. Unfortunately it has to be the former. After further testing, I've come across two other issues.

The easier of the two is that asm uses the format x = +y to show that y is a double and therefore so should x (it also does this with constants eg +1 but 1.0 also works, not sure why)

The other (much more complicated) issue is that when removing unused variables, if the rhs is left it breaks asm eg var a = new Float32Array(b) becomes new Float32Array(b) which unfortunately breaks

@fpirsch
fpirsch commented Jul 19, 2014

x=x|0 is still optimized to x|=0 and this prevents asm.js from working.
Other problems with asm.js:

  • var declarations at the beginning of the asm module are moved at the end.
  • variables with the same name as the asm.js module.
  • x=x|0,y=y|0; should be x=x|0;y=y|0;
  • The && and || like in for(init;condition1 && condition2;) are forbidden.

A lot of classical JS constructs are not allowed for asm.js to successfully compile.
Maybe the best thing would be to locate "use asm" functions and turn off any optimizations except white space removal and variable renaming (and not using the function's name).

@michaelficarra

@fpirsch: What you are looking for can be easily accomplished by composing esprima, esshorten, and escodegen. No need for the kind of restructuring that uglifyjs/esmangle do if that's all you want.

@nickdesaulniers

Indeed. I can run the mangler, but the compressor produces invalid asm.js (still valid js). It looks like I can drill down as the compressor has arguments. In the meantime, can this issue be reopened?

@fpirsch
fpirsch commented Oct 22, 2014

@michaelficarra You're right, but my use case is a file with both regular js and asm.js (https://github.com/fpirsch/twin-bcrypt/blob/master/src/twin-bcrypt.js).
I ended up with a custom build process where I extract the asm.js code, minify it myself, and reinject it in the uglified file. It works but it would be so much better if uglifyJS could cope with it.
Plus, my suggestion to not optimize functions beginning with "use asm" doesn't seem so much work.

@feross
feross commented Mar 17, 2015

Yeah, automatically disabling the compressor in code that contains a "use asm"; statement sounds like the right solution. It would make uglify do the right thing in the presence of mixed asm and non-asm code. Right now, uglify just breaks the code.

Can this issue be re-opened?

@AgentME
AgentME commented Aug 28, 2015

Any news on this? I'd like to be able to have 'use asm' functions within larger minified javascript bundles. I could choose to not use uglifyjs or certain options, but it's not much help if I release a library that contains 'use asm' functions. I'd have to include a disclaimer telling people to avoid using uglify on any code bundles including the library. (Actually, the disclaimer would have to be viral: any libraries which use my library would need to include the same disclaimer and so on!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.