提交 bf7f5876 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Reduce allocation of ExpressionVisitor instances

上级 edcd07a7
...@@ -61,6 +61,37 @@ public class ExpressionVisitor { ...@@ -61,6 +61,37 @@ public class ExpressionVisitor {
public static final ExpressionVisitor EVALUATABLE_VISITOR = public static final ExpressionVisitor EVALUATABLE_VISITOR =
new ExpressionVisitor(EVALUATABLE); new ExpressionVisitor(EVALUATABLE);
/**
* Count of cached INDEPENDENT and EVALUATABLE visitors with different query
* level.
*/
private static final int CACHED = 8;
/**
* INDEPENDENT listeners with query level 0, 1, ...
*/
private static final ExpressionVisitor[] INDEPENDENT_VISITORS;
/**
* EVALUATABLE listeners with query level 0, 1, ...
*/
private static final ExpressionVisitor[] EVALUATABLE_VISITORS;
static {
ExpressionVisitor[] a = new ExpressionVisitor[CACHED];
a[0] = INDEPENDENT_VISITOR;
for (int i = 1; i < CACHED; i++) {
a[i] = new ExpressionVisitor(INDEPENDENT, i);
}
INDEPENDENT_VISITORS = a;
a = new ExpressionVisitor[CACHED];
a[0] = EVALUATABLE_VISITOR;
for (int i = 1; i < CACHED; i++) {
a[i] = new ExpressionVisitor(EVALUATABLE, i);
}
EVALUATABLE_VISITORS = a;
}
/** /**
* Request to set the latest modification id (addDataModificationId). * Request to set the latest modification id (addDataModificationId).
*/ */
...@@ -148,6 +179,17 @@ public class ExpressionVisitor { ...@@ -148,6 +179,17 @@ public class ExpressionVisitor {
this.maxDataModificationId = null; this.maxDataModificationId = null;
} }
private ExpressionVisitor(int type, int queryLevel) {
this.type = type;
this.queryLevel = queryLevel;
this.dependencies = null;
this.columns1 = null;
this.columns2 = null;
this.table = null;
this.resolver = null;
this.maxDataModificationId = null;
}
/** /**
* Create a new visitor object to collect dependencies. * Create a new visitor object to collect dependencies.
* *
...@@ -245,11 +287,18 @@ public class ExpressionVisitor { ...@@ -245,11 +287,18 @@ public class ExpressionVisitor {
* Increment or decrement the query level. * Increment or decrement the query level.
* *
* @param offset 1 to increment, -1 to decrement * @param offset 1 to increment, -1 to decrement
* @return a clone of this expression visitor, with the changed query level * @return this visitor or its clone with the changed query level
*/ */
public ExpressionVisitor incrementQueryLevel(int offset) { public ExpressionVisitor incrementQueryLevel(int offset) {
return new ExpressionVisitor(type, queryLevel + offset, dependencies, if (type == INDEPENDENT) {
columns1, table, resolver, maxDataModificationId, columns2); offset += queryLevel;
return offset < CACHED ? INDEPENDENT_VISITORS[offset] : new ExpressionVisitor(INDEPENDENT, offset);
} else if (type == EVALUATABLE) {
offset += queryLevel;
return offset < CACHED ? EVALUATABLE_VISITORS[offset] : new ExpressionVisitor(EVALUATABLE, offset);
} else {
return this;
}
} }
/** /**
...@@ -287,6 +336,7 @@ public class ExpressionVisitor { ...@@ -287,6 +336,7 @@ public class ExpressionVisitor {
} }
int getQueryLevel() { int getQueryLevel() {
assert type == INDEPENDENT || type == EVALUATABLE;
return queryLevel; return queryLevel;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论