Skip to content

Commit

Permalink
Use Cons to save memory building contexts (#88)
Browse files Browse the repository at this point in the history
Closes #55

In my local profiling this saves about 8% of memory on the huge snowflake query.
  • Loading branch information
crisptrutski authored Aug 22, 2024
1 parent 484ef9e commit d95f197
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions java/com/metabase/macaw/AstWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Borrows substantially from JSqlParser's TablesNamesFinder

import clojure.lang.Cons;
import clojure.lang.IFn;

import java.util.ArrayDeque;
Expand All @@ -10,6 +11,8 @@
import java.util.List;
import java.util.Map;

import clojure.lang.ISeq;
import clojure.lang.PersistentList;
import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
Expand Down Expand Up @@ -250,7 +253,7 @@ public String getValue() {

private Acc acc;
private final EnumMap<CallbackKey, IFn> callbacks;
private final Deque<Scope> contextStack;
private ISeq contextStack = PersistentList.EMPTY;
private long nextScopeId = 1;

/**
Expand All @@ -262,7 +265,6 @@ public String getValue() {
public AstWalker(Map<CallbackKey, IFn> rawCallbacks, Acc val) {
this.acc = val;
this.callbacks = new EnumMap<>(rawCallbacks);
this.contextStack = new ArrayDeque<>();
}

/**
Expand All @@ -273,21 +275,21 @@ public void invokeCallback(CallbackKey key, Object visitedItem) {
IFn callback = this.callbacks.get(key);
if (callback != null) {
//noinspection unchecked
this.acc = (Acc) callback.invoke(acc, visitedItem, this.contextStack.toArray());
this.acc = (Acc) callback.invoke(acc, visitedItem, this.contextStack);
}
}

private void pushContext(QueryScopeLabel label) {
this.contextStack.push(Scope.fromLabel(nextScopeId++, label));
this.contextStack = new Cons(Scope.fromLabel(nextScopeId++, label), this.contextStack);
}

private void pushContext(@SuppressWarnings("SameParameterValue") String type, String label) {
this.contextStack.push(Scope.other(nextScopeId++, type, label));
this.contextStack = new Cons(Scope.other(nextScopeId++, type, label), this.contextStack);
}

// This is pure sugar, but it's nice to be symmetrical with pushContext
private void popContext() {
this.contextStack.pop();
this.contextStack = this.contextStack.more();
}

/**
Expand Down

0 comments on commit d95f197

Please sign in to comment.