Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size of generated PagesHashStrategy constructor #23720

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions core/trino-main/src/main/java/io/trino/sql/gen/JoinCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import io.airlift.bytecode.Parameter;
import io.airlift.bytecode.Scope;
import io.airlift.bytecode.Variable;
import io.airlift.bytecode.control.ForLoop;
import io.airlift.bytecode.control.IfStatement;
import io.airlift.bytecode.expression.BytecodeExpression;
import io.airlift.bytecode.expression.BytecodeExpressions;
import io.airlift.bytecode.instruction.LabelNode;
import io.airlift.slice.SizeOf;
import io.trino.Session;
import io.trino.annotation.UsedByGeneratedCode;
import io.trino.cache.CacheStatsMBean;
import io.trino.cache.NonEvictableLoadingCache;
import io.trino.operator.HashArraySizeSupplier;
Expand Down Expand Up @@ -274,53 +274,27 @@ private static void generateConstructor(
MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), channels, hashChannel);

Variable thisVariable = constructorDefinition.getThis();
Variable blockIndex = constructorDefinition.getScope().declareVariable(int.class, "blockIndex");

BytecodeBlock constructor = constructorDefinition
.getBody()
.comment("super();")
.append(thisVariable)
.invokeConstructor(Object.class);

constructor.comment("this.size = INSTANCE_SIZE")
.append(thisVariable.setField(sizeField, getStatic(instanceSizeField)));
constructor
.append(thisVariable)
.getVariable(channels)
.invokeStatic(JoinCompiler.class, "computeRetainedSize", long.class, List.class)
.append(getStatic(instanceSizeField))
.longAdd()
.putField(sizeField);

constructor.comment("Set channel fields");

for (int index = 0; index < channelFields.size(); index++) {
BytecodeExpression channel = channels.invoke("get", Object.class, constantInt(index))
.cast(type(ObjectArrayList.class, Block.class));

constructor.append(thisVariable.setField(channelFields.get(index), channel));

BytecodeBlock loopBody = new BytecodeBlock();

constructor.comment("for(blockIndex = 0; blockIndex < channel.size(); blockIndex++) { size += channel.get(i).getRetainedSizeInBytes() }")
.append(new ForLoop()
.initialize(blockIndex.set(constantInt(0)))
.condition(new BytecodeBlock()
.append(blockIndex)
.append(channel.invoke("size", int.class))
.invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class))
.update(new BytecodeBlock().incrementVariable(blockIndex, (byte) 1))
.body(loopBody));

loopBody.append(thisVariable)
.append(thisVariable)
.getField(sizeField)
.append(
channel.invoke("get", Object.class, blockIndex)
.cast(type(Block.class))
.invoke("getRetainedSizeInBytes", long.class))
.longAdd()
.putField(sizeField);

constructor.append(thisVariable)
.append(thisVariable)
.getField(sizeField)
.append(invokeStatic(SizeOf.class, "sizeOf", long.class, channel.invoke("elements", Object[].class)))
.longAdd()
.putField(sizeField);
}

constructor.comment("Set join channel fields");
Expand Down Expand Up @@ -1040,6 +1014,22 @@ private BytecodeNode typeEqualsIgnoreNulls(
leftBlock, leftBlockPosition, rightBlock, rightBlockPosition);
}

@UsedByGeneratedCode
public static long computeRetainedSize(List<List<Block>> channels)
{
long result = 0;

for (List<Block> channel : channels) {
ObjectArrayList<Block> blocks = (ObjectArrayList<Block>) channel;
result += SizeOf.sizeOf(blocks.elements());
for (Block block : channel) {
result += block.getRetainedSizeInBytes();
}
}

return result;
}

public static class LookupSourceSupplierFactory
{
private final Constructor<? extends LookupSourceSupplier> constructor;
Expand Down