HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

switch statements case ordering #1190

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
switch case statements must be in order.

see:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// @formatting pretty_print
// ==/ClosureCompiler==

(function(){

var OP =
{
add:0,
sub:1,
mul:2,
div:3,
shl:4,
shr:5,
shrun:7,
and:6

};
var Asm = function(es,esi,IL,line)
{
var v0;

switch (IL[line])
{
case OP.add:
v0 = esi--;
es[esi] += es[v0];
break;

case OP.sub:
v0 = esi--;
es[esi] -= es[v0];
break;

case OP.mul:
v0 = esi--;
es[esi] *= es[v0];
break;

case OP.div:
v0 = es[esi--];
es[esi] /= v0;
break;

case OP.shl:
v0 = esi--;
es[esi] <<= es[v0];
break;

case OP.shr:
v0 = esi--;
es[esi] >>= es[v0];
break;

case OP.shrun:
v0 = esi--;
es[esi] >>>= es[v0];
break;

case OP.and:
v0 = esi--;
es[esi] &= es[v0];
break;

}
}
window['A'] = Asm;
})();

-----------------------------------------
Compiled version
-----------------------------------------

(function() {
  window.A = function(a, b, c, d) {
    switch(c[d]) {
      case 0:
        c = b--;
        a[b] += a[c];
        break;
      case 1:
        c = b--;
        a[b] -= a[c];
        break;
      case 2:
        c = b--;
        a[b] *= a[c];
        break;
      case 3:
        c = a[b--];
        a[b] /= c;
        break;
      case 4:
        c = b--;
        a[b] <<= a[c];
        break;
      case 5:
        c = b--;
        a[b] >>= a[c];
        break;
      case 7:
        c = b--;
        a[b] >>>= a[c];
        break;
      case 6:
        c = b--, a[b] &= a[c];
    }
  };
})();

I think case 7 must be after case 6. to  js engine creates branch table better

Original issue reported on code.google.com by beyaz1...@gmail.com on 9 Jan 2014 at 3:31

GoogleCodeExporter commented 9 years ago
We generally prioritize optimizations that decrease the compiled size, and 
leave the rest to the JS engines.

Out of curiosity, what's better about the branch table if 7 is after 6? I'd 
think that it doesn't matter.

Original comment by dim...@google.com on 9 Jan 2014 at 5:12

GoogleCodeExporter commented 9 years ago
because JS engine generates better branch table according to switch statements.

switch 1:

switch(a)
{
   case ,
   case ,
   case ,
   case 500:
   case 501:
   case ,
   case ,

}

second switch:

switch(a)
{
   case ,
   case ,
   case ,
   case 500:
   case 502:
   case ,
   case ,   
}
in switch 1 Js engines generates effective codes.
but in second switch JS/c#/java engines generates 2 switch statements or 
generates extra codes for handling case 501.
----------------------------------
another example:
switch(X)
{
   case 86:... break;
   case 24:... break;
   case 38:... break;
   case 91:... break;

}
do you think compiler generate switch statements ? answer  is no.
many compilers generates if statements for switch(X) 
--------------------------------------------
consequently, case index order is the most important in switch statements.

Original comment by beyaz1...@gmail.com on 10 Jan 2014 at 4:31

GoogleCodeExporter commented 9 years ago
if this is worthwhile, then v8 should just do it on its own. I see no good 
reason why this should live in closure-compiler over v8.

Original comment by Nicholas.J.Santos on 11 Jan 2014 at 11:08