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 install and bundle size by optimizing code gen #3996

Closed
2 tasks
everett1992 opened this issue Sep 28, 2022 · 6 comments
Closed
2 tasks

Reduce install and bundle size by optimizing code gen #3996

everett1992 opened this issue Sep 28, 2022 · 6 comments
Assignees
Labels
feature-request New feature or enhancement. May require GitHub community feedback. p2 This is a standard priority issue queued This issues is on the AWS team's backlog

Comments

@everett1992
Copy link
Contributor

everett1992 commented Sep 28, 2022

Describe the feature

The sdk code gen creates a lot of duplicate code for every command and type in the service model. Optimizing the code generator to use more helper functions should reduce the size of each client - affecting local install size, bundle size, parsing time, and potentially runtime performance. I'm not confident about runtime performance, but my understanding is that javascript runtimes are better at optimizing functions that are called repeatedly.

Examples from @aws-sdk/client-dynamodb@3.180.0

dist-cjs/models/models_0.js1

const ArchivalSummaryFilterSensitiveLog = (obj) => ({ ...obj });
exports.ArchivalSummaryFilterSensitiveLog = ArchivalSummaryFilterSensitiveLog;
const AttributeDefinitionFilterSensitiveLog = (obj) => ({ ...obj, });
exports.AttributeDefinitionFilterSensitiveLog = AttributeDefinitionFilterSensitiveLog;

repeated 154 times for different functions.
This could be reduced with a helper saving ~18 bytes per type.

const assign = (obj) => ({ ...obj });
const ArchivalSummaryFilterSensitiveLog = assign;
exports.ArchivalSummaryFilterSensitiveLog = ArchivalSummaryFilterSensitiveLog;
const AttributeDefinitionFilterSensitiveLog = assign;
exports.AttributeDefinitionFilterSensitiveLog = AttributeDefinitionFilterSensitiveLog;

dist-cjs/protocols/Aws_json1_0.js

Every command matches this pattern

const serializeAws_json1_0BatchExecuteStatementCommand = async (input, context) => {
    const headers = {
        "content-type": "application/x-amz-json-1.0",
        "x-amz-target": "DynamoDB_20120810.BatchExecuteStatement",
    };
    let body;
    body = JSON.stringify(serializeAws_json1_0BatchExecuteStatementInput(input, context));
    return buildHttpRpcRequest(context, headers, "/", undefined, body);
};
exports.serializeAws_json1_0BatchExecuteStatementCommand = serializeAws_json1_0BatchExecuteStatementCommand;

Switching to a helper saves about 40kb

const command = (target, fn, input, context) => {
  const headers = {
    "content-type": "application/x-amz-json-1.0",
    "x-amz-target": target,
  };
  let body;
  body = JSON.stringify(fn(input, context));
  return buildHttpRpcRequest(context, headers, "/", undefined, body);
}

// repeat for each command. Event this could be optimized for size, but I think it's caused by tsc, not aws codegen.
const serializeAws_json1_0BatchExecuteStatementCommand = command.bind(null, "DynamoDB_20120810.BatchExecuteStatement", serializeAws_json1_0BatchExecuteStatementInput)
exports.serializeAws_json1_0BatchExecuteStatementCommand = serializeAws_json1_0BatchExecuteStatementCommand;

These are the simplest examples I could find but a lot of the generated code has repetitive structure that could be abstracted into helpers.

Use Case

Bundling aws sdk and smity clients for the web or lambda.

Proposed Solution

Optimize the code gen

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

3.180.0

Environment details (OS name and version, etc.)

node

Footnotes

  1. My examples are from dist-cjs which was compiled targeting ES2018, rather than dist-es which targets es5, but the repetition is present in both, and the source typescript.

@everett1992 everett1992 added feature-request New feature or enhancement. May require GitHub community feedback. needs-triage This issue or PR still needs to be triaged. labels Sep 28, 2022
@kuhe
Copy link
Contributor

kuhe commented Sep 30, 2022

This is a favorite topic of mine. I have worked on improvements in this area such as smithy-lang/smithy-typescript#576, which I plan to also apply to JSON protocols like your example.

But they are not a high priority compared to functional features and fixes, and I don't know when I can continue this.

PRs are welcome. For this one the generator is located at https://github.com/awslabs/smithy-typescript/blob/main/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java#L213 as generateOperationSerializer.

@ajredniwja ajredniwja added p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Oct 3, 2022
@kuhe kuhe self-assigned this Oct 3, 2022
@kuhe
Copy link
Contributor

kuhe commented Mar 27, 2023

#4544 contributed towards this

JSON serde optimization: #4625

@RanVaknin RanVaknin added the queued This issues is on the AWS team's backlog label Feb 14, 2024
@kuhe
Copy link
Contributor

kuhe commented Mar 14, 2024

closing this because it is being addressed in a continuous manner

@kuhe kuhe closed this as completed Mar 14, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2024
@kuhe
Copy link
Contributor

kuhe commented Jul 11, 2024

Don't ask me why I made this list, but just as a record, here are some PRs related to reducing generated code size:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request New feature or enhancement. May require GitHub community feedback. p2 This is a standard priority issue queued This issues is on the AWS team's backlog
Projects
None yet
Development

No branches or pull requests

4 participants