Skip to content

Commit

Permalink
Merge pull request #48 from georgejecook/chore/optimize-lookups
Browse files Browse the repository at this point in the history
optimizes lookups
  • Loading branch information
georgejecook authored May 8, 2022
2 parents 19c6acf + 896d91d commit 2be3b80
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/lib/binding/BindingProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class BindingProcessor {

} else {

file.bindingClass = this.fileMap.allClasses.get(file.vmClassName);
file.bindingClass = this.fileMap.allClasses[file.vmClassName];

if (!file.bindingClass) {
addXmlBindingVMClassNotFound(file);
Expand Down
16 changes: 8 additions & 8 deletions src/lib/files/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export class File {
public nodeClasses = new Map<string, NodeClass>();

get parentXmlFile(): XmlFile | undefined {
return this.fileMap.allXMLComponentFiles.get(this.parentComponentName)?.bscFile as XmlFile;
return this.fileMap.allXMLComponentFiles[this.parentComponentName]?.bscFile as XmlFile;
}
get parentFile(): File | undefined {
return this.fileMap.allXMLComponentFiles.get(this.parentComponentName);
return this.fileMap.allXMLComponentFiles[this.parentComponentName];
}

get fileType(): FileType {
Expand Down Expand Up @@ -84,7 +84,7 @@ export class File {
bindings = bindings.concat(this.bindings);
}
if (this.parentXmlFile) {
let parentFile = this.fileMap.allFiles.get(this.parentXmlFile.pathAbsolute);
let parentFile = this.fileMap.allFiles[this.parentXmlFile.pathAbsolute];
return parentFile?.getAllParentBindings(bindings);
} else {
return bindings;
Expand All @@ -98,7 +98,7 @@ export class File {
addSetItems(ids, this.tagIds);
}
if (this.parentXmlFile) {
let parentFile = this.fileMap.allFiles.get(this.parentXmlFile.pathAbsolute);
let parentFile = this.fileMap.allFiles[this.parentXmlFile.pathAbsolute];
return parentFile?.getAllParentTagIds(ids);
} else {
return ids;
Expand All @@ -112,7 +112,7 @@ export class File {
addSetItems(ids, this.fieldIds);
}
if (this.parentXmlFile) {
let parentFile = this.fileMap.allFiles.get(this.parentXmlFile.pathAbsolute);
let parentFile = this.fileMap.allFiles[this.parentXmlFile.pathAbsolute];
return parentFile?.getAllParentFieldIds(ids);
} else {
return ids;
Expand Down Expand Up @@ -191,7 +191,7 @@ export class File {
for (let method of cs.methods) {
result[method.name.text.toLowerCase()] = true;
}
cs = cs.parentClassName ? this.fileMap.allClasses.get(cs.parentClassName.getName(ParseMode.BrighterScript).replace(/_/g, '.')) : null;
cs = cs.parentClassName ? this.fileMap.allClasses[cs.parentClassName.getName(ParseMode.BrighterScript).replace(/_/g, '.')] : null;
}

return result;
Expand All @@ -200,10 +200,10 @@ export class File {
public getParents(): ClassStatement[] {
if (!this.parents || this.parents.length === 0) {
this.parents = [];
let next = this.bindingClass.parentClassName ? this.fileMap.allClasses.get(this.bindingClass.parentClassName.getName(ParseMode.BrighterScript).replace(/_/g, '.')) : null;
let next = this.bindingClass.parentClassName ? this.fileMap.allClasses[this.bindingClass.parentClassName.getName(ParseMode.BrighterScript).replace(/_/g, '.')] : null;
while (next) {
this.parents.push(next);
next = next.parentClassName ? this.fileMap.allClasses.get(next.parentClassName.getName(ParseMode.BrighterScript).replace(/_/g, '.')) : null;
next = next.parentClassName ? this.fileMap.allClasses[next.parentClassName.getName(ParseMode.BrighterScript).replace(/_/g, '.')] : null;
}
}

Expand Down
48 changes: 24 additions & 24 deletions src/lib/files/ProjectFileMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ import type { NodeClass } from '../node-classes/NodeClass';
export class ProjectFileMap {

constructor() {
this.allFiles = new Map<string, File>();
this.allXMLComponentFiles = new Map<string, File>();
this.allFiles = {};
this.allXMLComponentFiles = {};
this.allClassNames = new Set<string>();
}

public allClasses = new Map<string, ClassStatement>();
public allClasses: Record<string, ClassStatement> = {};
public allClassNames: Set<string>;
public allClassFiles = new Map<string, File>();
public allXMLComponentFiles: Map<string, File>;
public allFiles: Map<string, File>;
public nodeClasses = new Map<string, NodeClass>();
public nodeClassesByPath = new Map<string, NodeClass[]>();
public allClassFiles = {};
public allXMLComponentFiles: Record<string, File>;
public allFiles: Record<string, File>;
public nodeClasses: Record<string, NodeClass> = {};
public nodeClassesByPath: Record<string, NodeClass[]> = {};
public pathsByNamespace: Record<string, Record<string, boolean>> = {};

get XMLComponentNames(): string[] {
return [...this.allXMLComponentFiles.keys()];
return Object.keys(this.allXMLComponentFiles);
}

get files(): File[] {
return [...this.allFiles.values()];
return Object.values(this.allFiles);
}

get classNames(): string[] {
return [...this.allClassNames.values()];
return Object.values(this.allClassNames);
}

public validComps = new Set<string>(
Expand Down Expand Up @@ -100,10 +100,10 @@ export class ProjectFileMap {

public addXMLComponent(file: File) {
if (file.fileType === FileType.Xml) {
if (!this.allXMLComponentFiles.has(file.componentName) || file.fullPath === this.allXMLComponentFiles.get(file.componentName).fullPath) {
this.allXMLComponentFiles.set(file.componentName, file);
if (!this.allXMLComponentFiles[file.componentName] || file.fullPath === this.allXMLComponentFiles[file.componentName].fullPath) {
this.allXMLComponentFiles[file.componentName] = file;
} else {
const duplicateFile = this.allXMLComponentFiles.get(file.componentName);
const duplicateFile = this.allXMLComponentFiles[file.componentName];
addProjectFileMapErrorDuplicateXMLComp(file, duplicateFile.fullPath);
}
}
Expand All @@ -112,8 +112,8 @@ export class ProjectFileMap {
public addClass(classStatement: ClassStatement, mFile: File) {
let className = classStatement.getName(ParseMode.BrighterScript);
this.allClassNames.add(className);
this.allClassFiles.set(className, mFile);
this.allClasses.set(className, classStatement);
this.allClassFiles[className] = mFile;
this.allClasses[className] = classStatement;
mFile.classNames.add(className);
}
public addNamespaces(file: BrsFile) {
Expand All @@ -130,9 +130,9 @@ export class ProjectFileMap {
}

public removeClass(name: string) {
this.allClassNames.delete(name);
this.allClassFiles.delete(name);
this.allClasses.delete(name);
delete this.allClassNames[name];
delete this.allClassFiles[name];
delete this.allClasses[name];
}

public removeFileClasses(file: File) {
Expand All @@ -144,12 +144,12 @@ export class ProjectFileMap {

public removeFile(file: File) {
this.removeFileClasses(file);
this.allFiles.delete(file.fullPath);
delete this.allFiles[file.fullPath];
}

public getFileForClass(className: string) {
if (this.allClasses.has(className)) {
return this.allClassFiles.get(className);
if (this.allClasses[className]) {
return this.allClassFiles[className];
}
return undefined;
}
Expand All @@ -159,13 +159,13 @@ export class ProjectFileMap {
if (isBrsFile(file.bscFile)) {
this.addNamespaces(file.bscFile);
}
this.allFiles.set(file.fullPath, file);
this.allFiles[file.fullPath] = file;
}

public addBscFiles(files: Record<string, BrsFile | XmlFile>) {
for (let filePath in files) {
let bscFile = files[filePath];
let file = this.allFiles.get(bscFile.pathAbsolute);
let file = this.allFiles[bscFile.pathAbsolute];
if (file) {
file.bscFile = bscFile;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/node-classes/NodeClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,18 @@ export class NodeClass {

public getClassHierarchy(className: string, fileMap: ProjectFileMap): ClassStatement[] {
let items = [];
let parent = fileMap.allClasses.get(className);
let parent = fileMap.allClasses[className];
while (parent) {
items.push(parent);
parent = fileMap.allClasses.get(parent.parentClassName?.getName(ParseMode.BrighterScript));
parent = fileMap.allClasses[parent.parentClassName?.getName(ParseMode.BrighterScript)];
}
return items;
}

public validateBaseComponent(builder: ProgramBuilder, fileMap: ProjectFileMap) {
let comp = builder.program.getComponent(this.extendsName.toLowerCase());

if (!(comp?.file?.componentName?.text === this.extendsName || fileMap.validComps.has(this.extendsName) || fileMap.nodeClasses.has(this.extendsName))) {
if (!(comp?.file?.componentName?.text === this.extendsName || fileMap.validComps.has(this.extendsName) || fileMap.nodeClasses[this.extendsName])) {
addNodeClassNoExtendNodeFound(this.file, this.name, this.extendsName, this.annotation.range.start.line, this.annotation.range.start.character);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/lib/node-classes/NodeClassUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export default class NodeClassUtil {
}

public addFile(file: BrsFile, mFile: File) {
for (let nodeClass of this.fileMap.nodeClassesByPath.get(file.pathAbsolute) || []) {
this.fileMap.nodeClasses.delete(nodeClass.name);
mFile.nodeClasses.delete(nodeClass.name);
for (let nodeClass of this.fileMap.nodeClassesByPath[file.pathAbsolute] || []) {
delete this.fileMap.nodeClasses[nodeClass.name];
delete mFile.nodeClasses[nodeClass.name];
}
this.fileMap.nodeClassesByPath.delete(file.pathAbsolute);
delete this.fileMap.nodeClassesByPath[file.pathAbsolute];

for (let cs of file.parser.references.classStatements) {
let annotation = cs.annotations?.find((a) => a.name.toLowerCase() === 'task' || a.name.toLowerCase() === 'node');
Expand All @@ -39,7 +39,7 @@ export default class NodeClassUtil {
if (args.length < 2 || !nodeName || !extendsName) {
addNodeClassBadDeclaration(file, '', annotation.range.start.line, annotation.range.start.character + 1);
console.log(' bad class in ', file.pkgPath);
} else if (this.fileMap.nodeClasses.has(nodeName)) {
} else if (this.fileMap.nodeClasses[nodeName]) {
addNodeClassDuplicateName(file, nodeName, annotation.range.start.line, annotation.range.start.character + 1);
console.log(' duplicate node in ', file.pkgPath);
} else {
Expand All @@ -62,11 +62,11 @@ export default class NodeClassUtil {
if (isValid) {
//is valid
let nodeClass = new NodeClass(nodeType, file, cs, nodeName, extendsName, annotation, this.fileMap, lazyAnnotation !== undefined, waitInitAnnotation !== undefined);
this.fileMap.nodeClasses.set(nodeClass.generatedNodeName, nodeClass);
let nodeClasses = this.fileMap.nodeClassesByPath.get(file.pathAbsolute);
this.fileMap.nodeClasses[nodeClass.generatedNodeName] = nodeClass;
let nodeClasses = this.fileMap.nodeClassesByPath[file.pathAbsolute];
if (!nodeClasses) {
nodeClasses = [];
this.fileMap.nodeClassesByPath.set(file.pathAbsolute, nodeClasses);
this.fileMap.nodeClassesByPath[file.pathAbsolute] = nodeClasses;
}
mFile.nodeClasses.set(file.pathAbsolute, nodeClass);
nodeClasses.push(nodeClass);
Expand All @@ -93,7 +93,7 @@ export default class NodeClassUtil {
if false
? "maestro nodeclass test-utils"`;
for (let nc of [...this.fileMap.nodeClasses.values()]) {
for (let nc of Object.values(this.fileMap.nodeClasses)) {
codeText += `\n else if name = "${nc.classStatement.getName(ParseMode.BrightScript).toLowerCase()}"
'bs:disable-next-line
instance = __${nc.classStatement.getName(ParseMode.BrightScript)}_builder()
Expand Down
2 changes: 1 addition & 1 deletion src/lib/reflection/ReflectionUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class ReflectionUtil {
}

public addFile(file: BrsFile) {
let mFile = this.fileMap.allFiles.get(file.pathAbsolute);
let mFile = this.fileMap.allFiles[file.pathAbsolute];
this.fileMap.removeFileClasses(mFile);
for (let cs of file.parser.references.classStatements) {
this.fileMap.addClass(cs, mFile);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function getAlternateFileNames(fileName: string): string[] {

export function getAssociatedFile(file: BrsFile | XmlFile, fileMap: ProjectFileMap): File | undefined {
for (let filePath of getAlternateFileNames(file.pathAbsolute)) {
let mFile = fileMap.allFiles.get(filePath);
let mFile = fileMap.allFiles[filePath];
if (mFile) {
return mFile;
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export function getAllFields(fileMap: ProjectFileMap, cs: ClassStatement, vis?:
result[field.name.text.toLowerCase()] = field;
}
}
cs = cs.parentClassName ? fileMap.allClasses.get(cs.parentClassName.getName(brighterscript.ParseMode.BrighterScript).replace(/_/g, '.')) : null;
cs = cs.parentClassName ? fileMap.allClasses[cs.parentClassName.getName(brighterscript.ParseMode.BrighterScript).replace(/_/g, '.')] : null;
}

return result;
Expand All @@ -195,7 +195,7 @@ export function getAllMethods(fileMap: ProjectFileMap, cs: ClassStatement, vis?:
result[method.name.text.toLowerCase()] = method;
}
}
cs = cs.parentClassName ? fileMap.allClasses.get(cs.parentClassName.getName(brighterscript.ParseMode.BrighterScript).replace(/_/g, '.')) : null;
cs = cs.parentClassName ? fileMap.allClasses[cs.parentClassName.getName(brighterscript.ParseMode.BrighterScript).replace(/_/g, '.')] : null;
}

return result;
Expand All @@ -209,7 +209,7 @@ export function getAllAnnotations(fileMap: ProjectFileMap, cs: ClassStatement) {
result[annotation.name.toLowerCase()] = true;
}
}
cs = cs.parentClassName ? fileMap.allClasses.get(cs.parentClassName.getName(brighterscript.ParseMode.BrighterScript).replace(/_/g, '.')) : null;
cs = cs.parentClassName ? fileMap.allClasses[cs.parentClassName.getName(brighterscript.ParseMode.BrighterScript).replace(/_/g, '.')] : null;
}

return result;
Expand Down
18 changes: 9 additions & 9 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class MaestroPlugin implements CompilerPlugin {

afterFileParse(file: (BrsFile | XmlFile)): void {
// console.log('MAESTRO afp-----', file.pathAbsolute);
let mFile = this.fileMap.allFiles.get(file.pathAbsolute);
let mFile = this.fileMap.allFiles[file.pathAbsolute];
if (!mFile) {
mFile = this.fileMap.createFile(file);
}
Expand Down Expand Up @@ -226,7 +226,7 @@ export class MaestroPlugin implements CompilerPlugin {
return;
}
// console.log('MAESTRO running stf.....');
let compFile = this.fileMap.allFiles.get(file.pathAbsolute);
let compFile = this.fileMap.allFiles[file.pathAbsolute];
if (this.maestroConfig.processXMLFiles) {

if (compFile?.fileType === FileType.Xml && compFile?.vmClassName) {
Expand Down Expand Up @@ -281,7 +281,7 @@ export class MaestroPlugin implements CompilerPlugin {
if (this.maestroConfig.processXMLFiles) {
for (let filePath of [...this.dirtyCompFilePaths.values()]) {
// console.time('Validate bindings');
let file = this.fileMap.allFiles.get(filePath);
let file = this.fileMap.allFiles[filePath];
file.bscFile = this.builder.program.getFileByPathAbsolute(filePath);
file.resetDiagnostics();
this.bindingProcessor.validateBindings(file);
Expand All @@ -295,7 +295,7 @@ export class MaestroPlugin implements CompilerPlugin {

if (!this.maestroConfig.nodeClasses.buildForIDE) {
console.time('Build node classes');
for (let nc of [...this.fileMap.nodeClasses.values()]) {
for (let nc of Object.values(this.fileMap.nodeClasses)) {
nc.generateCode(this.fileFactory, this.builder.program, this.fileMap, false);
}
if (this.maestroConfig.nodeClasses.generateTestUtils) {
Expand Down Expand Up @@ -330,7 +330,7 @@ export class MaestroPlugin implements CompilerPlugin {
if (this.maestroConfig.extraValidation.doExtraValidation) {
console.time('Do additional validations');
for (let f of [...this.mFilesToValidate.values()]) {
let mFile = this.fileMap.allFiles.get(f.pathAbsolute);
let mFile = this.fileMap.allFiles[f.pathAbsolute];
if (mFile && this.shouldDoExtraValidationsOnFile(f)) {
this.checkMReferences(mFile);
this.doExtraValidations(f);
Expand All @@ -341,7 +341,7 @@ export class MaestroPlugin implements CompilerPlugin {
this.mFilesToValidate.clear();
console.time('Validate node classes');
for (let filePath of [...this.dirtyNodeClassPaths.values()]) {
for (let nc of this.fileMap.nodeClassesByPath.get(filePath)) {
for (let nc of this.fileMap.nodeClassesByPath[filePath]) {
if (this.shouldDoExtraValidationsOnFile(nc.file)) {
nc.validate();
nc.validateBaseComponent(this.builder, this.fileMap);
Expand All @@ -356,7 +356,7 @@ export class MaestroPlugin implements CompilerPlugin {
getCompFilesThatHaveFileInScope(file: BscFile): File[] {
let compFiles = [];
let lowerPath = file.pkgPath.toLowerCase();
for (let compFile of [...this.fileMap.allFiles.values()].filter((f) => f.fileType === FileType.Xml && f.vmClassName)) {
for (let compFile of Object.values(this.fileMap.allFiles).filter((f) => f.fileType === FileType.Xml && f.vmClassName)) {
let xmlFile = compFile.bscFile as XmlFile;
if (xmlFile.getAllDependencies().includes(lowerPath)) {
compFiles.push(compFile);
Expand Down Expand Up @@ -447,7 +447,7 @@ export class MaestroPlugin implements CompilerPlugin {
}
this.updateAsFunctionCalls(event.file);
}
for (let nc of this.fileMap.nodeClasses.values()) {
for (let nc of Object.values(this.fileMap.nodeClasses)) {
nc.replacePublicMFieldRefs(this.fileMap);
}
}
Expand Down Expand Up @@ -585,7 +585,7 @@ export class MaestroPlugin implements CompilerPlugin {

for (let entry of entries) {
if (isXmlFile(entry.file)) {
let mFile = this.fileMap.allFiles.get(entry.file.pathAbsolute);
let mFile = this.fileMap.allFiles[entry.file.pathAbsolute];
// eslint-disable-next-line @typescript-eslint/dot-notation
if (mFile.isValid) {
//it's a binding file
Expand Down

0 comments on commit 2be3b80

Please sign in to comment.