From 134225241ebec1f19b3718ca93ab805fd85ca146 Mon Sep 17 00:00:00 2001 From: James Petty Date: Thu, 16 May 2024 13:37:38 -0400 Subject: [PATCH 1/3] Allow reusing Scope temp variables --- src/main/java/io/airlift/bytecode/Scope.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/io/airlift/bytecode/Scope.java b/src/main/java/io/airlift/bytecode/Scope.java index 9b66e00..951eaf7 100644 --- a/src/main/java/io/airlift/bytecode/Scope.java +++ b/src/main/java/io/airlift/bytecode/Scope.java @@ -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; @@ -33,6 +36,7 @@ public class Scope private final Map variables = new TreeMap<>(); private final Map tempVariables = new TreeMap<>(); private final List allVariables = new ArrayList<>(); + private final Map> releasedTempVariables = new HashMap<>(); private final Variable thisVariable; @@ -73,6 +77,22 @@ public Variable createTempVariable(Class type) return variable; } + public Variable getOrCreateTempVariable(Class type) + { + Deque 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); From c760595c62276354caed9dae6da6d88059d0cf99 Mon Sep 17 00:00:00 2001 From: James Petty Date: Thu, 16 May 2024 14:33:19 -0400 Subject: [PATCH 2/3] Simplify ParameterizedType#equals --- src/main/java/io/airlift/bytecode/ParameterizedType.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/io/airlift/bytecode/ParameterizedType.java b/src/main/java/io/airlift/bytecode/ParameterizedType.java index ab3b912..36dbcde 100644 --- a/src/main/java/io/airlift/bytecode/ParameterizedType.java +++ b/src/main/java/io/airlift/bytecode/ParameterizedType.java @@ -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 From 263f2d54bfa23ac3b7569de94590136b7b874eb0 Mon Sep 17 00:00:00 2001 From: James Petty Date: Thu, 16 May 2024 14:39:22 -0400 Subject: [PATCH 3/3] Use ImmutableList.builderWithExpectedSize --- src/main/java/io/airlift/bytecode/ParameterizedType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/airlift/bytecode/ParameterizedType.java b/src/main/java/io/airlift/bytecode/ParameterizedType.java index 36dbcde..f3faacc 100644 --- a/src/main/java/io/airlift/bytecode/ParameterizedType.java +++ b/src/main/java/io/airlift/bytecode/ParameterizedType.java @@ -110,7 +110,7 @@ private ParameterizedType(Class type, Class... parameters) this.className = getPathName(type); this.simpleName = type.getSimpleName(); - ImmutableList.Builder builder = ImmutableList.builder(); + ImmutableList.Builder builder = ImmutableList.builderWithExpectedSize(parameters.length); for (Class parameter : parameters) { builder.add(toInternalIdentifier(parameter)); } @@ -128,7 +128,7 @@ private ParameterizedType(Class type, ParameterizedType... parameters) this.className = getPathName(type); this.simpleName = type.getSimpleName(); - ImmutableList.Builder builder = ImmutableList.builder(); + ImmutableList.Builder builder = ImmutableList.builderWithExpectedSize(parameters.length); for (ParameterizedType parameter : parameters) { builder.add(parameter.toString()); }