Author: cromwellian
Date: Thu Jun 25 16:41:29 2009
New Revision: 5633

Modified:
    wiki/AdvancedCompilerOptimizations.wiki

Log:
Added hypothetical "global object interning/inlining" optimization for enums

Modified: wiki/AdvancedCompilerOptimizations.wiki
==============================================================================
--- wiki/AdvancedCompilerOptimizations.wiki     (original)
+++ wiki/AdvancedCompilerOptimizations.wiki     Thu Jun 25 16:41:29 2009
@@ -484,6 +484,44 @@

  The key point is that the usage of a type in one context would not affect  
how the type itself could be optimized in other contexts. As it stands  
today, we can't do the above optimization if anywhere in the program  
someone calls `BasicList#add()`. A great practical example is Widgets  
firing events. You want widgets to be *able* to fire events *if* someone  
actually wants to listen to them. In the cases where you can see that there  
are no listeners to a widget, you'd really like the entirely of the  
even-firing infrastructur to completely disappear.

+=== Global Object Interning/Hoisted Inlining ===
+
+Inspired by Joel's attempts to optimize Java Enums, consider that we have  
a class `Foo`
+
+{{{
+public class Foo {
+  final int field1;
+  final String field2;
+
+  public Foo(int x, String y) { this.field1=x; this.field2=y; }
+  public int getField1() { return field1; }
+  public String getField2() { return field2; }
+}
+}}}
+
+and this class is provably only ever instantiated with compile time  
literal constants and contains no ability to mutate these fields.
+
+{{{
+new Foo(42, "Adams");
+new Foo(99, "Agent");
+new Foo(5, "Johnny");
+new Foo(10100, "Google");
+}}}
+
+Then the compiler may globally intern/hoist these field constants:
+{{{
+public class FooHoist {
+  public static int[] field1= {42, 99, 5, 10100};
+  public static String[] field2 = {"Adams", "Agent", "Johnny", "Google"};
+}
+}}}
+
+And any reference to `Foo` may now be replaced with an ordinal index into  
`FooHoist`. e.g. `new Foo(5, "Johnny");` becomes  `2`.
+
+This would allow the entire classtype for `Foo` to be pruned in many  
circumstances, the chief example being Enum classes. In the case where  
`Foo` is polymorphic, you need a separate dispatch table, but you can still  
eliminate the type and it's subtypes.
+
+This optimization is mostly useful for Enums and Type-Safe enums.
+
  == Semantic Techniques ==

  Let us call a method semantic, if it is recognized by the compiler by  
name, and the compiler has a heuristic for evaluating the same function,  
discarding the original implementation.  Semantic techniques are used by a  
number of compilers to replace operations with intrinsically faster ones.  
Consider the Java library function `Math.sin()`. While the library could  
theoretically have an implementation of sin() using table lookup or power  
series expansion, in some architectures, there is a native CPU instruction  
for computing `sin`, and therefore, the JIT compiler can in many cases,  
detect the call to Math.sin(), and replace it with an inlined instruction.  
Let us call this technique _semantic inlining_.

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to