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

Sortable set #5083

Merged
merged 10 commits into from Jun 21, 2017
Merged

Sortable set #5083

merged 10 commits into from Jun 21, 2017

Conversation

timse
Copy link
Member

@timse timse commented Jun 18, 2017

What kind of change does this PR introduce?

feature

Did you add tests for your changes?

tested through existing code

If relevant, link to documentation update:

N/A

Summary

Introduce a "SortableSet" Collection that abstracts the sorting out of the responsibility of Module/Chunk

Does this PR introduce a breaking change?

No

Other information

Per default _chunks are flagged as sorted in Module, however, this only holds true as they are initialized empty.
Concatenated Module, however, has initial modules and therefore is not guaranteed to be ordered, the flags should, therefore, be false. Using SortedSet fixes this as a side effect.

use SortableSet to keep "_modules" sorted
use SortableSet to keep "_chunks" sorted
fix tests that was falsy - per default chunks are flagged as sorted in module
however this only holds true as they are initialized empty. Concatenated module however
has initial modules and therefore is not guaranteed to be ordered, the flags should therfor be false.
Using SortedSet fixes this as a sideeffect
lib/Chunk.js Outdated
class Chunk {

static sortById(a, b) {
Copy link
Member

Choose a reason for hiding this comment

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

Why making this methods public? What was wrong with the original way, which hides them?

Copy link
Member Author

Choose a reason for hiding this comment

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

Initially I wanted to flip them, so sortByXXX was in Module and Chunk would use Module.sortByXXX to sort its Modules.
As Module should know how to sort modules, not Chunk.
However node and circular dependencies let me down and so this is a left over of that approach, happy to make them private again, but I'd rather find a solution for the intial idea. :D

* @returns {void}
*/
sortWith(sortFn) {
const sortedArray = Array.from(this).sort(sortFn);
Copy link
Member

Choose a reason for hiding this comment

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

The original impl checked if the set was already sorted before sorting. The new SortableSet should do the same. add and clear should be overriden to invalidate sorting. Best store the current active sortFn.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I wasn't sure if this approach is ok and if I wanted to shadow as little as possible from the initial set (: happy to add some logic there

@@ -19,7 +19,7 @@ Child
[0] (webpack)/test/statsCases/scope-hoisting-multi/common_lazy_shared.js 25 bytes {0} {1} {2} [built]
[1] (webpack)/test/statsCases/scope-hoisting-multi/vendor.js 25 bytes {5} [built]
ModuleConcatenation (inner): module is an entrypoint
[2] (webpack)/test/statsCases/scope-hoisting-multi/common.js + 1 modules 62 bytes {4} {3} [built]
[2] (webpack)/test/statsCases/scope-hoisting-multi/common.js + 1 modules 62 bytes {3} {4} [built]
Copy link
Member

Choose a reason for hiding this comment

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

This should not happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

see: bd8c6cf

fix tests that was falsy - per default chunks are flagged as sorted in module
however this only holds true as they are initialized empty. Concatenated module however
has initial modules and therefore is not guaranteed to be ordered, the flags should therfor be false.
Using SortedSet fixes this as a sideeffect

Copy link
Member

Choose a reason for hiding this comment

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

Ah okey. 👍

Copy link
Member

Choose a reason for hiding this comment

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

It seem to be different again...

Copy link
Member Author

Choose a reason for hiding this comment

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

ha yea, it tries to use the sort method from Module but its no longer exposed...

meh, i'd love to just have a compareBy method on them :P maybe in another PR if thats ok :D

@webpack-bot
Copy link
Contributor

@timse Thanks for your update.

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

@sokra Please review the new changes.

super(initialIterable);
this._sortFn = defaultSort;
this._lastActiveSortFn = null;
this._isSorted = false;
Copy link
Member

Choose a reason for hiding this comment

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

_isSorted is redundant to _lastActiveSortFn. _isSorted === !!_lastActiveSortFn.

=> _isSorted can be removed.

/**
* @returns {void}
*/
clear() {
Copy link
Member

Choose a reason for hiding this comment

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

clear can be removed, as an empty set is always sorted.

Copy link
Member Author

Choose a reason for hiding this comment

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

true :D

* @returns {void}
*/
sortWith(sortFn) {
if(this._isSorted && sortFn === this._lastActiveSortFn) {
Copy link
Member

Choose a reason for hiding this comment

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

We could skip sorting if this.size === 0. This avoids creating a new empty array below.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

@@ -19,7 +19,7 @@ Child
[0] (webpack)/test/statsCases/scope-hoisting-multi/common_lazy_shared.js 25 bytes {0} {1} {2} [built]
[1] (webpack)/test/statsCases/scope-hoisting-multi/vendor.js 25 bytes {5} [built]
ModuleConcatenation (inner): module is an entrypoint
[2] (webpack)/test/statsCases/scope-hoisting-multi/common.js + 1 modules 62 bytes {4} {3} [built]
[2] (webpack)/test/statsCases/scope-hoisting-multi/common.js + 1 modules 62 bytes {3} {4} [built]
Copy link
Member

Choose a reason for hiding this comment

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

Ah okey. 👍

- remove unnecessary _isSorted flag
- remove clear-override as an empty set is "sorted"
- early return on empty sets
@webpack-bot
Copy link
Contributor

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

@sokra sokra merged commit 25a904b into webpack:master Jun 21, 2017
@sokra
Copy link
Member

sokra commented Jun 21, 2017

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants