This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
perf(lambda-at-edge): fix rollup bundling for dynamic imports perf #1029
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Note: testing rollup config to properly bundle dynamic imports like s3client which is not always needed. The cost is an additional wrapper on top of main code but this seems expected optimization from Rollup.js (preloading and parsing optimization).
Previously, inlining dynamic imports into one file was still causing that code to be at least partially preloaded outside of the handler, e.g initializing some S3/AWS SDK related objects even if the S3Client itself was only initialized when needed. So it caused higher cold starts due to this for all paths. With this change, we split into multiple chunks. Total there should be a few chunks:
Total size of all chunks remains around the same but slightly higher at 657 kB vs. 651 kB before. However, Lambda does not need to read (and preload some parts of it) ~166 kB of code for S3 unless it's needed. There is one extra file I/O due to the wrapper requiring the core code but from tests, it appears to be negligible as ~25 ms is saved in init time from my tests (195 -> 170 ms with the 512 MB Lambda function)
In the future, we can do further optimizations like getting rid of complexity of having the origin response handler, and even using Rollup again at build/deploy time to further get rid of code paths that are not used (e.g if user has no rewrites, we can get rid of that code)