Skip to content

Commit beccf94

Browse files
committed
convert algorithm to iterative, use less memory
1 parent caa20ea commit beccf94

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

lib/Chunk.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -536,43 +536,64 @@ class Chunk {
536536
}
537537

538538
getChunkMaps(includeEntries, realHash) {
539-
const chunksProcessed = new Set();
540539
const chunkHashMap = Object.create(null);
541540
const chunkNameMap = Object.create(null);
542-
const addChunk = chunk => {
543-
if(chunksProcessed.has(chunk)) return;
544-
chunksProcessed.add(chunk);
541+
542+
const queue = [this];
543+
const chunksEnqueued = new Set([this]);
544+
545+
while(queue.length > 0) {
546+
const chunk = queue.pop();
545547
if(!chunk.hasRuntime() || includeEntries) {
546548
chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
547549
if(chunk.name)
548550
chunkNameMap[chunk.id] = chunk.name;
549551
}
550-
chunk._chunks.forEach(addChunk);
551-
};
552-
addChunk(this);
552+
for(const child of chunk.chunksIterable) {
553+
if(chunksEnqueued.has(child)) continue;
554+
chunksEnqueued.add(child);
555+
queue.push(child);
556+
}
557+
}
558+
553559
return {
554560
hash: chunkHashMap,
555561
name: chunkNameMap
556562
};
557563
}
558564

559565
getChunkModuleMaps(includeEntries, filterFn) {
560-
const chunksProcessed = new Set();
561566
const chunkModuleIdMap = Object.create(null);
562567
const chunkModuleHashMap = Object.create(null);
563-
(function addChunk(chunk) {
564-
if(chunksProcessed.has(chunk)) return;
565-
chunksProcessed.add(chunk);
568+
569+
const chunksEnqueued = new Set([this]);
570+
const queue = [this];
571+
572+
while(queue.length > 0) {
573+
const chunk = queue.pop();
566574
if(!chunk.hasRuntime() || includeEntries) {
567-
const array = chunk.getModules().filter(filterFn);
568-
if(array.length > 0)
569-
chunkModuleIdMap[chunk.id] = array.map(m => m.id).sort();
570-
for(const m of array) {
571-
chunkModuleHashMap[m.id] = m.renderedHash;
575+
let array = undefined;
576+
for(const module of chunk.modulesIterable) {
577+
if(filterFn(module)) {
578+
if(array === undefined) {
579+
array = [];
580+
chunkModuleIdMap[chunk.id] = array;
581+
}
582+
array.push(module.id);
583+
chunkModuleHashMap[module.id] = module.renderedHash;
584+
}
585+
}
586+
if(array !== undefined) {
587+
array.sort();
572588
}
573589
}
574-
chunk._chunks.forEach(addChunk);
575-
}(this));
590+
for(const child of chunk.chunksIterable) {
591+
if(chunksEnqueued.has(child)) continue;
592+
chunksEnqueued.add(child);
593+
queue.push(child);
594+
}
595+
}
596+
576597
return {
577598
id: chunkModuleIdMap,
578599
hash: chunkModuleHashMap
@@ -585,8 +606,9 @@ class Chunk {
585606

586607
while(queue.length > 0) {
587608
const chunk = queue.pop();
588-
if(chunk.getModules().some(filterFn))
589-
return true;
609+
for(const module of chunk.modulesIterable)
610+
if(filterFn(module))
611+
return true;
590612
for(const next of chunk.chunksIterable) {
591613
if(!chunksProcessed.has(next)) {
592614
chunksProcessed.add(next);

0 commit comments

Comments
 (0)