Skip to content

Commit

Permalink
Improve cycle prevention mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Dec 10, 2022
1 parent d3a2e23 commit c2de6b9
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions src/utils/chunkAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ function createChunks(
}));
}

function getChunkModulesBySignature(
assignedEntriesByModule: ReadonlyDependentModuleMap,
allEntries: Iterable<Module>
) {
const chunkModules: { [chunkSignature: string]: Module[] } = Object.create(null);
for (const [module, assignedEntries] of assignedEntriesByModule) {
let chunkSignature = '';
for (const entry of allEntries) {
chunkSignature += assignedEntries.has(entry) ? CHAR_DEPENDENT : CHAR_INDEPENDENT;
}
const chunk = chunkModules[chunkSignature];
if (chunk) {
chunk.push(module);
} else {
chunkModules[chunkSignature] = [module];
}
}
return chunkModules;
}

/**
* This function tries to get rid of small chunks by merging them with other
* chunks. In order to merge chunks, one must obey the following rule:
Expand Down Expand Up @@ -316,26 +336,6 @@ const CHAR_DEPENDENT = 'X';
const CHAR_INDEPENDENT = '_';
const CHAR_CODE_DEPENDENT = CHAR_DEPENDENT.charCodeAt(0);

function getChunkModulesBySignature(
assignedEntriesByModule: ReadonlyDependentModuleMap,
allEntries: Iterable<Module>
) {
const chunkModules: { [chunkSignature: string]: Module[] } = Object.create(null);
for (const [module, assignedEntries] of assignedEntriesByModule) {
let chunkSignature = '';
for (const entry of allEntries) {
chunkSignature += assignedEntries.has(entry) ? CHAR_DEPENDENT : CHAR_INDEPENDENT;
}
const chunk = chunkModules[chunkSignature];
if (chunk) {
chunk.push(module);
} else {
chunkModules[chunkSignature] = [module];
}
}
return chunkModules;
}

function getPartitionedChunks(
chunkModulesBySignature: { [chunkSignature: string]: Module[] },
minChunkSize: number
Expand Down Expand Up @@ -515,17 +515,32 @@ function mergeChunks(
}
}

// If a module is a transitive but not a direct dependency of the other chunk,
// merging is prohibited as that would create a new cyclic dependency.
// Merging will not produce cycles if
// - no chunk is a transitive dependency of the other, or
// - none of the direct non-merged dependencies of a chunk have the other
// chunk as a transitive dependency
function isValidMerge(mergedChunk: ChunkDescription, targetChunk: ChunkDescription) {
return (
(!targetChunk.transitiveDependencies.has(mergedChunk) ||
targetChunk.dependencies.has(mergedChunk)) &&
(!mergedChunk.transitiveDependencies.has(targetChunk) ||
mergedChunk.dependencies.has(targetChunk))
return !(
hasTransitiveDependency(mergedChunk, targetChunk) ||
hasTransitiveDependency(targetChunk, mergedChunk)
);
}

function hasTransitiveDependency(
dependentChunk: ChunkDescription,
dependencyChunk: ChunkDescription
) {
if (!dependentChunk.transitiveDependencies.has(dependencyChunk)) {
return false;
}
for (const dependency of dependentChunk.dependencies) {
if (dependency.transitiveDependencies.has(dependencyChunk)) {
return true;
}
}
return false;
}

function getChunksInPartition(
chunk: ChunkDescription,
minChunkSize: number,
Expand Down Expand Up @@ -566,7 +581,3 @@ function mergeSignatures(sourceSignature: string, targetSignature: string): stri
}
return signature;
}

function getNewSet<T>() {
return new Set<T>();
}

0 comments on commit c2de6b9

Please sign in to comment.