Skip to content

Commit

Permalink
Update most usages of Map<K, true> to Set
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Jun 19, 2020
1 parent ee22092 commit 7743935
Show file tree
Hide file tree
Showing 26 changed files with 201 additions and 200 deletions.
52 changes: 25 additions & 27 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ts {
/**
* The map has key by source file's path that has been changed
*/
changedFilesSet?: ReadonlyMap<string, true>;
changedFilesSet?: ReadonlySet<string>;
/**
* Set of affected files being iterated
*/
Expand All @@ -47,7 +47,7 @@ namespace ts {
/**
* True if the semantic diagnostics were copied from the old state
*/
semanticDiagnosticsFromOldState?: Map<string, true>;
semanticDiagnosticsFromOldState?: Set<string>;
/**
* program corresponding to this state
*/
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace ts {
/**
* The map has key by source file's path that has been changed
*/
changedFilesSet: Map<string, true>;
changedFilesSet: Set<string>;
/**
* Set of affected files being iterated
*/
Expand All @@ -116,15 +116,15 @@ namespace ts {
/**
* Already seen affected files
*/
seenAffectedFiles: Map<string, true> | undefined;
seenAffectedFiles: Set<string> | undefined;
/**
* whether this program has cleaned semantic diagnostics cache for lib files
*/
cleanedDiagnosticsOfLibFiles?: boolean;
/**
* True if the semantic diagnostics were copied from the old state
*/
semanticDiagnosticsFromOldState?: Map<string, true>;
semanticDiagnosticsFromOldState?: Set<string>;
/**
* program corresponding to this state
*/
Expand Down Expand Up @@ -159,9 +159,9 @@ namespace ts {
programEmitComplete?: true;
}

function hasSameKeys<T, U>(map1: ReadonlyMap<string, T> | undefined, map2: ReadonlyMap<string, U> | undefined): boolean {
function hasSameKeys(map1: ReadonlyCollection<string> | undefined, map2: ReadonlyCollection<string> | undefined): boolean {
// Has same size and every key is present in both maps
return map1 as ReadonlyMap<string, T | U> === map2 || map1 !== undefined && map2 !== undefined && map1.size === map2.size && !forEachKey(map1, key => !map2.has(key));
return map1 === map2 || map1 !== undefined && map2 !== undefined && map1.size === map2.size && !forEachKey(map1, key => !map2.has(key));
}

/**
Expand All @@ -176,7 +176,7 @@ namespace ts {
if (!outFile(compilerOptions)) {
state.semanticDiagnosticsPerFile = createMap<readonly Diagnostic[]>();
}
state.changedFilesSet = createMap<true>();
state.changedFilesSet = new Set();

const useOldState = BuilderState.canReuseOldState(state.referencedMap, oldState);
const oldCompilerOptions = useOldState ? oldState!.compilerOptions : undefined;
Expand All @@ -194,14 +194,12 @@ namespace ts {
}

// Copy old state's changed files set
if (changedFilesSet) {
copyEntries(changedFilesSet, state.changedFilesSet);
}
changedFilesSet?.forEach(value => state.changedFilesSet.add(value));
if (!outFile(compilerOptions) && oldState!.affectedFilesPendingEmit) {
state.affectedFilesPendingEmit = oldState!.affectedFilesPendingEmit.slice();
state.affectedFilesPendingEmitKind = cloneMapOrUndefined(oldState!.affectedFilesPendingEmitKind);
state.affectedFilesPendingEmitIndex = oldState!.affectedFilesPendingEmitIndex;
state.seenAffectedFiles = createMap();
state.seenAffectedFiles = new Set();
}
}

Expand All @@ -225,7 +223,7 @@ namespace ts {
// Referenced file was deleted in the new program
newReferences && forEachKey(newReferences, path => !state.fileInfos.has(path) && oldState!.fileInfos.has(path))) {
// Register file as changed file and do not copy semantic diagnostics, since all changed files need to be re-evaluated
state.changedFilesSet.set(sourceFilePath, true);
state.changedFilesSet.add(sourceFilePath);
}
else if (canCopySemanticDiagnostics) {
const sourceFile = newProgram.getSourceFileByPath(sourceFilePath as Path)!;
Expand All @@ -238,23 +236,23 @@ namespace ts {
if (diagnostics) {
state.semanticDiagnosticsPerFile!.set(sourceFilePath, oldState!.hasReusableDiagnostic ? convertToDiagnostics(diagnostics as readonly ReusableDiagnostic[], newProgram, getCanonicalFileName) : diagnostics as readonly Diagnostic[]);
if (!state.semanticDiagnosticsFromOldState) {
state.semanticDiagnosticsFromOldState = createMap<true>();
state.semanticDiagnosticsFromOldState = new Set();
}
state.semanticDiagnosticsFromOldState.set(sourceFilePath, true);
state.semanticDiagnosticsFromOldState.add(sourceFilePath);
}
}
});

// If the global file is removed, add all files as changed
if (useOldState && forEachEntry(oldState!.fileInfos, (info, sourceFilePath) => info.affectsGlobalScope && !state.fileInfos.has(sourceFilePath))) {
BuilderState.getAllFilesExcludingDefaultLibraryFile(state, newProgram, /*firstSourceFile*/ undefined)
.forEach(file => state.changedFilesSet.set(file.resolvedPath, true));
.forEach(file => state.changedFilesSet.add(file.resolvedPath));
}
else if (oldCompilerOptions && !outFile(compilerOptions) && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
// Add all files to affectedFilesPendingEmit since emit changed
newProgram.getSourceFiles().forEach(f => addToAffectedFilesPendingEmit(state, f.resolvedPath, BuilderFileEmit.Full));
Debug.assert(!state.seenAffectedFiles || !state.seenAffectedFiles.size);
state.seenAffectedFiles = state.seenAffectedFiles || createMap<true>();
state.seenAffectedFiles = state.seenAffectedFiles || new Set();
}

state.buildInfoEmitPending = !!state.changedFilesSet.size;
Expand Down Expand Up @@ -304,15 +302,15 @@ namespace ts {
function cloneBuilderProgramState(state: Readonly<BuilderProgramState>): BuilderProgramState {
const newState = BuilderState.clone(state) as BuilderProgramState;
newState.semanticDiagnosticsPerFile = cloneMapOrUndefined(state.semanticDiagnosticsPerFile);
newState.changedFilesSet = cloneMap(state.changedFilesSet);
newState.changedFilesSet = new Set(state.changedFilesSet);
newState.affectedFiles = state.affectedFiles;
newState.affectedFilesIndex = state.affectedFilesIndex;
newState.currentChangedFilePath = state.currentChangedFilePath;
newState.currentAffectedFilesSignatures = cloneMapOrUndefined(state.currentAffectedFilesSignatures);
newState.currentAffectedFilesExportedModulesMap = cloneMapOrUndefined(state.currentAffectedFilesExportedModulesMap);
newState.seenAffectedFiles = cloneMapOrUndefined(state.seenAffectedFiles);
newState.seenAffectedFiles = state.seenAffectedFiles && new Set(state.seenAffectedFiles);
newState.cleanedDiagnosticsOfLibFiles = state.cleanedDiagnosticsOfLibFiles;
newState.semanticDiagnosticsFromOldState = cloneMapOrUndefined(state.semanticDiagnosticsFromOldState);
newState.semanticDiagnosticsFromOldState = state.semanticDiagnosticsFromOldState && new Set(state.semanticDiagnosticsFromOldState);
newState.program = state.program;
newState.compilerOptions = state.compilerOptions;
newState.affectedFilesPendingEmit = state.affectedFilesPendingEmit && state.affectedFilesPendingEmit.slice();
Expand Down Expand Up @@ -387,7 +385,7 @@ namespace ts {
state.affectedFiles = BuilderState.getFilesAffectedBy(state, program, nextKey.value as Path, cancellationToken, computeHash, state.currentAffectedFilesSignatures, state.currentAffectedFilesExportedModulesMap);
state.currentChangedFilePath = nextKey.value as Path;
state.affectedFilesIndex = 0;
state.seenAffectedFiles = state.seenAffectedFiles || createMap<true>();
state.seenAffectedFiles = state.seenAffectedFiles || new Set();
}
}

Expand Down Expand Up @@ -529,7 +527,7 @@ namespace ts {
}

Debug.assert(!!state.currentAffectedFilesExportedModulesMap);
const seenFileAndExportsOfFile = createMap<true>();
const seenFileAndExportsOfFile = new Set<string>();
// Go through exported modules from cache first
// If exported modules has path, all files referencing file exported from are affected
if (forEachEntry(state.currentAffectedFilesExportedModulesMap, (exportedModules, exportedFromPath) =>
Expand All @@ -551,7 +549,7 @@ namespace ts {
/**
* Iterate on files referencing referencedPath
*/
function forEachFilesReferencingPath(state: BuilderProgramState, referencedPath: Path, seenFileAndExportsOfFile: Map<string, true>, fn: (state: BuilderProgramState, filePath: Path) => boolean) {
function forEachFilesReferencingPath(state: BuilderProgramState, referencedPath: Path, seenFileAndExportsOfFile: Set<string>, fn: (state: BuilderProgramState, filePath: Path) => boolean) {
return forEachEntry(state.referencedMap!, (referencesInFile, filePath) =>
referencesInFile.has(referencedPath) && forEachFileAndExportsOfFile(state, filePath as Path, seenFileAndExportsOfFile, fn)
);
Expand All @@ -560,8 +558,8 @@ namespace ts {
/**
* fn on file and iterate on anything that exports this file
*/
function forEachFileAndExportsOfFile(state: BuilderProgramState, filePath: Path, seenFileAndExportsOfFile: Map<string, true>, fn: (state: BuilderProgramState, filePath: Path) => boolean): boolean {
if (!addToSeen(seenFileAndExportsOfFile, filePath)) {
function forEachFileAndExportsOfFile(state: BuilderProgramState, filePath: Path, seenFileAndExportsOfFile: Set<string>, fn: (state: BuilderProgramState, filePath: Path) => boolean): boolean {
if (!tryAddToSet(seenFileAndExportsOfFile, filePath)) {
return false;
}

Expand Down Expand Up @@ -618,7 +616,7 @@ namespace ts {
state.programEmitComplete = true;
}
else {
state.seenAffectedFiles!.set((affected as SourceFile).resolvedPath, true);
state.seenAffectedFiles!.add((affected as SourceFile).resolvedPath);
if (emitKind !== undefined) {
(state.seenEmittedFiles || (state.seenEmittedFiles = createMap())).set((affected as SourceFile).resolvedPath, emitKind);
}
Expand Down Expand Up @@ -1143,7 +1141,7 @@ namespace ts {
// template is undefined, and instead will just exit the loop.
for (const key in mapLike) {
if (hasProperty(mapLike, key)) {
map.set(toPath(key), arrayToSet(mapLike[key], toPath));
map.set(toPath(key), new Set(mapLike[key].map(toPath)));
}
}
return map;
Expand Down
24 changes: 12 additions & 12 deletions src/compiler/builderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace ts {
* That means hence forth these files are assumed to have
* no change in their signature for this version of the program
*/
hasCalledUpdateShapeSignature: Map<string, true>;
hasCalledUpdateShapeSignature: Set<string>;
/**
* Cache of all files excluding default library file for the current program
*/
Expand All @@ -73,7 +73,7 @@ namespace ts {
/**
* Referenced files with values for the keys as referenced file's path to be true
*/
export type ReferencedSet = ReadonlyMap<string, true>;
export type ReferencedSet = ReadonlySet<string>;
/**
* Compute the hash to store the shape of the file
*/
Expand Down Expand Up @@ -113,8 +113,8 @@ namespace ts {
/**
* Gets the referenced files for a file from the program with values for the keys as referenced file's path to be true
*/
function getReferencedFiles(program: Program, sourceFile: SourceFile, getCanonicalFileName: GetCanonicalFileName): Map<string, true> | undefined {
let referencedFiles: Map<string, true> | undefined;
function getReferencedFiles(program: Program, sourceFile: SourceFile, getCanonicalFileName: GetCanonicalFileName): Set<string> | undefined {
let referencedFiles: Set<string> | undefined;

// We need to use a set here since the code can contain the same import twice,
// but that will only be one dependency.
Expand Down Expand Up @@ -186,9 +186,9 @@ namespace ts {

function addReferencedFile(referencedPath: Path) {
if (!referencedFiles) {
referencedFiles = createMap<true>();
referencedFiles = new Set<string>();
}
referencedFiles.set(referencedPath, true);
referencedFiles.add(referencedPath);
}
}

Expand All @@ -206,7 +206,7 @@ namespace ts {
const fileInfos = createMap<FileInfo>();
const referencedMap = newProgram.getCompilerOptions().module !== ModuleKind.None ? createMap<ReferencedSet>() : undefined;
const exportedModulesMap = referencedMap ? createMap<ReferencedSet>() : undefined;
const hasCalledUpdateShapeSignature = createMap<true>();
const hasCalledUpdateShapeSignature = new Set<string>();
const useOldState = canReuseOldState(referencedMap, oldState);

// Create the reference map, and set the file infos
Expand Down Expand Up @@ -258,7 +258,7 @@ namespace ts {
fileInfos,
referencedMap: cloneMapOrUndefined(state.referencedMap),
exportedModulesMap: cloneMapOrUndefined(state.exportedModulesMap),
hasCalledUpdateShapeSignature: cloneMap(state.hasCalledUpdateShapeSignature),
hasCalledUpdateShapeSignature: new Set(state.hasCalledUpdateShapeSignature),
};
}

Expand Down Expand Up @@ -298,7 +298,7 @@ namespace ts {

export function updateSignatureOfFile(state: BuilderState, signature: string | undefined, path: Path) {
state.fileInfos.get(path)!.signature = signature;
state.hasCalledUpdateShapeSignature.set(path, true);
state.hasCalledUpdateShapeSignature.add(path);
}

/**
Expand Down Expand Up @@ -365,16 +365,16 @@ namespace ts {
return;
}

let exportedModules: Map<string, true> | undefined;
let exportedModules: Set<string> | undefined;
exportedModulesFromDeclarationEmit.forEach(symbol => addExportedModule(getReferencedFileFromImportedModuleSymbol(symbol)));
exportedModulesMapCache.set(sourceFile.resolvedPath, exportedModules || false);

function addExportedModule(exportedModulePath: Path | undefined) {
if (exportedModulePath) {
if (!exportedModules) {
exportedModules = createMap<true>();
exportedModules = new Set();
}
exportedModules.set(exportedModulePath, true);
exportedModules.add(exportedModulePath);
}
}
}
Expand Down
Loading

0 comments on commit 7743935

Please sign in to comment.