DeadCodeElimination does a lot of work to rewrite code with no runtime values into constants, saving output size and runtime performance. Given two constants, most binary operations (except assignment) can be rewritten, and with only one constant there are still cases where they can be rewritten depending on the constant (e.g. 0 * <expr>, 1 * , 0 + , true && `, etc).
Additionally, DeadCodeVisitor.tryOptimizeStringCall's purpose is to replace static calls on java.lang.String as well as instance methods called on string literals. Other methods are rewritten, such as .ordinal() calls on constant enums, and GWT.isScript(), always compiled to the boolean literal true (note that this probably instead should be handed by UnifyAst.maybeHandleMagicMethodCall).
There are other pure static and instance methods that could be optimized out here as well - most of java.lang.Math and StrictMath (except random()).
Integer.parseInt(String) and Long.parseLong(String) both delegate to Number.__parseAndValidateLong with a constant radix=10. If no other radix values are used in an application, eventually Character.digit is rewritten to this implementation:
function digit_0(c){
if (c >= 48 && c < 48 + $wnd.Math.min(10, 10)) {
return c - 48;
}
if (c >= 97 && c < 97) {
return c - 97 + 10;
}
if (c >= 65 && c < 65) {
return c - 65 + 10;
}
return -1;
}
It is also suspicious that the SUM operations are not combined - addition is associative over int, so regrouping (c - 65) + 10 into c - (65 - 10) should be done.
Boxed primitive types could perhaps be treated the same way for some marginal impact - Integer.hashCode will be inlined away, and Integer.max()/min() will be inlined to allow Math.max/min to be compiled out.
DeadCodeElimination does a lot of work to rewrite code with no runtime values into constants, saving output size and runtime performance. Given two constants, most binary operations (except assignment) can be rewritten, and with only one constant there are still cases where they can be rewritten depending on the constant (e.g.
0 * <expr>,1 *,0 +,true && `, etc).Additionally, DeadCodeVisitor.tryOptimizeStringCall's purpose is to replace static calls on java.lang.String as well as instance methods called on string literals. Other methods are rewritten, such as .ordinal() calls on constant enums, and
GWT.isScript(), always compiled to the boolean literaltrue(note that this probably instead should be handed by UnifyAst.maybeHandleMagicMethodCall).There are other pure static and instance methods that could be optimized out here as well - most of java.lang.Math and StrictMath (except
random()).Integer.parseInt(String) and Long.parseLong(String) both delegate to
Number.__parseAndValidateLongwith a constant radix=10. If no other radix values are used in an application, eventually Character.digit is rewritten to this implementation:It is also suspicious that the SUM operations are not combined - addition is associative over
int, so regrouping(c - 65) + 10intoc - (65 - 10)should be done.Boxed primitive types could perhaps be treated the same way for some marginal impact - Integer.hashCode will be inlined away, and Integer.max()/min() will be inlined to allow Math.max/min to be compiled out.