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 generated code size for Row Constructors #21299

Merged

Conversation

Bhargavi-Sagi
Copy link
Contributor

@Bhargavi-Sagi Bhargavi-Sagi commented Mar 28, 2024

Description

Reduces generated code size for rows with large number of fields. Earlier PageProjectionWork.evaluate method code generation used to work for ~900 columns. After trino#17342, evaluate method runs into method too large issue for ~650 columns. Post this change RowConstructorCodeGenerator can generate byte code for ~850 columns.

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
(X ) Release notes are required, with the following suggested text:

# Section
* Increase the number of columns supported by MERGE queries before MethodTooLargeExceptions are encountered. ({issue}`issuenumber`)

@cla-bot cla-bot bot added the cla-signed label Mar 28, 2024
@github-actions github-actions bot added the iceberg Iceberg connector label Mar 28, 2024
@wendigo wendigo requested review from dain and martint March 28, 2024 05:29
@Bhargavi-Sagi Bhargavi-Sagi changed the title Rewrite RowConstructorCodeGenerator to reduce generated code size Reduce generated code size for Row Constructors Mar 29, 2024
@Bhargavi-Sagi Bhargavi-Sagi force-pushed the rowconstructor-codegen-fix branch 2 times, most recently from c7a8f2e to a15c5d1 Compare March 30, 2024 15:57
Copy link
Member

@pettyjamesm pettyjamesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM with small comments, @dain PTAL.

Suggested release notes: "Increase the number of columns supported by MERGE queries before MethodTooLargeExceptions are encountered"

@@ -61,6 +62,26 @@ private RowBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, BlockBu
this.fieldBlockBuildersList = List.of(fieldBlockBuilders);
}

public static BlockBuilder[] createFieldBlockBuildersForSingleRow(Type rowType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add an inline comment that this is called from generated code (the @UsedByGeneratedCode annotatation isn't in scope within trino-spi, otherwise we would want to use that). Otherwise people might assume this method is unused.

cc: @dain - is RowBlockBuilder the right place for this code? I don't know of a better one since this is field-builder specific behavior for row constructors, but maybe you do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we don't modify the SPI for this. Instead let's just put the code in RowConstructorCodeGenerator.

return createFieldBlockBuilders(rowType.getTypeParameters(), null, 1);
}

public static SqlRow createSqlRowFromFieldBuildersForSingleRow(BlockBuilder[] fieldBuilders)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion about an inline comment that this is called from generated code, and question to @dain about whether these methods have a better home somewhere else.

Copy link
Member

@pettyjamesm pettyjamesm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, @dain PTAL

Copy link
Member

@dain dain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments but overall looks good.

@@ -61,6 +62,26 @@ private RowBlockBuilder(@Nullable BlockBuilderStatus blockBuilderStatus, BlockBu
this.fieldBlockBuildersList = List.of(fieldBlockBuilders);
}

public static BlockBuilder[] createFieldBlockBuildersForSingleRow(Type rowType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we don't modify the SPI for this. Instead let's just put the code in RowConstructorCodeGenerator.


private BytecodeNode generateExpressionForLargeRows(BytecodeGeneratorContext context)
{
BytecodeBlock block = new BytecodeBlock().setDescription("Constructor for " + rowType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment on how this is different from the above code. Specifically touch on where we get less byte code and why the performance is not as good as above (otherwise we would use the same code for both).

@@ -42,6 +46,7 @@ public class RowConstructorCodeGenerator
{
private final Type rowType;
private final List<RowExpression> arguments;
private static final int MEGAMORPHIC_FIELD_COUNT = 64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume this value is arbitrary and not perf tested. If so, we should comment here, so people don't assume 64 is the best value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the value is taken from a constant by the same name in RowType, but unlike that variation was not specifically arrived at by testing for performance but rather was chosen based on seeming reasonable to balance the code size vs performance trade off (ie: mostly arbitrary). We can add a comment to that effect.

@pettyjamesm pettyjamesm merged commit 2fd0145 into trinodb:master Apr 10, 2024
95 checks passed
@pettyjamesm
Copy link
Member

Merged, thanks @Bhargavi-Sagi!

@github-actions github-actions bot added this to the 445 milestone Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed iceberg Iceberg connector
Development

Successfully merging this pull request may close these issues.

None yet

4 participants