Skip to content

Commit

Permalink
Improve chunk assignment performance (#4736)
Browse files Browse the repository at this point in the history
* Slightly simplify graph analysis

Also generates dynamicImportsByEntry that will help us to track already loaded
modules more efficiently.

* Implement new algorithm with hopefully better performance

* Further refine chunking algorithm

* Add cut-off condition to only back-track modules if there are few imports

* Use shared Set generator
  • Loading branch information
lukastaegert committed Dec 10, 2022
1 parent 2c23141 commit f368adf
Show file tree
Hide file tree
Showing 43 changed files with 386 additions and 114 deletions.
6 changes: 3 additions & 3 deletions cli/run/batchWarnings.ts
@@ -1,6 +1,6 @@
import type { RollupWarning } from '../../src/rollup/types';
import { bold, gray, yellow } from '../../src/utils/colors';
import { getOrCreate } from '../../src/utils/getOrCreate';
import { getNewArray, getOrCreate } from '../../src/utils/getOrCreate';
import { printQuotedStringList } from '../../src/utils/printStringList';
import relativeId from '../../src/utils/relativeId';
import { stderr } from '../logging';
Expand All @@ -23,7 +23,7 @@ export default function batchWarnings(): BatchWarnings {
warningOccurred = true;

if (warning.code! in deferredHandlers) {
getOrCreate(deferredWarnings, warning.code!, () => []).push(warning);
getOrCreate(deferredWarnings, warning.code!, getNewArray).push(warning);
} else if (warning.code! in immediateHandlers) {
immediateHandlers[warning.code!](warning);
} else {
Expand Down Expand Up @@ -220,7 +220,7 @@ const deferredHandlers: {

const dependencies = new Map<string, string[]>();
for (const warning of warnings) {
getOrCreate(dependencies, relativeId(warning.exporter!), () => []).push(
getOrCreate(dependencies, relativeId(warning.exporter!), getNewArray).push(
relativeId(warning.id!)
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Chunk.ts
Expand Up @@ -41,7 +41,7 @@ import { assignExportsToMangledNames, assignExportsToNames } from './utils/expor
import type { GenerateCodeSnippets } from './utils/generateCodeSnippets';
import getExportMode from './utils/getExportMode';
import getIndentString from './utils/getIndentString';
import { getOrCreate } from './utils/getOrCreate';
import { getNewArray, getOrCreate } from './utils/getOrCreate';
import { getStaticDependencies } from './utils/getStaticDependencies';
import type { HashPlaceholderGenerator } from './utils/hashPlaceholders';
import { replacePlaceholders } from './utils/hashPlaceholders';
Expand Down Expand Up @@ -911,7 +911,7 @@ export default class Chunk {
dependency = this.chunkByModule.get(module)!;
imported = dependency.getVariableExportName(variable);
}
getOrCreate(importsByDependency, dependency, () => []).push({
getOrCreate(importsByDependency, dependency, getNewArray).push({
imported,
local: variable.getName(this.snippets.getPropertyAccess)
});
Expand Down Expand Up @@ -1023,7 +1023,7 @@ export default class Chunk {
(imported !== 'default' || isDefaultAProperty(interop(module.id), true));
}
}
getOrCreate(reexportSpecifiers, dependency, () => []).push({
getOrCreate(reexportSpecifiers, dependency, getNewArray).push({
imported,
needsLiveBinding,
reexported: exportName
Expand Down
8 changes: 4 additions & 4 deletions src/Module.ts
Expand Up @@ -63,7 +63,7 @@ import {
warnDeprecation
} from './utils/error';
import { getId } from './utils/getId';
import { getOrCreate } from './utils/getOrCreate';
import { getNewSet, getOrCreate } from './utils/getOrCreate';
import { getOriginalLocation } from './utils/getOriginalLocation';
import { makeLegal } from './utils/identifierHelpers';
import {
Expand Down Expand Up @@ -164,10 +164,10 @@ function getVariableForExportNameRecursive(
}

function getAndExtendSideEffectModules(variable: Variable, module: Module): Set<Module> {
const sideEffectModules = getOrCreate(
const sideEffectModules = getOrCreate<Variable, Set<Module>>(
module.sideEffectDependenciesByVariable,
variable,
() => new Set()
getNewSet
);
let currentVariable: Variable | null = variable;
const referencedVariables = new Set([currentVariable]);
Expand Down Expand Up @@ -613,7 +613,7 @@ export default class Module {
getOrCreate(
importerForSideEffects.sideEffectDependenciesByVariable,
variable,
() => new Set()
getNewSet
).add(this);
setAlternativeExporterIfCyclic(variable, importerForSideEffects, this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ast/utils/PathTracker.ts
@@ -1,4 +1,4 @@
import { getOrCreate } from '../../utils/getOrCreate';
import { getNewSet, getOrCreate } from '../../utils/getOrCreate';
import type { Entity } from '../Entity';

export const UnknownKey = Symbol('Unknown Key');
Expand Down Expand Up @@ -98,7 +98,7 @@ export class DiscriminatedPathTracker {
currentPaths[pathSegment] ||
Object.create(null, { [EntitiesKey]: { value: new Map<unknown, Set<Entity>>() } });
}
const trackedEntities = getOrCreate(currentPaths[EntitiesKey], discriminator, () => new Set());
const trackedEntities = getOrCreate(currentPaths[EntitiesKey], discriminator, getNewSet);
if (trackedEntities.has(entity)) return true;
trackedEntities.add(entity);
return false;
Expand Down

0 comments on commit f368adf

Please sign in to comment.