Skip to content

Avoid no-op export map updates #45238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/compiler/builderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ namespace ts {
*/
readonly exportedModulesMap: BuilderState.ManyToManyPathMap | undefined;

previousCache?: {
id: number,
version: number,
};

/**
* true if file version is used as signature
* This helps in delaying the calculation of the d.ts hash as version for the file till reasonable time
Expand Down Expand Up @@ -80,6 +85,7 @@ namespace ts {
}

export interface ReadonlyManyToManyPathMap {
readonly id: number;
clone(): ManyToManyPathMap;
forEach(action: (v: ReadonlySet<Path>, k: Path) => void): void;
getKeys(v: Path): ReadonlySet<Path> | undefined;
Expand All @@ -96,13 +102,18 @@ namespace ts {
}

export interface ManyToManyPathMap extends ReadonlyManyToManyPathMap {
version(): number; // Incremented each time the contents are changed
deleteKey(k: Path): boolean;
set(k: Path, v: ReadonlySet<Path>): void;
}

let manyToManyPathMapCount = 0;
export function createManyToManyPathMap(): ManyToManyPathMap {
function create(forward: ESMap<Path, ReadonlySet<Path>>, reverse: ESMap<Path, Set<Path>>, deleted: Set<Path> | undefined): ManyToManyPathMap {
let version = 0;
const map: ManyToManyPathMap = {
id: manyToManyPathMapCount++,
version: () => version,
clone: () => create(new Map(forward), new Map(reverse), deleted && new Set(deleted)),
forEach: fn => forward.forEach(fn),
getKeys: v => reverse.get(v),
Expand All @@ -121,26 +132,33 @@ namespace ts {

set.forEach(v => deleteFromMultimap(reverse, v, k));
forward.delete(k);
version++;
return true;
},
set: (k, vSet) => {
deleted?.delete(k);
let changed = !!deleted?.delete(k);

const existingVSet = forward.get(k);
forward.set(k, vSet);

existingVSet?.forEach(v => {
if (!vSet.has(v)) {
changed = true;
deleteFromMultimap(reverse, v, k);
}
});

vSet.forEach(v => {
if (!existingVSet?.has(v)) {
changed = true;
addToMultimap(reverse, v, k);
}
});

if (changed) {
version++;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's vitally important that this not miss a case where the map has actually changed - please double-check.

}

return map;
},
};
Expand Down Expand Up @@ -475,6 +493,22 @@ namespace ts {
export function updateExportedFilesMapFromCache(state: BuilderState, exportedModulesMapCache: ManyToManyPathMap | undefined) {
if (exportedModulesMapCache) {
Debug.assert(!!state.exportedModulesMap);

const cacheId = exportedModulesMapCache.id;
const cacheVersion = exportedModulesMapCache.version();
if (state.previousCache) {
if (state.previousCache.id === cacheId && state.previousCache.version === cacheVersion) {
// If this is the same cache at the same version as last time this BuilderState
// was updated, there's no need to update again
return;
}
state.previousCache.id = cacheId;
state.previousCache.version = cacheVersion;
}
else {
state.previousCache = { id: cacheId, version: cacheVersion };
}

exportedModulesMapCache.deletedKeys()?.forEach(path => state.exportedModulesMap!.deleteKey(path));
exportedModulesMapCache.forEach((exportedModules, path) => state.exportedModulesMap!.set(path, exportedModules));
}
Expand Down