Skip to content

Commit

Permalink
Add a cache for class proxies in BytecodeRecorderImpl
Browse files Browse the repository at this point in the history
We would generate class proxies for each class even if we already
generated one, which is counter productive.
Introduce a cache to avoid that and make things a bit more efficient.

(cherry picked from commit 4feaf0f)
  • Loading branch information
gsmet committed Jan 14, 2025
1 parent a8d4fdd commit ff3c414
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public class BytecodeRecorderImpl implements RecorderContext {
private final Map<Class<?>, NewRecorder> existingRecorderValues = new ConcurrentHashMap<>();
private final List<BytecodeInstruction> storedMethodCalls = new ArrayList<>();

private final IdentityHashMap<Class<?>, String> classProxies = new IdentityHashMap<>();
private final Map<String, String> classProxyNamesToOriginalClassNames = new HashMap<>();
private final Map<String, Class<?>> originalClassNamesToClassProxyClasses = new HashMap<>();
private final Map<Class<?>, SubstitutionHolder> substitutions = new HashMap<>();
private final Map<Class<?>, NonDefaultConstructorHolder> nonDefaultConstructors = new HashMap<>();
private final String className;
Expand Down Expand Up @@ -273,14 +274,20 @@ public Class<?> classProxy(String name) {
return void.class;
}

Class<?> proxyClass = originalClassNamesToClassProxyClasses.get(name);
if (proxyClass != null) {
return proxyClass;
}

ProxyFactory<Object> factory = new ProxyFactory<>(new ProxyConfiguration<Object>()
.setSuperClass(Object.class)
.setClassLoader(classLoader)
.setAnchorClass(getClass())
.setProxyNameSuffix("$$ClassProxy" + COUNT.incrementAndGet()));
Class theClass = factory.defineClass();
classProxies.put(theClass, name);
return theClass;
proxyClass = factory.defineClass();
classProxyNamesToOriginalClassNames.put(proxyClass.getName(), name);
originalClassNamesToClassProxyClasses.put(name, proxyClass);
return proxyClass;
}

@Override
Expand Down Expand Up @@ -743,14 +750,10 @@ ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle ar
method.load(param.toString()));
}
};
} else if (param instanceof Class<?>) {
if (!((Class) param).isPrimitive()) {
} else if (param instanceof Class<?> clazz) {
if (!clazz.isPrimitive()) {
// Only try to load the class by name if it is not a primitive class
String name = classProxies.get(param);
if (name == null) {
name = ((Class) param).getName();
}
String finalName = name;
String finalName = classProxyNamesToOriginalClassNames.getOrDefault(clazz.getName(), clazz.getName());
return new DeferredParameter() {
@Override
ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
Expand All @@ -770,7 +773,7 @@ ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle ar
return new DeferredParameter() {
@Override
ResultHandle doLoad(MethodContext context, MethodCreator method, ResultHandle array) {
return method.loadClassFromTCCL((Class) param);
return method.loadClassFromTCCL(clazz);
}
};
}
Expand Down

0 comments on commit ff3c414

Please sign in to comment.