Skip to content

Simplify identifier creation in ConcatenatedModules - #6001

Merged
sokra merged 2 commits into
webpack:masterfrom
lencioni:concatenation-efficiency
Nov 24, 2017
Merged

sokra merged 2 commits into
webpack:masterfrom
lencioni:concatenation-efficiency

Conversation

@lencioni

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Optimization

Did you add tests for your changes?

No

If relevant, link to documentation update:

Summary

This was implemented in #5997 to fix some memory issues, but I think we
can make this a little more efficient. By using a single forEach instead
of a map, filter, and join, we avoid unnecessary array creations and
iterations.

Does this PR introduce a breaking change?

No

Other information

@webpack-bot

Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@sokra

sokra commented Nov 23, 2017

Copy link
Copy Markdown
Member

I did measure it and calling hash.update often is more expensive than calling it with a large string.

As explanation I would guess update calls into node.js C++ + OpenSSL while concatenating strings is very optimized in JIT.

@sokra

sokra commented Nov 23, 2017

Copy link
Copy Markdown
Member

But thanks for the PR. We can optimize webpack by doing the reverse: calling hash.update less often and instead concatenate string.

@sokra sokra closed this Nov 23, 2017
@sokra sokra reopened this Nov 23, 2017

@sokra sokra left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Feel free to rewrite it to string concatenation instead:

for(const info of this._orderedConcatenationList) {
  if(info.type === "concatenated") {
    str += info.module.identifier() + " ";
  }
}
hash.update(str);

This was implemented in webpack#5997 to fix some memory issues, but I think we
can make this a little more efficient. By using a single forEach instead
of a map, filter, and join, we avoid unnecessary array creations and
iterations.
@lencioni
lencioni force-pushed the concatenation-efficiency branch from f05724b to 5968f6e Compare November 23, 2017 16:01
@webpack-bot

Copy link
Copy Markdown
Contributor

@lencioni Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@sokra Please review the new changes.

@lencioni

lencioni commented Nov 23, 2017 •

Copy link
Copy Markdown
Contributor Author

Thanks for doing the profiling @sokra! I've added a commit to this PR that incorporates this change.

@lencioni
lencioni force-pushed the concatenation-efficiency branch from 5968f6e to 7967700 Compare November 23, 2017 16:02
Comment thread lib/optimize/ConcatenatedModule.js Outdated
switch(info.type) {
case "concatenated":
return info.module.identifier();
let orderedConcatenationListIdentifiers;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

let a;
a += 'wat';
console.log(a); // "undefinedwat"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

lol, woops! fixed now. Thanks!

for(let i = 0; i < this._orderedConcatenationList.length; i++) {
if(this._orderedConcatenationList[i].type === "concatenated") {
orderedConcatenationListIdentifiers += this._orderedConcatenationList[i].module.identifier();
orderedConcatenationListIdentifiers += " ";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's strange

@sokra did some profiling and it seems that calling crypto.update
multiple times is slower than calling it once with a large string.

> As explanation I would guess update calls into node.js C++ + OpenSSL
> while concatenating strings is very optimized in JIT.

To take better advantage of this information, I have rewritten this
performance-sensitive code to build up a concatenated string and then
hash it at the end.

Since we are thinking about performance here, I opted for a regular for
loop instead of using the forEach iterator. I also did some basic
benchmarking of string concatenation strategies and discovered that
separating out the addition of the extra space into its own
concatenation line instead of tacking it on to the end of a single
concatenation might just barely have the edge in terms of performance. I
think it is also very readable this way, so it seems like a good
tradeoff to make: a little lost conciseness for speed.
@lencioni
lencioni force-pushed the concatenation-efficiency branch from 7967700 to 37d70bd Compare November 23, 2017 16:31
@sokra
sokra merged commit 2525466 into webpack:master Nov 24, 2017
@sokra

sokra commented Nov 24, 2017

Copy link
Copy Markdown
Member

Thanks

@sokra

sokra commented Nov 25, 2017

Copy link
Copy Markdown
Member

#5718

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.

4 participants