Avoid creating CallbackInfos where they are unused (#52)

* Skip passing CallbackInfos to handlers which don't use them

Allegedly can cause measurable differences in hot code

* Fix simple injectors

When only taking a CallbackInfo(Returnable) the slot is unrelated to the target
This commit is contained in:
Chocohead authored and GitHub committed 2021-06-28 17:54:30 +01:00
1 parent 38825f7bd9
commit ef14a841ac
1 file changed
+28 -5
@@ -163,6 +163,12 @@ public class CallbackInjector extends Injector {
*/
private boolean captureArgs = true;
/**
* Whether {@link #handler} uses the {@link CallbackInfo}/
* {@link CallbackInfoReturnable} it would normally be passed
*/
final boolean usesCallbackInfo;
Callback(MethodNode handler, Target target, final InjectionNode node, final LocalVariableNode[] locals, boolean captureLocals) {
this.handler = handler;
this.target = target;
@@ -206,6 +212,19 @@ public class CallbackInjector extends Injector {
if (this.canCaptureLocals) {
this.invoke.add(this.localTypes.length - this.frameSize);
}
//If the handler doesn't captureArgs, the CallbackInfo(Returnable) will be the first LVT slot, otherwise it will be at the target's frameSize
int callbackInfoSlot = handlerArgs.length == 1 ? Bytecode.isStatic(handler) ? 0 : 1 : frameSize;
boolean seenCallbackInfoUse = false;
for (AbstractInsnNode insn : handler.instructions) {
//Look for anywhere the CallbackInfo(Returnable) is loaded in the handler, it's unused if it is never loaded in
if (insn.getType() == AbstractInsnNode.VAR_INSN && insn.getOpcode() == Opcodes.ALOAD && ((VarInsnNode) insn).var == callbackInfoSlot) {
seenCallbackInfoUse = true;
break;
}
}
Injector.logger.debug("{} does{} use it's CallbackInfo{}", info, seenCallbackInfoUse ? "" : "n't", Type.VOID_TYPE == target.returnType ? "" : "Returnable");
usesCallbackInfo = seenCallbackInfoUse;
}
/**
@@ -520,12 +539,14 @@ public class CallbackInjector extends Injector {
}
}
this.dupReturnValue(callback);
if (this.cancellable || this.totalInjections > 1) {
this.createCallbackInfo(callback, true);
if (callback.usesCallbackInfo) {
this.dupReturnValue(callback);
if (this.cancellable || this.totalInjections > 1) {
this.createCallbackInfo(callback, true);
}
}
this.invokeCallback(callback, callbackMethod);
this.injectCancellationCode(callback);
if (callback.usesCallbackInfo) this.injectCancellationCode(callback);
callback.inject();
this.info.notifyInjected(callback.target);
@@ -635,7 +656,9 @@ public class CallbackInjector extends Injector {
* @param callback callback handle
*/
private void loadOrCreateCallbackInfo(final Callback callback) {
if (this.cancellable || this.totalInjections > 1) {
if (!callback.usesCallbackInfo) {
callback.add(new InsnNode(Opcodes.ACONST_NULL));
} else if (this.cancellable || this.totalInjections > 1) {
callback.add(new VarInsnNode(Opcodes.ALOAD, this.callbackInfoVar), false, true);
} else {
this.createCallbackInfo(callback, false);