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 memory usage of eachMapping w/ loop #5

Merged
merged 1 commit into from
Oct 29, 2021

Conversation

noppa
Copy link

@noppa noppa commented Jul 22, 2021

Avoid creating arrays of intermediate values to pass to aCallback, by
refactoring the .map + .forEach combination to a single loop that that
creates the options object and immediately passes it to aCallback.

This reduces memory usage, because engine no longer has to hold all the
options objects in memory, each of the created objects is free to be gcd
once the loop continues to next item.

When the looped-over array mappings is very large (e.g. >40k items),
the operation may even crash with out of memory exception without this
patch.

Avoid creating arrays of intermediate values to pass to `aCallback`, by
refactoring the .map + .forEach combination to a single loop that that
creates the options object and immediately passes it to aCallback.

This reduces memory usage, because engine no longer has to hold all the
options objects in memory, each of the created objects is free to be gcd
once the loop continues to next item.

When the looped-over array `mappings` is very large (e.g. >40k items),
the operation may even crash with out of memory exception without this
patch.
@noppa
Copy link
Author

noppa commented Jul 22, 2021

After upgrading to Webpack 5, our largeish application's full production build started throwing out of memory exceptions, even after replacing source-map with source-map-js. Little profiling showed the culprit to be this function that created an intermediate array (which in our case was over 40k items) of options before passing them to the callback.

After this patch, our build passes fine and takes about 20% less memory.

@noppa
Copy link
Author

noppa commented Jul 23, 2021

Pinging @7rulnik because you are not in the Watch-list of this repo 🙂

@7rulnik
Copy link
Owner

7rulnik commented Jul 23, 2021

@noppa hi and thanks!

I will take a look into it on next week

Copy link
Owner

@7rulnik 7rulnik left a comment

Choose a reason for hiding this comment

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

@noppa sorry for such slow response :(

Great catch!

BTW webpack doesn't use source-map library anymore since https://github.com/webpack/webpack/releases/tag/v5.47.0. But it still makes sense for PostCSS for example!

@7rulnik 7rulnik merged commit f45fcf9 into 7rulnik:patch-0.6.1 Oct 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants