Skip to content

Commit

Permalink
perf: avoid redundant array creation in binary node
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Nov 24, 2024
1 parent d0b6d4c commit a6f2906
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,33 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newMultiMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethod = context.newMultiMethod(this.right);

int res1 = localVarConsumer.createLocalVariable("res1", Type.getDescriptor(double[].class));

m.load(6, InstructionAdapter.OBJECT_TYPE);
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.arraylength();
m.iconst(0);
m.invokevirtual(Type.getInternalName(ArrayCache.class), "getDoubleArray", Type.getMethodDescriptor(Type.getType(double[].class), Type.INT_TYPE, Type.BOOLEAN_TYPE), false);
m.store(res1, InstructionAdapter.OBJECT_TYPE);
context.callDelegateMulti(m, leftMethod);
context.callDelegateMulti(m, rightMethod, res1);

context.doCountedLoop(m, localVarConsumer, idx -> bytecodeGenMultiBody(m, idx, res1));

m.load(6, InstructionAdapter.OBJECT_TYPE);
m.load(res1, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(Type.getInternalName(ArrayCache.class), "recycle", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(double[].class)), false);
if (leftMethod.isConst()) {
context.callDelegateMulti(m, rightMethod);
context.doCountedLoop(m, localVarConsumer, idx -> bytecodeGenConstMultiBody(m, idx, leftMethod.constValue()));
} else {
int res1 = localVarConsumer.createLocalVariable("res1", Type.getDescriptor(double[].class));

m.load(6, InstructionAdapter.OBJECT_TYPE);
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.arraylength();
m.iconst(0);
m.invokevirtual(Type.getInternalName(ArrayCache.class), "getDoubleArray", Type.getMethodDescriptor(Type.getType(double[].class), Type.INT_TYPE, Type.BOOLEAN_TYPE), false);
m.store(res1, InstructionAdapter.OBJECT_TYPE);
context.callDelegateMulti(m, leftMethod);
context.callDelegateMulti(m, rightMethod, res1);

context.doCountedLoop(m, localVarConsumer, idx -> bytecodeGenMultiBody(m, idx, res1));

m.load(6, InstructionAdapter.OBJECT_TYPE);
m.load(res1, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(Type.getInternalName(ArrayCache.class), "recycle", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(double[].class)), false);
}

m.areturn(Type.VOID_TYPE);
}

protected abstract void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1);

protected abstract void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ protected void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1) {
m.add(Type.DOUBLE_TYPE);
m.astore(Type.DOUBLE_TYPE);
}

@Override
protected void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft) {
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.dconst(constLeft);
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.DOUBLE_TYPE);
m.add(Type.DOUBLE_TYPE);
m.astore(Type.DOUBLE_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ protected void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1) {
);
m.astore(Type.DOUBLE_TYPE);
}

@Override
protected void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft) {
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.dconst(constLeft);
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.DOUBLE_TYPE);
m.invokestatic(
Type.getInternalName(Math.class),
"max",
Type.getMethodDescriptor(Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE),
false
);
m.astore(Type.DOUBLE_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m
protected void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1) {
throw new UnsupportedOperationException();
}

@Override
protected void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ protected void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1) {
);
m.astore(Type.DOUBLE_TYPE);
}

@Override
protected void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft) {
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.dconst(constLeft);
m.load(1, InstructionAdapter.OBJECT_TYPE);
m.load(idx, Type.INT_TYPE);
m.aload(Type.DOUBLE_TYPE);
m.invokestatic(
Type.getInternalName(Math.class),
"min",
Type.getMethodDescriptor(Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE),
false
);
m.astore(Type.DOUBLE_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m
protected void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1) {
throw new UnsupportedOperationException();
}

@Override
protected void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,9 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m
protected void bytecodeGenMultiBody(InstructionAdapter m, int idx, int res1) {
throw new UnsupportedOperationException();
}

@Override
protected void bytecodeGenConstMultiBody(InstructionAdapter m, int idx, double constLeft) {
throw new UnsupportedOperationException();
}
}

0 comments on commit a6f2906

Please sign in to comment.