Skip to content

Commit

Permalink
Allow reusing Scope temp variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pettyjamesm committed May 16, 2024
1 parent 45bf10f commit 1342252
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/io/airlift/bytecode/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.objectweb.asm.Type;

import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -33,6 +36,7 @@ public class Scope
private final Map<String, Variable> variables = new TreeMap<>();
private final Map<String, Variable> tempVariables = new TreeMap<>();
private final List<Variable> allVariables = new ArrayList<>();
private final Map<ParameterizedType, Deque<Variable>> releasedTempVariables = new HashMap<>();

private final Variable thisVariable;

Expand Down Expand Up @@ -73,6 +77,22 @@ public Variable createTempVariable(Class<?> type)
return variable;
}

public Variable getOrCreateTempVariable(Class<?> type)
{
Deque<Variable> typeVariables = releasedTempVariables.get(type(type));
if (typeVariables == null || typeVariables.isEmpty()) {
return createTempVariable(type);
}
return typeVariables.pop();
}

public void releaseTempVariableForReuse(Variable tempVariable)
{
requireNonNull(tempVariable, "tempVariable is null");
checkArgument(tempVariable == tempVariables.get(tempVariable.getName()), "invalid tempVariable release: %s", tempVariable);
releasedTempVariables.computeIfAbsent(tempVariable.getType(), ignored -> new LinkedList<>()).push(tempVariable);
}

public Variable getTempVariable(String name)
{
Variable variable = tempVariables.get(name);
Expand Down

0 comments on commit 1342252

Please sign in to comment.