Permalink
Browse files

Optim: iterating array lists with c-for is cheaper memory-wise than v…

…ia iterator
  • Loading branch information...
1 parent f38083f commit 1381bbb05d974618710c61030dab90ae52cf2ace @FroMage FroMage committed Dec 4, 2013
@@ -2,6 +2,7 @@
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
/**
@@ -40,7 +41,9 @@ void setDeclaration(Declaration type) {
//of defaulted type arguments
if (declaration instanceof Generic) {
Map<TypeParameter, ProducedType> result = typeArguments;
- for (TypeParameter pt: ((Generic) declaration).getTypeParameters()) {
+ List<TypeParameter> typeParameters = ((Generic) declaration).getTypeParameters();
+ for (int i=0,l=typeParameters.size();i<l;i++) {
+ TypeParameter pt = typeParameters.get(i);
ProducedType dta = pt.getDefaultTypeArgument();
if (dta!=null) {
if (!typeArguments.containsKey(pt)) {
@@ -1,5 +1,7 @@
package com.redhat.ceylon.compiler.typechecker.tree;
+import java.util.List;
+
import org.antlr.runtime.Token;
public class CustomTree extends Tree {
@@ -13,8 +15,9 @@ public FunctionArgument(Token token) {
public void visitChildren(Visitor visitor) {
if (getType()!=null)
getType().visit(visitor);
- for (ParameterList pl: getParameterLists())
- pl.visit(visitor);
+ List<ParameterList> parameterLists = getParameterLists();
+ for (int i=0,l=parameterLists.size();i<l;i++)
+ parameterLists.get(i).visit(visitor);
if (getExpression()!=null)
getExpression().visit(visitor);
if (getBlock()!=null)
@@ -81,8 +84,9 @@ public void visitChildren(Visitor visitor) {
}
else {
Walker.walkTypedDeclaration(visitor, this);
- for (Tree.ParameterList subnode: getParameterLists())
- subnode.visit(visitor);
+ List<ParameterList> parameterLists = getParameterLists();
+ for (int i=0,l=parameterLists.size();i<l;i++)
+ parameterLists.get(i).visit(visitor);
}
}
@Override public String getNodeType() {
@@ -118,8 +122,9 @@ public void visitChildren(Visitor visitor) {
if (getTypeConstraintList()!=null)
getTypeConstraintList().visit(visitor);
Walker.walkTypedDeclaration(visitor, this);
- for (Tree.ParameterList subnode: getParameterLists())
- subnode.visit(visitor);
+ List<ParameterList> parameterLists = getParameterLists();
+ for (int i=0,l=parameterLists.size();i<l;i++)
+ parameterLists.get(i).visit(visitor);
// if (getSpecifierExpression() instanceof LazySpecifierExpression)
if (getSpecifierExpression()!=null)
getSpecifierExpression().visit(visitor);
@@ -148,8 +153,9 @@ public void visitChildren(Visitor visitor) {
getTypeConstraintList().visit(visitor);
if (getType()!=null)
getType().visit(visitor);
- for (Tree.ParameterList subnode: getParameterLists())
- subnode.visit(visitor);
+ List<ParameterList> parameterLists = getParameterLists();
+ for (int i=0,l=parameterLists.size();i<l;i++)
+ parameterLists.get(i).visit(visitor);
if (getBlock()!=null)
getBlock().visit(visitor);
}
@@ -150,7 +150,9 @@ private Token getFirstChildToken() {
//don't come with useful offset information
isMissingToken(this.token) ?
null : this.token;
- for (Node child: getChildren()) {
+ List<Node> children = getChildren();
+ for (int i=0;i<children.size();i++) {
+ Node child = children.get(i);
Token tok = child.getFirstChildToken();
if (tok!=null && (token==null ||
tok.getTokenIndex()<token.getTokenIndex())) {
View
@@ -11,6 +11,7 @@ grammar Walkergen;
nodeList :
{
println("package com.redhat.ceylon.compiler.typechecker.tree;\n");
+ println("import java.util.List;");
println("import static com.redhat.ceylon.compiler.typechecker.tree.Tree.*;");
println("import static com.redhat.ceylon.compiler.typechecker.tree.Tree.Package;\n");
println("public class Walker {\n");
@@ -42,11 +43,17 @@ subnode : n=NODE_NAME '?'? f=FIELD_NAME
{ println(" if (node.get" + className($n.text) + "()!=null)"); }
{ println(" node.get" + className($n.text) + "().visit(visitor);"); }
| mn=NODE_NAME '*'
- { println(" for (" + className($mn.text) + " subnode: node.get" + className($mn.text) +"s())"); }
+ { println(" List<"+className($mn.text)+"> "+className($mn.text)+"s = node.get" + className($mn.text) +"s();"); }
+ { println(" for (int i=0,l=" + className($mn.text) + "s.size();i<l;i++){"); }
+ { println(" "+className($mn.text)+" subnode = "+className($mn.text)+"s.get(i);"); }
{ println(" subnode.visit(visitor);"); }
+ { println(" }"); }
| mn=NODE_NAME '*' f=FIELD_NAME
- { println(" for (" + className($mn.text) + " subnode: node.get" + initialUpper($f.text) +"s())"); }
+ { println(" List<"+className($mn.text)+"> "+className($mn.text)+"s = node.get" + initialUpper($f.text) +"s();"); }
+ { println(" for (int i=0,l=" + className($mn.text) + "s.size();i<l;i++){"); }
+ { println(" "+className($mn.text)+" subnode = "+className($mn.text)+"s.get(i);"); }
{ println(" subnode.visit(visitor);"); }
+ { println(" }"); }
;
field : 'abstract'? (TYPE_NAME|'boolean') FIELD_NAME ';'

0 comments on commit 1381bbb

Please sign in to comment.