Asm.js support? #167

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

Comments

Projects
None yet
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

This comment has been minimized.

Show comment
Hide comment
@mishoo

mishoo Mar 28, 2013

Owner

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

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

This comment has been minimized.

Show comment
Hide comment
@timmutton

timmutton Jun 1, 2013

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

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

This comment has been minimized.

Show comment
Hide comment
@tracend

tracend 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.

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

This comment has been minimized.

Show comment
Hide comment
@timmutton

timmutton Jun 2, 2013

I've been playing around with the asm branch of lljs and that was the only
issue I had (tested by manually fixing it and it worked), although the
output only had one closure. I also had to turn off sequences, function
hoisting and variable joining but thats an easy enough fix

On Sun, Jun 2, 2013 at 9:43 AM, ✌ Makis Tracend notifications@github.comwrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/mishoo/UglifyJS2/issues/167#issuecomment-18799161
.

I've been playing around with the asm branch of lljs and that was the only
issue I had (tested by manually fixing it and it worked), although the
output only had one closure. I also had to turn off sequences, function
hoisting and variable joining but thats an easy enough fix

On Sun, Jun 2, 2013 at 9:43 AM, ✌ Makis Tracend notifications@github.comwrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/mishoo/UglifyJS2/issues/167#issuecomment-18799161
.

@tracend

This comment has been minimized.

Show comment
Hide comment
@tracend

tracend 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.

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

This comment has been minimized.

Show comment
Hide comment
@timmutton

timmutton Jun 2, 2013

Yeah, the dynamic aspect of javascript is really neat.

My understanding is that asm and lljs aren't meant to be a full replacement
for normal js, just a way to speed up arithmetic heavy parts (and the
results so far are impressive).

The fact that you can mix multiple languages/styles into one program is a
feature that's unique to javascript and probably one of its greater
strengths, I can certainly see it becoming common for a project to have
some files compiled by tsc, ljc and whatever the coffeescript compiler is
called, then merged together/optimised by a program like uglifyjs or
closure (although id prefer uglifyjs because its in js and is more
configurable)
On 02/06/2013 1:03 PM, "✌ Makis Tracend" notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/mishoo/UglifyJS2/issues/167#issuecomment-18801044
.

Yeah, the dynamic aspect of javascript is really neat.

My understanding is that asm and lljs aren't meant to be a full replacement
for normal js, just a way to speed up arithmetic heavy parts (and the
results so far are impressive).

The fact that you can mix multiple languages/styles into one program is a
feature that's unique to javascript and probably one of its greater
strengths, I can certainly see it becoming common for a project to have
some files compiled by tsc, ljc and whatever the coffeescript compiler is
called, then merged together/optimised by a program like uglifyjs or
closure (although id prefer uglifyjs because its in js and is more
configurable)
On 02/06/2013 1:03 PM, "✌ Makis Tracend" notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/mishoo/UglifyJS2/issues/167#issuecomment-18801044
.

@tracend

This comment has been minimized.

Show comment
Hide comment
@tracend

tracend 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.

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

This comment has been minimized.

Show comment
Hide comment
@timmutton

timmutton Jun 2, 2013

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

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

This comment has been minimized.

Show comment
Hide comment
@tracend

tracend 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

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

This comment has been minimized.

Show comment
Hide comment
@mishoo

mishoo Jun 2, 2013

Owner

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.

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

This comment has been minimized.

Show comment
Hide comment
@timmutton

timmutton Jun 2, 2013

@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 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

This comment has been minimized.

Show comment
Hide comment
@tracend

tracend Jun 2, 2013

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

tracend commented Jun 2, 2013

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

@mishoo

This comment has been minimized.

Show comment
Hide comment
@mishoo

mishoo Jun 7, 2013

Owner

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?

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

This comment has been minimized.

Show comment
Hide comment
@timmutton

timmutton Jun 8, 2013

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

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

This comment has been minimized.

Show comment
Hide comment
@fpirsch

fpirsch 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).

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

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra Jul 20, 2014

Contributor

@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.

Contributor

michaelficarra commented Jul 20, 2014

@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

This comment has been minimized.

Show comment
Hide comment
@nickdesaulniers

nickdesaulniers Oct 21, 2014

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?

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

This comment has been minimized.

Show comment
Hide comment
@fpirsch

fpirsch 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.

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

This comment has been minimized.

Show comment
Hide comment
@feross

feross 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?

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?

@feross feross referenced this issue in webtorrent/webtorrent Mar 17, 2015

Closed

Uglify compression causes "invalid asm.js warning" #281

@AgentME

This comment has been minimized.

Show comment
Hide comment
@AgentME

AgentME Aug 28, 2015

Contributor

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!)

Contributor

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