Skip to content

Commit

Permalink
perf: fold ConstantNode into calling methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Nov 19, 2024
1 parent a490047 commit f2bbfed
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ public AstNode transform(AstTransformer transformer) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newSingleMethod(this.left);
String rightMethod = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newSingleMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethod = context.newSingleMethod(this.right);

context.callDelegateSingle(m, leftMethod);
context.callDelegateSingle(m, rightMethod);
}

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newMultiMethod(this.left);
String rightMethod = context.newMultiMethod(this.right);
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));

Expand All @@ -98,14 +98,7 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m
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);
m.load(0, InstructionAdapter.OBJECT_TYPE);
m.load(res1, InstructionAdapter.OBJECT_TYPE);
m.load(2, InstructionAdapter.OBJECT_TYPE);
m.load(3, InstructionAdapter.OBJECT_TYPE);
m.load(4, InstructionAdapter.OBJECT_TYPE);
m.load(5, InstructionAdapter.OBJECT_TYPE);
m.load(6, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(context.className, rightMethod, BytecodeGen.Context.MULTI_DESC, false);
context.callDelegateMulti(m, rightMethod, res1);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void evalMulti(double[] res, int[] x, int[] y, int[] z, EvalType type) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newSingleMethod(this.left);
String rightMethod = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newSingleMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethod = context.newSingleMethod(this.right);

Label minLabel = new Label();

Expand All @@ -62,8 +62,8 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newMultiMethod(this.left);
String rightMethodSingle = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newMultiMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethodSingle = context.newSingleMethod(this.right);
context.callDelegateMulti(m, leftMethod);

context.doCountedLoop(m, localVarConsumer, idx -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void evalMulti(double[] res, int[] x, int[] y, int[] z, EvalType type) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newSingleMethod(this.left);
String rightMethod = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newSingleMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethod = context.newSingleMethod(this.right);

Label minLabel = new Label();

Expand All @@ -62,8 +62,8 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newMultiMethod(this.left);
String rightMethodSingle = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newMultiMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethodSingle = context.newSingleMethod(this.right);
context.callDelegateMulti(m, leftMethod);

context.doCountedLoop(m, localVarConsumer, idx -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void evalMulti(double[] res, int[] x, int[] y, int[] z, EvalType type) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newSingleMethod(this.left);
String rightMethod = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newSingleMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethod = context.newSingleMethod(this.right);

Label notZero = new Label();

Expand All @@ -55,8 +55,8 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String leftMethod = context.newMultiMethod(this.left);
String rightMethodSingle = context.newSingleMethod(this.right);
BytecodeGen.Context.ValuesMethodDefD leftMethod = context.newMultiMethod(this.left);
BytecodeGen.Context.ValuesMethodDefD rightMethodSingle = context.newSingleMethod(this.right);
context.callDelegateMulti(m, leftMethod);

context.doCountedLoop(m, localVarConsumer, idx -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public AstNode transform(AstTransformer transformer) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String delegateMethod = context.newSingleMethod(this.delegate);
BytecodeGen.Context.ValuesMethodDefD delegateMethod = context.newSingleMethod(this.delegate);
String cacheLikeField = context.newField(IFastCacheLike.class, this.cacheLike);
genPostprocessingMethod(context, cacheLikeField);

Expand Down Expand Up @@ -123,7 +123,7 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String delegateMethod = context.newMultiMethod(this.delegate);
BytecodeGen.Context.ValuesMethodDefD delegateMethod = context.newMultiMethod(this.delegate);
String cacheLikeField = context.newField(IFastCacheLike.class, this.cacheLike);

genPostprocessingMethod(context, cacheLikeField);
Expand Down Expand Up @@ -164,8 +164,8 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m

private void genPostprocessingMethod(BytecodeGen.Context context, String cacheLikeField) {
String methodName = String.format("postProcessing_%s", cacheLikeField);
String delegateSingle = context.newSingleMethod(this.delegate);
String delegateMulti = context.newMultiMethod(this.delegate);
String delegateSingle = context.newSingleMethodUnoptimized(this.delegate);
String delegateMulti = context.newMultiMethodUnoptimized(this.delegate);
context.genPostprocessingMethod(methodName, m -> {
Label cacheExists = new Label();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ public AstNode transform(AstTransformer transformer) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String inputMethod = context.newSingleMethod(this.input);
String whenInRangeMethod = context.newSingleMethod(this.whenInRange);
String whenOutOfRangeMethod = context.newSingleMethod(this.whenOutOfRange);
BytecodeGen.Context.ValuesMethodDefD inputMethod = context.newSingleMethod(this.input);
BytecodeGen.Context.ValuesMethodDefD whenInRangeMethod = context.newSingleMethod(this.whenInRange);
BytecodeGen.Context.ValuesMethodDefD whenOutOfRangeMethod = context.newSingleMethod(this.whenOutOfRange);

int inputValue = localVarConsumer.createLocalVariable("inputValue", Type.DOUBLE_TYPE.getDescriptor());
context.callDelegateSingle(m, inputMethod);
Expand Down Expand Up @@ -241,10 +241,10 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String inputSingle = context.newSingleMethod(this.input);
String whenInRangeSingle = context.newSingleMethod(this.whenInRange);
String whenOutOfRangeSingle = context.newSingleMethod(this.whenOutOfRange);
String inputMulti = context.newMultiMethod(this.input);
BytecodeGen.Context.ValuesMethodDefD inputSingle = context.newSingleMethod(this.input);
BytecodeGen.Context.ValuesMethodDefD whenInRangeSingle = context.newSingleMethod(this.whenInRange);
BytecodeGen.Context.ValuesMethodDefD whenOutOfRangeSingle = context.newSingleMethod(this.whenOutOfRange);
BytecodeGen.Context.ValuesMethodDefD inputMulti = context.newMultiMethod(this.input);
// String whenInRangeMulti = context.newMultiMethod(this.whenInRange);
// String whenOutOfRangeMulti = context.newMultiMethod(this.whenOutOfRange);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public AstNode transform(AstTransformer transformer) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String nextMethod = context.newSingleMethod(this.next);
BytecodeGen.Context.ValuesMethodDefD nextMethod = context.newSingleMethod(this.next);
context.callDelegateSingle(m, nextMethod);
m.areturn(Type.DOUBLE_TYPE);
}

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String nextMethod = context.newMultiMethod(this.next);
BytecodeGen.Context.ValuesMethodDefD nextMethod = context.newMultiMethod(this.next);
context.callDelegateMulti(m, nextMethod);
m.areturn(Type.VOID_TYPE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public AstNode transform(AstTransformer transformer) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String inputMethod = context.newSingleMethod(this.input);
BytecodeGen.Context.ValuesMethodDefD inputMethod = context.newSingleMethod(this.input);
String noiseField = context.newField(DensityFunction.Noise.class, this.noise);
int scale = localVarConsumer.createLocalVariable("scale", Type.DOUBLE_TYPE.getDescriptor());

Expand Down Expand Up @@ -120,7 +120,7 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String inputMethod = context.newMultiMethod(this.input);
BytecodeGen.Context.ValuesMethodDefD inputMethod = context.newMultiMethod(this.input);
String noiseField = context.newField(DensityFunction.Noise.class, this.noise);

context.callDelegateMulti(m, inputMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public AstNode transform(AstTransformer transformer) {
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String noiseField = context.newField(Noise.class, this.noise);

String shiftXMethod = context.newSingleMethod(this.shiftX);
String shiftYMethod = context.newSingleMethod(this.shiftY);
String shiftZMethod = context.newSingleMethod(this.shiftZ);
BytecodeGen.Context.ValuesMethodDefD shiftXMethod = context.newSingleMethod(this.shiftX);
BytecodeGen.Context.ValuesMethodDefD shiftYMethod = context.newSingleMethod(this.shiftY);
BytecodeGen.Context.ValuesMethodDefD shiftZMethod = context.newSingleMethod(this.shiftZ);

m.load(0, InstructionAdapter.OBJECT_TYPE);
m.getfield(context.className, noiseField, Type.getDescriptor(Noise.class));
Expand Down Expand Up @@ -117,9 +117,9 @@ public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
String noiseField = context.newField(Noise.class, this.noise);

String shiftXMethod = context.newMultiMethod(this.shiftX);
String shiftYMethod = context.newMultiMethod(this.shiftY);
String shiftZMethod = context.newMultiMethod(this.shiftZ);
BytecodeGen.Context.ValuesMethodDefD shiftXMethod = context.newMultiMethod(this.shiftX);
BytecodeGen.Context.ValuesMethodDefD shiftYMethod = context.newMultiMethod(this.shiftY);
BytecodeGen.Context.ValuesMethodDefD shiftZMethod = context.newMultiMethod(this.shiftZ);

int res1 = localVarConsumer.createLocalVariable("res1", Type.getDescriptor(double[].class));
int res2 = localVarConsumer.createLocalVariable("res2", Type.getDescriptor(double[].class));
Expand All @@ -139,24 +139,8 @@ public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m
m.store(res2, InstructionAdapter.OBJECT_TYPE);

context.callDelegateMulti(m, shiftXMethod);

m.load(0, InstructionAdapter.OBJECT_TYPE);
m.load(res1, InstructionAdapter.OBJECT_TYPE);
m.load(2, InstructionAdapter.OBJECT_TYPE);
m.load(3, InstructionAdapter.OBJECT_TYPE);
m.load(4, InstructionAdapter.OBJECT_TYPE);
m.load(5, InstructionAdapter.OBJECT_TYPE);
m.load(6, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(context.className, shiftYMethod, BytecodeGen.Context.MULTI_DESC, false);

m.load(0, InstructionAdapter.OBJECT_TYPE);
m.load(res2, InstructionAdapter.OBJECT_TYPE);
m.load(2, InstructionAdapter.OBJECT_TYPE);
m.load(3, InstructionAdapter.OBJECT_TYPE);
m.load(4, InstructionAdapter.OBJECT_TYPE);
m.load(5, InstructionAdapter.OBJECT_TYPE);
m.load(6, InstructionAdapter.OBJECT_TYPE);
m.invokevirtual(context.className, shiftZMethod, BytecodeGen.Context.MULTI_DESC, false);
context.callDelegateMulti(m, shiftYMethod, res1);
context.callDelegateMulti(m, shiftZMethod, res2);

context.doCountedLoop(m, localVarConsumer, idx -> {
m.load(1, InstructionAdapter.OBJECT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ public AstNode transform(AstTransformer transformer) {

@Override
public void doBytecodeGenSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
ValuesMethodDef splineMethod = doBytecodeGenSpline(context, this.spline);
BytecodeGen.Context.ValuesMethodDefF splineMethod = doBytecodeGenSpline(context, this.spline);
callSplineSingle(context, m, splineMethod);
m.cast(Type.FLOAT_TYPE, Type.DOUBLE_TYPE);
m.areturn(Type.DOUBLE_TYPE);
}

private static ValuesMethodDef doBytecodeGenSpline(BytecodeGen.Context context, Spline<DensityFunctionTypes.Spline.SplinePos, DensityFunctionTypes.Spline.DensityFunctionWrapper> spline) {
private static BytecodeGen.Context.ValuesMethodDefF doBytecodeGenSpline(BytecodeGen.Context context, Spline<DensityFunctionTypes.Spline.SplinePos, DensityFunctionTypes.Spline.DensityFunctionWrapper> spline) {
{
String cachedSplineMethod = context.getCachedSplineMethod(spline);
if (cachedSplineMethod != null) {
return new ValuesMethodDef(false, cachedSplineMethod, 0.0F);
return new BytecodeGen.Context.ValuesMethodDefF(cachedSplineMethod);
}
}
if (spline instanceof Spline.FixedFloatFunction<DensityFunctionTypes.Spline.SplinePos, DensityFunctionTypes.Spline.DensityFunctionWrapper> spline1) {
return new ValuesMethodDef(true, null, spline1.value());
return new BytecodeGen.Context.ValuesMethodDefF(spline1.value());
}
String name = context.nextMethodName("Spline");
InstructionAdapter m = new InstructionAdapter(
Expand Down Expand Up @@ -99,9 +99,9 @@ private static ValuesMethodDef doBytecodeGenSpline(BytecodeGen.Context context,
};

if (spline instanceof Spline.Implementation<DensityFunctionTypes.Spline.SplinePos, DensityFunctionTypes.Spline.DensityFunctionWrapper> impl) {
ValuesMethodDef[] valuesMethods = impl.values().stream()
BytecodeGen.Context.ValuesMethodDefF[] valuesMethods = impl.values().stream()
.map(spline1 -> doBytecodeGenSpline(context, spline1))
.toArray(ValuesMethodDef[]::new);
.toArray(BytecodeGen.Context.ValuesMethodDefF[]::new);

String locations = context.newField(float[].class, impl.locations());
String derivatives = context.newField(float[].class, impl.derivatives());
Expand All @@ -111,7 +111,7 @@ private static ValuesMethodDef doBytecodeGenSpline(BytecodeGen.Context context,

int lastConst = impl.locations().length - 1;

String locationFunction = context.newSingleMethod(McToAst.toAst(impl.locationFunction().function().value()));
BytecodeGen.Context.ValuesMethodDefD locationFunction = context.newSingleMethod(McToAst.toAst(impl.locationFunction().function().value()));
context.callDelegateSingle(m, locationFunction);
m.cast(Type.DOUBLE_TYPE, Type.FLOAT_TYPE);
m.store(point, Type.FLOAT_TYPE);
Expand Down Expand Up @@ -395,10 +395,10 @@ private static ValuesMethodDef doBytecodeGenSpline(BytecodeGen.Context context,

context.cacheSplineMethod(spline, name);

return new ValuesMethodDef(false, name, 0.0F);
return new BytecodeGen.Context.ValuesMethodDefF(name);
}

private static void callSplineSingle(BytecodeGen.Context context, InstructionAdapter m, ValuesMethodDef target) {
private static void callSplineSingle(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.ValuesMethodDefF target) {
if (target.isConst()) {
m.fconst(target.constValue());
} else {
Expand All @@ -411,9 +411,6 @@ private static void callSplineSingle(BytecodeGen.Context context, InstructionAda
}
}

private record ValuesMethodDef(boolean isConst, String generatedMethod, float constValue) {
}

@Override
public void doBytecodeGenMulti(BytecodeGen.Context context, InstructionAdapter m, BytecodeGen.Context.LocalVarConsumer localVarConsumer) {
context.delegateAllToSingle(m, localVarConsumer, this);
Expand Down
Loading

0 comments on commit f2bbfed

Please sign in to comment.