Skip to content

Commit

Permalink
performance(editor) Cache Execution Scope (#6076)
Browse files Browse the repository at this point in the history
- `createExecutionScope` caches the scopes by filename and the project
contents as a whole.
  • Loading branch information
seanparsons authored Jul 15, 2024
1 parent 91a17fa commit 29617cb
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export interface ExecutionScope {
requireResult: MapLike<any>
}

let lastSeenProjectContents: ProjectContentTreeRoot | null
let executionScopeCache: { [filename: string]: ExecutionScope } = {}

export function createExecutionScope(
filePath: string,
customRequire: (importOrigin: string, toImport: string) => any,
Expand All @@ -70,11 +73,18 @@ export function createExecutionScope(
updateInvalidatedPaths: DomWalkerInvalidatePathsCtxData,
shouldIncludeCanvasRootInTheSpy: boolean,
editedText: ElementPath | null,
): {
scope: MapLike<any>
topLevelJsxComponents: Map<string | null, UtopiaJSXComponent>
requireResult: MapLike<any>
} {
): ExecutionScope {
// Return something from the cache as appropriate.
if (lastSeenProjectContents === projectContents) {
if (filePath in executionScopeCache) {
return executionScopeCache[filePath]
}
} else {
lastSeenProjectContents = projectContents
executionScopeCache = {}
}

// Build the scope.
const { topLevelElements, imports, jsxFactoryFunction, combinedTopLevelArbitraryBlock } =
getParseSuccessForFilePath(filePath, projectContents)
const requireResult: MapLike<any> = importResultFromImports(filePath, imports, customRequire)
Expand Down Expand Up @@ -236,11 +246,13 @@ export function createExecutionScope(
},
})

return {
const toReturn = {
scope: executionScope,
topLevelJsxComponents: topLevelJsxComponents,
requireResult: requireResult,
}
executionScopeCache[filePath] = toReturn
return toReturn
}

const emptyHighlightBoundsResult = { code: '', highlightBounds: null }
Expand Down

0 comments on commit 29617cb

Please sign in to comment.