Skip to content

Reduce memory usage of eachMapping w/ loop - #5

Merged
7rulnik merged 1 commit into
7rulnik:patch-0.6.1from
noppa:reduce-memory-usage
Oct 29, 2021
Merged

7rulnik merged 1 commit into
7rulnik:patch-0.6.1from
noppa:reduce-memory-usage

Conversation

@noppa

@noppa noppa commented Jul 22, 2021

Copy link
Copy Markdown

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

noppa commented Jul 22, 2021 •

Copy link
Copy Markdown
Author

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

noppa commented Jul 23, 2021

Copy link
Copy Markdown
Author

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

@7rulnik

7rulnik commented Jul 23, 2021

Copy link
Copy Markdown
Owner

@noppa hi and thanks!

I will take a look into it on next week

@7rulnik 7rulnik left a comment

Copy link
Copy Markdown
Owner

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