From e59b4706067586dc777c464ca4fbf3aaf703504b Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Thu, 3 Nov 2022 15:58:32 -0400 Subject: [PATCH] Cache `getCallableByName` (#739) --- src/Scope.ts | 28 +++++++++++++++++++--------- src/files/BrsFile.spec.ts | 6 +++--- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Scope.ts b/src/Scope.ts index e5010a06a..45ea110d8 100644 --- a/src/Scope.ts +++ b/src/Scope.ts @@ -4,7 +4,7 @@ import { CompletionItemKind } from 'vscode-languageserver'; import chalk from 'chalk'; import type { DiagnosticInfo } from './DiagnosticMessages'; import { DiagnosticMessages } from './DiagnosticMessages'; -import type { CallableContainer, BsDiagnostic, FileReference, BscFile, CallableContainerMap, FileLink } from './interfaces'; +import type { CallableContainer, BsDiagnostic, FileReference, BscFile, CallableContainerMap, FileLink, Callable } from './interfaces'; import type { Program } from './Program'; import { BsClassValidator } from './validators/ClassValidator'; import type { NamespaceStatement, FunctionStatement, ClassStatement, EnumStatement, InterfaceStatement, EnumMemberStatement, ConstStatement } from './parser/Statement'; @@ -488,15 +488,25 @@ export class Scope { * If there are overridden callables with the same name, the closest callable to this scope is returned */ public getCallableByName(name: string) { - let lowerName = name.toLowerCase(); - let callables = this.getAllCallables(); - for (let callable of callables) { - const callableName = callable.callable.getName(ParseMode.BrighterScript); - // Split by `.` and check the last term to consider namespaces. - if (callableName.toLowerCase() === lowerName || callableName.split('.').pop()?.toLowerCase() === lowerName) { - return callable.callable; + return this.getCallableMap().get( + name.toLowerCase() + ); + } + + public getCallableMap() { + return this.cache.getOrAdd('callableMap', () => { + const result = new Map(); + for (let callable of this.getAllCallables()) { + const callableName = callable.callable.getName(ParseMode.BrighterScript)?.toLowerCase(); + result.set(callableName, callable.callable); + result.set( + // Split by `.` and check the last term to consider namespaces. + callableName.split('.').pop()?.toLowerCase(), + callable.callable + ); } - } + return result; + }); } /** diff --git a/src/files/BrsFile.spec.ts b/src/files/BrsFile.spec.ts index cddb07581..85af3f02b 100644 --- a/src/files/BrsFile.spec.ts +++ b/src/files/BrsFile.spec.ts @@ -2011,14 +2011,14 @@ describe('BrsFile', () => { ' The main function ' sub main() - log("hello") + writeToLog("hello") end sub ' ' Prints a message to the log. ' Works with *markdown* **content** ' - sub log(message as string) + sub writeToLog(message as string) print message end sub `); @@ -2028,7 +2028,7 @@ describe('BrsFile', () => { program.getHover(file.srcPath, Position.create(5, 22))[0].contents ).to.equal([ '```brightscript', - 'sub log(message as string) as void', + 'sub writeToLog(message as string) as void', '```', '***', '',