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 9, 2022
1 parent cf06c30 commit c2834b8
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions src/utils/chunkAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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 @@ -312,26 +332,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 @@ -511,17 +511,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

0 comments on commit c2834b8

Please sign in to comment.