-
Notifications
You must be signed in to change notification settings - Fork 509
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
perf(aws-lambda): inline dynamic chunks to reduce first invokation time #650
perf(aws-lambda): inline dynamic chunks to reduce first invokation time #650
Conversation
Would you provide an example of some code that takes that additional time? Inlining dynamic imports will make the entrypoint larger, so this is paradoxical behaviour. |
Thanks for the PR. Dynamic chunks loading in general (both browser and Node) has a natural overhead. But it is essential to make timings stable when bundle size does grow while if we inline dependencies, it increases size even when not used. So whenever possible with Nitro presets, we tend to use chunks. As @danielroe mentioned, would be nice if you can provide a public reproduction of your project to test timing differences. |
Thank you for reviewing @danielroe @pi0. I understand what you mean, but fact is not so in AWS Lambda.
Here's what the documentation says.
So dynamic chunk loading may not be a good match for AWS Lambda. Also I created public reproduction here. Please try it. requires
Deploy commandTo deploy, just run the following command: git clone https://github.com/WinterYukky/nitro-api-gateway-has-cold-start.git
cd nitro-api-gateway-has-cold-start
yarn install
yarn cdk deploy
# If you want to destroy, run this command
# yarn cdk destroy ...and, my executed logs is this. Please focus the inlineDynamicImports is
|
Thanks for sharing links and stats @WinterYukky β€οΈ Also linking this. When assuming most of the chunks of the server bundle will be used during every invocation, inlining them makes perfect sense and reduces latency (actually faster invokation). However, when the number of server handlers (and individual libraries and other assets they use) grows, I believe this adds to the cold-start time. Also nitro function is special in the way that it is not a single function but a function dispatcher itself that invokes different handlers. I'm positive this would be a nice idea to enable inlining by default but please let me run some more tests and check alternatives if we can keep using chunks but warmup opon code-initialization for entry critical chunks. |
@pi0 We could add this as |
Co-Authored-By: WinterYukky <49480575+WinterYukky@users.noreply.github.com>
I have added docs for aws deployment for now to note about |
π Linked issue
None
β Type of change
π Description
This PR will reduce AWS Lambda runtime and improve cost savings and performance for customers.
By default, inlineDynamicImports is false in nitro. Therefore, if we define an API that uses any module under the
server/api
directory and run it, the module will be loaded at runtime and the first time execution will take a long time. In fact, I created and run an API that just scans DynamoDB using aws-sdk v3, it will take more than 3 seconds the first time. This exceeds the default timeout time of AWS Lambda, which causes the API Gateway response to be an Internal Server Error. To avoid this, set inlineDynamicImports to true in the aws-lambda preset. This change speeds up the API from taking 3 seconds to 300 milliseconds.π Checklist