Skip to content

Commit

Permalink
Avoid creating unnecessary instances of CleanupActions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek authored and manovotn committed Sep 12, 2023
1 parent a7f6707 commit 3c0897c
Showing 1 changed file with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,31 @@ public InvokerBuilder<T> setInvocationWrapper(Class<?> clazz, String methodName)
return this;
}

private boolean requiresCleanup() {
boolean isStaticMethod = Modifier.isStatic(method.getModifiers());
if (instanceTransformer != null && !isStaticMethod) {
return true;
}
for (int i = 0; i < argTransformers.length; i++) {
if (argTransformers[i] != null) {
return true;
}
}
if (instanceLookup && !isStaticMethod) {
return true;
}
for (int i = 0; i < argLookup.length; i++) {
if (argLookup[i]) {
return true;
}
}
return false;
}

InvokerImpl<B, ?> doBuild() {
boolean isStaticMethod = Modifier.isStatic(method.getModifiers());
int instanceArguments = isStaticMethod ? 0 : 1;
boolean requiresCleanup = requiresCleanup();

MethodHandle mh = MethodHandleUtils.createMethodHandle(method);

Expand Down Expand Up @@ -171,7 +193,7 @@ public InvokerBuilder<T> setInvocationWrapper(Class<?> clazz, String methodName)
//
// inputs without transformations, or with transformations without cleanup, are left
// intact and application of the transformer only replaces the single argument
{
if (requiresCleanup) {
MethodType incomingType = MethodType.methodType(mh.type().returnType(), CleanupActions.class);
for (Class<?> paramType : mh.type().parameterArray()) {
if (paramType != CleanupActions.class) {
Expand Down Expand Up @@ -236,7 +258,7 @@ public InvokerBuilder<T> setInvocationWrapper(Class<?> clazz, String methodName)
//
// inputs without lookup are left intact and application of the transformer
// only replaces the single argument
{
if (requiresCleanup) {
int[] reordering = new int[mh.type().parameterCount()];
int paramCounter = 1;
for (int i = 0; i < reordering.length; i++) {
Expand All @@ -251,7 +273,7 @@ public InvokerBuilder<T> setInvocationWrapper(Class<?> clazz, String methodName)
}

// cleanup
{
if (requiresCleanup) {
MethodHandle cleanupMethod = mh.type().returnType() == void.class
? MethodHandleUtils.CLEANUP_FOR_VOID
: MethodHandleUtils.CLEANUP_FOR_NONVOID;
Expand All @@ -266,15 +288,15 @@ public InvokerBuilder<T> setInvocationWrapper(Class<?> clazz, String methodName)
}

// spread argument array into individual arguments
// keep leading arguments: `CleanupAction` if needed target instance if exists
int leadingArgumentsToKeep = (requiresCleanup ? 1 : 0) + (isStaticMethod ? 0 : 1);
if (isStaticMethod) {
// keep 1 leading argument, CleanupActions
MethodHandle invoker = MethodHandles.spreadInvoker(mh.type(), 1);
MethodHandle invoker = MethodHandles.spreadInvoker(mh.type(), leadingArgumentsToKeep);
invoker = MethodHandles.insertArguments(invoker, 0, mh);
invoker = MethodHandles.dropArguments(invoker, 1, Object.class);
invoker = MethodHandles.dropArguments(invoker, requiresCleanup ? 1 : 0, Object.class);
mh = invoker;
} else {
// keep 2 leading arguments, CleanupActions and the target instance
MethodHandle invoker = MethodHandles.spreadInvoker(mh.type(), 2);
MethodHandle invoker = MethodHandles.spreadInvoker(mh.type(), leadingArgumentsToKeep);
invoker = MethodHandles.insertArguments(invoker, 0, mh);
mh = invoker;
}
Expand All @@ -285,11 +307,11 @@ public InvokerBuilder<T> setInvocationWrapper(Class<?> clazz, String methodName)
if (PrimitiveUtils.hasPrimitive(parameterTypes)) {
MethodHandle replacePrimitiveNulls = MethodHandleUtils.REPLACE_PRIMITIVE_NULLS;
replacePrimitiveNulls = MethodHandles.insertArguments(replacePrimitiveNulls, 1, (Object) parameterTypes);
mh = MethodHandles.filterArguments(mh, 2, replacePrimitiveNulls);
mh = MethodHandles.filterArguments(mh, requiresCleanup ? 2 : 1, replacePrimitiveNulls);
}

// instantiate `CleanupActions`
{
if (requiresCleanup) {
mh = MethodHandles.foldArguments(mh, MethodHandleUtils.CLEANUP_ACTIONS_CTOR);
}

Expand Down

0 comments on commit 3c0897c

Please sign in to comment.