Skip to content

Commit

Permalink
fast compute hash code of deep expression without conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Aug 6, 2024
1 parent 76c984a commit 68ee628
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ public Integer visit(Expression expr, Void context) {
if (expr.children().isEmpty()) {
return 0;
}
return collectCommonExpressionByDepth(expr.children().stream().map(child ->
child.accept(this, context)).reduce(Math::max).map(m -> m + 1).orElse(1), expr);
return collectCommonExpressionByDepth(
expr.children()
.stream()
.map(child -> child.accept(this, context))
.reduce(Math::max)
.map(m -> m + 1)
.orElse(1),
expr
);
}

private int collectCommonExpressionByDepth(int depth, Expression expr) {
Expand All @@ -53,7 +60,6 @@ private int collectCommonExpressionByDepth(int depth, Expression expr) {

public static Set<Expression> getExpressionsFromDepthMap(
int depth, Map<Integer, Set<Expression>> depthMap) {
depthMap.putIfAbsent(depth, new LinkedHashSet<>());
return depthMap.get(depth);
return depthMap.computeIfAbsent(depth, d -> new LinkedHashSet<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public abstract class Expression extends AbstractTreeNode<Expression> implements
private final boolean compareWidthAndDepth;
private final Supplier<Set<Slot>> inputSlots = Suppliers.memoize(
() -> collect(e -> e instanceof Slot && !(e instanceof ArrayItemSlot)));
private final int fastChildrenHashCode;

protected Expression(Expression... children) {
super(children);
Expand All @@ -80,12 +81,14 @@ protected Expression(Expression... children) {
this.depth = 1;
this.width = 1;
this.compareWidthAndDepth = supportCompareWidthAndDepth();
this.fastChildrenHashCode = 1;
break;
case 1:
Expression child = children[0];
this.depth = child.depth + 1;
this.width = child.width;
this.compareWidthAndDepth = child.compareWidthAndDepth && supportCompareWidthAndDepth();
this.fastChildrenHashCode = child.fastChildrenHashCode() + 1;
break;
case 2:
Expression left = children[0];
Expand All @@ -94,21 +97,25 @@ protected Expression(Expression... children) {
this.width = left.width + right.width;
this.compareWidthAndDepth =
left.compareWidthAndDepth && right.compareWidthAndDepth && supportCompareWidthAndDepth();
this.fastChildrenHashCode = left.fastChildrenHashCode() + right.fastChildrenHashCode() + 2;
break;
default:
int maxChildDepth = 0;
int sumChildWidth = 0;
boolean compareWidthAndDepth = true;
int fastChildrenHashCode = 0;
for (Expression expression : children) {
child = expression;
maxChildDepth = Math.max(child.depth, maxChildDepth);
sumChildWidth += child.width;
hasUnbound |= child.hasUnbound;
compareWidthAndDepth &= child.compareWidthAndDepth;
fastChildrenHashCode = fastChildrenHashCode + expression.fastChildrenHashCode() + 1;
}
this.depth = maxChildDepth + 1;
this.width = sumChildWidth;
this.compareWidthAndDepth = compareWidthAndDepth;
this.fastChildrenHashCode = fastChildrenHashCode;
}

checkLimit();
Expand All @@ -129,35 +136,41 @@ protected Expression(List<Expression> children, boolean inferred) {
this.depth = 1;
this.width = 1;
this.compareWidthAndDepth = supportCompareWidthAndDepth();
this.fastChildrenHashCode = 1;
break;
case 1:
Expression child = children.get(0);
this.depth = child.depth + 1;
this.width = child.width;
this.compareWidthAndDepth = child.compareWidthAndDepth && supportCompareWidthAndDepth();
this.fastChildrenHashCode = child.fastChildrenHashCode() + 1;
break;
case 2:
Expression left = children.get(0);
Expression right = children.get(1);
this.depth = Math.max(left.depth, right.depth) + 1;
this.depth = Math.max(left.depth, right.depth) + 2;
this.width = left.width + right.width;
this.compareWidthAndDepth =
left.compareWidthAndDepth && right.compareWidthAndDepth && supportCompareWidthAndDepth();
this.fastChildrenHashCode = left.fastChildrenHashCode() + right.fastChildrenHashCode();
break;
default:
int maxChildDepth = 0;
int sumChildWidth = 0;
boolean compareWidthAndDepth = true;
int fastChildrenhashCode = 1;
for (Expression expression : children) {
child = expression;
maxChildDepth = Math.max(child.depth, maxChildDepth);
sumChildWidth += child.width;
hasUnbound |= child.hasUnbound;
compareWidthAndDepth &= child.compareWidthAndDepth;
fastChildrenhashCode = fastChildrenhashCode + expression.fastChildrenHashCode() + 1;
}
this.depth = maxChildDepth + 1;
this.width = sumChildWidth;
this.compareWidthAndDepth = compareWidthAndDepth && supportCompareWidthAndDepth();
this.fastChildrenHashCode = fastChildrenhashCode;
}

checkLimit();
Expand Down Expand Up @@ -211,6 +224,10 @@ public TypeCheckResult checkInputDataTypes() {
return checkInputDataTypesInternal();
}

public int fastChildrenHashCode() {
return fastChildrenHashCode;
}

protected TypeCheckResult checkInputDataTypesInternal() {
return TypeCheckResult.SUCCESS;
}
Expand Down Expand Up @@ -406,8 +423,8 @@ public boolean equals(Object o) {
return false;
}
Expression that = (Expression) o;
if ((compareWidthAndDepth && (this.width != that.width || this.depth != that.depth))
|| arity() != that.arity() || !extraEquals(that)) {
if ((compareWidthAndDepth && (this.width != that.width || this.depth != that.depth
|| this.fastChildrenHashCode != that.fastChildrenHashCode)) || arity() != that.arity() || !extraEquals(that)) {
return false;
}
return equalsChildren(that);
Expand All @@ -430,7 +447,7 @@ protected boolean extraEquals(Expression that) {

@Override
public int hashCode() {
return getClass().hashCode();
return getClass().hashCode() + fastChildrenHashCode();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public int hashCode() {
return exprId.asInt();
}

@Override
public int fastChildrenHashCode() {
return exprId.asInt();
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitSlotReference(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ public int hashCode() {
return Objects.hashCode(getValue());
}

@Override
public int fastChildrenHashCode() {
return Objects.hashCode(getValue());
}

@Override
public String toString() {
return String.valueOf(getValue());
Expand Down

0 comments on commit 68ee628

Please sign in to comment.