Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reusing Scope temp variables #18

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/main/java/io/airlift/bytecode/ParameterizedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private ParameterizedType(Class<?> type, Class<?>... parameters)
this.className = getPathName(type);
this.simpleName = type.getSimpleName();

ImmutableList.Builder<String> builder = ImmutableList.builder();
ImmutableList.Builder<String> builder = ImmutableList.builderWithExpectedSize(parameters.length);
for (Class<?> parameter : parameters) {
builder.add(toInternalIdentifier(parameter));
}
Expand All @@ -128,7 +128,7 @@ private ParameterizedType(Class<?> type, ParameterizedType... parameters)
this.className = getPathName(type);
this.simpleName = type.getSimpleName();

ImmutableList.Builder<String> builder = ImmutableList.builder();
ImmutableList.Builder<String> builder = ImmutableList.builderWithExpectedSize(parameters.length);
for (ParameterizedType parameter : parameters) {
builder.add(parameter.toString());
}
Expand Down Expand Up @@ -220,12 +220,7 @@ public boolean equals(Object o)
}

ParameterizedType that = (ParameterizedType) o;

if (!type.equals(that.type)) {
return false;
}

return true;
return type.equals(that.type);
}

@Override
Expand Down
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
Loading