Skip to content

Reduce install and bundle size by optimizing code gen #3996

Closed
@everett1992

Description

@everett1992

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/[email protected]

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.

Metadata

Metadata

Assignees

Labels

feature-requestNew feature or enhancement. May require GitHub community feedback.p2This is a standard priority issuequeuedThis issues is on the AWS team's backlog

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions