Skip to content

Commit 08cd2f0

Browse files
committed
introduce ChunkGroup for AND in parents
1 parent 1c5f294 commit 08cd2f0

15 files changed

+418
-21
lines changed

lib/Chunk.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const util = require("util");
88
const compareLocations = require("./compareLocations");
9+
const ChunkGroup = require("./ChunkGroup");
910
const SortableSet = require("./util/SortableSet");
1011
let debugId = 1000;
1112

@@ -467,9 +468,24 @@ class Chunk {
467468
newChunk._blocks.add(block);
468469
block.chunks.push(newChunk);
469470
}
470-
for(const chunk of this._chunks) {
471-
newChunk.addChunk(chunk);
472-
chunk._parents.add(newChunk);
471+
for(const chunk of Array.from(this._chunks)) {
472+
if(chunk instanceof ChunkGroup) {
473+
newChunk.addChunk(chunk);
474+
chunk._parents.add(newChunk);
475+
} else {
476+
const newChunkGroup = new ChunkGroup();
477+
// remove old connection
478+
chunk._parents.delete(this);
479+
this._chunks.delete(chunk);
480+
// add chunk group connection
481+
chunk._parents.add(newChunkGroup);
482+
newChunkGroup._chunks.add(chunk);
483+
this._chunks.add(newChunkGroup);
484+
newChunkGroup._parents.add(this);
485+
// add newChunk connection
486+
newChunk._chunks.add(newChunkGroup);
487+
newChunkGroup._parents.add(newChunk);
488+
}
473489
}
474490
for(const parentChunk of this._parents) {
475491
parentChunk.addChunk(newChunk);

lib/ChunkGroup.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const SortableSet = require("./util/SortableSet");
8+
9+
const getArray = set => Array.from(set);
10+
11+
const sortById = (a, b) => {
12+
if(a.id < b.id) return -1;
13+
if(b.id < a.id) return 1;
14+
return 0;
15+
};
16+
17+
const getId = set => {
18+
set.sort();
19+
return Array.from(set, x => x.id).join("+");
20+
};
21+
22+
const getDebugId = set => {
23+
set.sort();
24+
return Array.from(set, x => x.debugId).join("+");
25+
};
26+
27+
class ChunkGroup {
28+
constructor() {
29+
this._chunks = new SortableSet(undefined, sortById);
30+
this._parents = new SortableSet(undefined, sortById);
31+
}
32+
33+
get debugId() {
34+
return this._parents.getFromUnorderedCache(getDebugId);
35+
}
36+
37+
get id() {
38+
return this._parents.getFromUnorderedCache(getId);
39+
}
40+
41+
isInitial() {
42+
return false;
43+
}
44+
45+
hasRuntime() {
46+
return false;
47+
}
48+
49+
hasEntryModule() {
50+
return false;
51+
}
52+
53+
get modulesIterable() {
54+
return [];
55+
}
56+
57+
addChunk(chunk) {
58+
if(this._chunks.has(chunk)) {
59+
return false;
60+
}
61+
this._chunks.add(chunk);
62+
return true;
63+
}
64+
65+
getChunks() {
66+
return this._chunks.getFromCache(getArray);
67+
}
68+
69+
getNumberOfChunks() {
70+
return this._chunks.size;
71+
}
72+
73+
get chunksIterable() {
74+
return this._chunks;
75+
}
76+
77+
addParent(parentChunk) {
78+
if(!this._parents.has(parentChunk)) {
79+
this._parents.add(parentChunk);
80+
return true;
81+
}
82+
return false;
83+
}
84+
85+
getParents() {
86+
return this._parents.getFromCache(getArray);
87+
}
88+
89+
setParents(newParents) {
90+
this._parents.clear();
91+
for(const p of newParents)
92+
this._parents.add(p);
93+
}
94+
95+
mapParents(fn) {
96+
return Array.from(this._parents, fn);
97+
}
98+
99+
getNumberOfParents() {
100+
return this._parents.size;
101+
}
102+
103+
hasParent(parent) {
104+
return this._parents.has(parent);
105+
}
106+
107+
get parentsIterable() {
108+
return this._parents;
109+
}
110+
111+
}
112+
113+
module.exports = ChunkGroup;

lib/optimize/RemoveParentModulesPlugin.js

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"use strict";
66

77
const Queue = require("../util/Queue");
8+
const ChunkGroup = require("../ChunkGroup");
89

910
const getParentChunksWithModule = (currentChunk, module) => {
1011
const chunks = [];
@@ -27,35 +28,75 @@ class RemoveParentModulesPlugin {
2728
apply(compiler) {
2829
compiler.hooks.compilation.tap("RemoveParentModulesPlugin", (compilation) => {
2930
const handler = (chunks) => {
30-
const queue = new Queue(chunks);
31+
const queue = new Queue();
3132
const availableModulesMap = new Map();
3233

3334
for(const chunk of chunks) {
3435
// initialize available modules for chunks without parents
35-
if(chunk.getNumberOfParents() === 0)
36+
if(chunk.getNumberOfParents() === 0) {
3637
availableModulesMap.set(chunk, new Set());
38+
for(const child of chunk.chunksIterable)
39+
queue.enqueue(child);
40+
}
3741
}
3842

3943
while(queue.length > 0) {
4044
const chunk = queue.dequeue();
4145
let availableModules = availableModulesMap.get(chunk);
4246
let changed = false;
43-
for(const parent of chunk.parentsIterable) {
44-
const availableModulesInParent = availableModulesMap.get(parent);
45-
if(availableModulesInParent !== undefined) {
46-
// If we know the available modules in parent: process these
47-
if(availableModules === undefined) {
48-
// if we have not own info yet: create new entry
49-
availableModules = new Set(availableModulesInParent);
50-
for(const m of parent.modulesIterable)
51-
availableModules.add(m);
52-
availableModulesMap.set(chunk, availableModules);
53-
changed = true;
54-
} else {
55-
for(const m of availableModules) {
56-
if(!parent.containsModule(m) && !availableModulesInParent.has(m)) {
57-
availableModules.delete(m);
58-
changed = true;
47+
if(chunk instanceof ChunkGroup) {
48+
let allParentAvailableModules = new Set();
49+
for(const parent of chunk.parentsIterable) {
50+
const availableModulesInParent = availableModulesMap.get(parent);
51+
if(availableModulesInParent === undefined) {
52+
allParentAvailableModules = undefined;
53+
break;
54+
}
55+
for(const module of availableModulesInParent)
56+
allParentAvailableModules.add(module);
57+
}
58+
if(availableModules === undefined) {
59+
// if we have not own info yet: create new entry
60+
availableModules = allParentAvailableModules;
61+
availableModulesMap.set(chunk, availableModules);
62+
changed = true;
63+
} else {
64+
for(const m of availableModules) {
65+
if(!allParentAvailableModules.has(m)) {
66+
availableModules.delete(m);
67+
changed = true;
68+
}
69+
}
70+
}
71+
} else {
72+
for(const parent of chunk.parentsIterable) {
73+
const availableModulesInParent = availableModulesMap.get(parent);
74+
if(availableModulesInParent !== undefined) {
75+
// If we know the available modules in parent: process these
76+
if(availableModules === undefined) {
77+
// if we have not own info yet: create new entry
78+
availableModules = new Set(availableModulesInParent);
79+
if(!(parent instanceof ChunkGroup)) {
80+
for(const m of parent.modulesIterable)
81+
availableModules.add(m);
82+
}
83+
availableModulesMap.set(chunk, availableModules);
84+
changed = true;
85+
} else {
86+
if(parent instanceof ChunkGroup) {
87+
for(const m of availableModules) {
88+
if(!availableModulesInParent.has(m)) {
89+
availableModules.delete(m);
90+
changed = true;
91+
}
92+
}
93+
} else {
94+
for(const m of availableModules) {
95+
if(!parent.containsModule(m) && !availableModulesInParent.has(m)) {
96+
availableModules.delete(m);
97+
changed = true;
98+
}
99+
}
59100
}
60101
}
61102
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Hash: 3ee24ba6bc0fc181d72b3ee24ba6bc0fc181d72b
2+
Child fitting:
3+
Hash: 3ee24ba6bc0fc181d72b
4+
Time: Xms
5+
Asset Size Chunks Chunk Names
6+
57459d21f0df51ccc855.js 2.42 KiB 0 [emitted]
7+
7c20dbb0769a7f28f25f.js 1.05 KiB 1 [emitted]
8+
60426dd4d835ada7b684.js 8.33 KiB 4 [emitted]
9+
445908eb332cabac751e.js 1.93 KiB 5 [emitted]
10+
Entrypoint main = 60426dd4d835ada7b684.js 445908eb332cabac751e.js 57459d21f0df51ccc855.js
11+
chunk {0} 57459d21f0df51ccc855.js 1.87 KiB [initial] [rendered]
12+
> aggressive-splitted main [4] ./index.js
13+
[3] ./e.js 899 bytes {0} [built]
14+
[4] ./index.js 111 bytes {0} [built]
15+
[6] ./f.js 900 bytes {0} [built]
16+
chunk {1} 7c20dbb0769a7f28f25f.js 916 bytes {0+4+5} [rendered]
17+
> [4] ./index.js 7:0-13
18+
[7] ./g.js 916 bytes {1} [built]
19+
chunk {4} 60426dd4d835ada7b684.js 1.76 KiB [entry] [rendered] [recorded]
20+
> aggressive-splitted main [4] ./index.js
21+
[2] ./d.js 899 bytes {4} [built]
22+
[5] ./a.js 899 bytes {4} [built]
23+
chunk {5} 445908eb332cabac751e.js 1.76 KiB [initial] [rendered] [recorded]
24+
> aggressive-splitted main [4] ./index.js
25+
[0] ./b.js 899 bytes {5} [built]
26+
[1] ./c.js 899 bytes {5} [built]
27+
Child content-change:
28+
Hash: 3ee24ba6bc0fc181d72b
29+
Time: Xms
30+
Asset Size Chunks Chunk Names
31+
57459d21f0df51ccc855.js 2.42 KiB 0 [emitted]
32+
7c20dbb0769a7f28f25f.js 1.05 KiB 1 [emitted]
33+
60426dd4d835ada7b684.js 8.33 KiB 4 [emitted]
34+
445908eb332cabac751e.js 1.93 KiB 5 [emitted]
35+
Entrypoint main = 60426dd4d835ada7b684.js 445908eb332cabac751e.js 57459d21f0df51ccc855.js
36+
chunk {0} 57459d21f0df51ccc855.js 1.87 KiB [initial] [rendered]
37+
> aggressive-splitted main [4] ./index.js
38+
[3] ./e.js 899 bytes {0} [built]
39+
[4] ./index.js 111 bytes {0} [built]
40+
[6] ./f.js 900 bytes {0} [built]
41+
chunk {1} 7c20dbb0769a7f28f25f.js 916 bytes {0+4+5} [rendered]
42+
> [4] ./index.js 7:0-13
43+
[7] ./g.js 916 bytes {1} [built]
44+
chunk {4} 60426dd4d835ada7b684.js 1.76 KiB [entry] [rendered] [recorded]
45+
> aggressive-splitted main [4] ./index.js
46+
[2] ./d.js 899 bytes {4} [built]
47+
[5] ./a.js 899 bytes {4} [built]
48+
chunk {5} 445908eb332cabac751e.js 1.76 KiB [initial] [rendered] [recorded]
49+
> aggressive-splitted main [4] ./index.js
50+
[0] ./b.js 899 bytes {5} [built]
51+
[1] ./c.js 899 bytes {5} [built]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*************************************************************************************************/
2+
/*************************************************************************************************/
3+
/*************************************************************************************************/
4+
/*************************************************************************************************/
5+
/*************************************************************************************************/
6+
/*************************************************************************************************/
7+
/*************************************************************************************************/
8+
/*************************************************************************************************/
9+
/*************************************************************************************************/
10+
require("./f");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require("./a");
2+
require("./b");
3+
require("./c");
4+
require("./d");
5+
require("./e");
6+
require("./f");
7+
import("./g");

0 commit comments

Comments
 (0)