Peephole optimization to speed up basic visitation a tiny bit.

Review by: spoon (TBR)


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@3249 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/JModVisitor.java b/dev/core/src/com/google/gwt/dev/jjs/ast/JModVisitor.java
index 0468afb..28898a0 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/JModVisitor.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/JModVisitor.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
@@ -17,48 +17,19 @@
 
 import com.google.gwt.dev.jjs.InternalCompilerException;
 
-import java.util.ArrayList;
 import java.util.List;
 
 /**
  * A visitor for iterating through and modifying an AST.
  */
-@SuppressWarnings("unchecked")
 public class JModVisitor extends JVisitor {
 
-  private interface ContextFactory {
-    Context create();
-  }
-
-  private class ContextPool extends ArrayList {
-
-    private ContextFactory factory;
-    private int pos = 0;
-
-    public ContextPool(ContextFactory factory) {
-      this.factory = factory;
-    }
-
-    public void release(Context ctx) {
-      if (get(--pos) != ctx) {
-        throw new InternalCompilerException(
-            "Tried to release the wrong context");
-      }
-    }
-
-    public Context take() {
-      if (pos == size()) {
-        add(factory.create());
-      }
-      return (Context) get(pos++);
-    }
-  }
-
-  private class ListContext implements Context {
-    private int index;
-    private List list;
-    private boolean removed;
-    private boolean replaced;
+  private static class ListContext implements Context {
+    boolean didChange;
+    int index;
+    List<JNode> list;
+    boolean removed;
+    boolean replaced;
 
     public boolean canInsert() {
       return true;
@@ -88,19 +59,11 @@
 
     public void replaceMe(JNode node) {
       checkState();
-      checkReplacement((JNode) list.get(index), node);
+      checkReplacement(list.get(index), node);
       list.set(index, node);
       didChange = replaced = true;
     }
 
-    protected void traverse(List list) {
-      this.list = list;
-      for (index = 0; index < list.size(); ++index) {
-        removed = replaced = false;
-        doTraverse((JNode) list.get(index), this);
-      }
-    }
-
     private void checkRemoved() {
       if (removed) {
         throw new InternalCompilerException("Node was already removed");
@@ -115,9 +78,10 @@
     }
   }
 
-  private class NodeContext implements Context {
-    private JNode node;
-    private boolean replaced;
+  private static class NodeContext implements Context {
+    boolean didChange;
+    JNode node;
+    boolean replaced;
 
     public boolean canInsert() {
       return false;
@@ -147,13 +111,6 @@
       this.node = node;
       didChange = replaced = true;
     }
-
-    protected JNode traverse(JNode node) {
-      this.node = node;
-      replaced = false;
-      doTraverse(node, this);
-      return this.node;
-    }
   }
 
   protected static void checkReplacement(JNode origNode, JNode newNode) {
@@ -168,51 +125,50 @@
 
   protected boolean didChange = false;
 
-  private final ContextPool listContextPool = new ContextPool(
-      new ContextFactory() {
-        public Context create() {
-          return new ListContext();
-        }
-      });
+  public JNode accept(JNode node) {
+    NodeContext ctx = new NodeContext();
+    try {
+      ctx.node = node;
+      node.traverse(this, ctx);
+      didChange |= ctx.didChange;
+      return ctx.node;
+    } catch (Throwable e) {
+      throw translateException(node, e);
+    }
+  }
 
-  private final ContextPool nodeContextPool = new ContextPool(
-      new ContextFactory() {
-        public Context create() {
-          return new NodeContext();
-        }
-      });
+  @SuppressWarnings("unchecked")
+  public void accept(List<? extends JNode> list) {
+    NodeContext ctx = new NodeContext();
+    try {
+      for (int i = 0, c = list.size(); i < c; ++i) {
+        ctx.replaced = false;
+        (ctx.node = list.get(i)).traverse(this, ctx);
+        ((List) list).set(i, ctx.node);
+      }
+      didChange |= ctx.didChange;
+    } catch (Throwable e) {
+      throw translateException(ctx.node, e);
+    }
+  }
+
+  @SuppressWarnings("unchecked")
+  public void acceptWithInsertRemove(List<? extends JNode> list) {
+    ListContext ctx = new ListContext();
+    try {
+      ctx.list = (List) list;
+      for (ctx.index = 0; ctx.index < list.size(); ++ctx.index) {
+        ctx.removed = ctx.replaced = false;
+        list.get(ctx.index).traverse(this, ctx);
+      }
+      didChange |= ctx.didChange;
+    } catch (Throwable e) {
+      throw translateException(list.get(ctx.index), e);
+    }
+  }
 
   public boolean didChange() {
     return didChange;
   }
 
-  protected JNode doAccept(JNode node) {
-    NodeContext ctx = (NodeContext) nodeContextPool.take();
-    try {
-      return ctx.traverse(node);
-    } finally {
-      nodeContextPool.release(ctx);
-    }
-  }
-
-  protected void doAccept(List list) {
-    NodeContext ctx = (NodeContext) nodeContextPool.take();
-    try {
-      for (int i = 0, c = list.size(); i < c; ++i) {
-        list.set(i, ctx.traverse((JNode) list.get(i)));
-      }
-    } finally {
-      nodeContextPool.release(ctx);
-    }
-  }
-
-  protected void doAcceptWithInsertRemove(List list) {
-    ListContext ctx = (ListContext) listContextPool.take();
-    try {
-      ctx.traverse(list);
-    } finally {
-      listContextPool.release(ctx);
-    }
-  }
-
 }
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/JVisitor.java b/dev/core/src/com/google/gwt/dev/jjs/ast/JVisitor.java
index 7e61a47..4babd97 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/JVisitor.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/JVisitor.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
@@ -30,7 +30,7 @@
 /**
  * A visitor for iterating through an AST.
  */
-@SuppressWarnings({"unused", "unchecked"})
+@SuppressWarnings("unused")
 public class JVisitor {
 
   protected static final Context UNMODIFIABLE_CONTEXT = new Context() {
@@ -61,24 +61,48 @@
 
   };
 
-  public final JExpression accept(JExpression node) {
-    return (JExpression) doAccept(node);
+  protected static InternalCompilerException translateException(JNode node,
+      Throwable e) {
+    InternalCompilerException ice;
+    if (e instanceof InternalCompilerException) {
+      ice = (InternalCompilerException) e;
+    } else {
+      ice = new InternalCompilerException("Unexpected error during visit.", e);
+    }
+    ice.addNode(node);
+    return ice;
   }
 
-  public final JNode accept(JNode node) {
-    return doAccept(node);
+  public final JExpression accept(JExpression node) {
+    return (JExpression) accept((JNode) node);
+  }
+
+  public JNode accept(JNode node) {
+    try {
+      node.traverse(this, UNMODIFIABLE_CONTEXT);
+      return node;
+    } catch (Throwable e) {
+      throw translateException(node, e);
+    }
   }
 
   public final JStatement accept(JStatement node) {
-    return (JStatement) doAccept(node);
+    return (JStatement) accept((JNode) node);
   }
 
-  public final void accept(List list) {
-    doAccept(list);
+  public void accept(List<? extends JNode> list) {
+    int i = 0;
+    try {
+      for (int c = list.size(); i < c; ++i) {
+        list.get(i).traverse(this, UNMODIFIABLE_CONTEXT);
+      }
+    } catch (Throwable e) {
+      throw translateException(list.get(i), e);
+    }
   }
 
-  public final void acceptWithInsertRemove(List list) {
-    doAcceptWithInsertRemove(list);
+  public void acceptWithInsertRemove(List<? extends JNode> list) {
+    accept(list);
   }
 
   public boolean didChange() {
@@ -504,40 +528,4 @@
   public boolean visit(JWhileStatement x, Context ctx) {
     return true;
   }
-
-  protected JNode doAccept(JNode node) {
-    doTraverse(node, UNMODIFIABLE_CONTEXT);
-    return node;
-  }
-
-  protected void doAccept(List list) {
-    for (Object node : list) {
-      doTraverse((JNode) node, UNMODIFIABLE_CONTEXT);
-    }
-  }
-
-  protected void doAcceptWithInsertRemove(List list) {
-    for (Object node : list) {
-      doTraverse((JNode) node, UNMODIFIABLE_CONTEXT);
-    }
-  }
-
-  protected final void doTraverse(JNode node, Context ctx) {
-    try {
-      node.traverse(this, ctx);
-    } catch (Throwable e) {
-      throw translateException(node, e);
-    }
-  }
-
-  private InternalCompilerException translateException(JNode node, Throwable e) {
-    InternalCompilerException ice;
-    if (e instanceof InternalCompilerException) {
-      ice = (InternalCompilerException) e;
-    } else {
-      ice = new InternalCompilerException("Unexpected error during visit.", e);
-    }
-    ice.addNode(node);
-    return ice;
-  }
 }