ProgramMemory refactor - eliminate copies of memory (#5273)

* refactor program memory for encapsulation and remove special treatment of return values

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Refactor ProgramMemory to isolate mutation

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Refactor ProgramMemory

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Generated output

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Cache the result of executing modules for their items

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Compress envs when popped

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Remove the last traces of geometry from the memory module

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* docs

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Fixups

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Frontend

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Improve Environment GC

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Fixup mock execution frontend and interpreter, misc bug fixes

Signed-off-by: Nick Cameron <nrc@ncameron.org>

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-02-12 10:22:56 +13:00
committed by GitHub
parent bc4d254297
commit 322f398049
139 changed files with 4825 additions and 8154 deletions

View File

@ -17,13 +17,14 @@ import {
emptyExecState,
ExecState,
initPromise,
KclValue,
parse,
PathToNode,
Program,
ProgramMemory,
recast,
SourceRange,
topLevelRange,
VariableMap,
} from 'lang/wasm'
import { getNodeFromPath, getSettingsAnnotation } from './queryAst'
import { codeManager, editorManager, sceneInfra } from 'lib/singletons'
@ -61,8 +62,8 @@ export class KclManager {
trivia: [],
}
private _execState: ExecState = emptyExecState()
private _programMemory: ProgramMemory = ProgramMemory.empty()
lastSuccessfulProgramMemory: ProgramMemory = ProgramMemory.empty()
private _variables: VariableMap = {}
lastSuccessfulVariables: VariableMap = {}
lastSuccessfulOperations: Operation[] = []
private _logs: string[] = []
private _errors: KCLError[] = []
@ -78,7 +79,9 @@ export class KclManager {
private _isExecutingCallback: (arg: boolean) => void = () => {}
private _astCallBack: (arg: Node<Program>) => void = () => {}
private _programMemoryCallBack: (arg: ProgramMemory) => void = () => {}
private _variablesCallBack: (arg: {
[key in string]?: KclValue | undefined
}) => void = () => {}
private _logsCallBack: (arg: string[]) => void = () => {}
private _kclErrorsCallBack: (errors: KCLError[]) => void = () => {}
private _diagnosticsCallback: (errors: Diagnostic[]) => void = () => {}
@ -97,18 +100,18 @@ export class KclManager {
this._switchedFiles = switchedFiles
}
get programMemory() {
return this._programMemory
get variables() {
return this._variables
}
// This is private because callers should be setting the entire execState.
private set programMemory(programMemory) {
this._programMemory = programMemory
this._programMemoryCallBack(programMemory)
private set variables(variables) {
this._variables = variables
this._variablesCallBack(variables)
}
private set execState(execState) {
this._execState = execState
this.programMemory = execState.memory
this.variables = execState.variables
}
get execState() {
@ -201,7 +204,7 @@ export class KclManager {
}
registerCallBacks({
setProgramMemory,
setVariables,
setAst,
setLogs,
setErrors,
@ -209,7 +212,7 @@ export class KclManager {
setIsExecuting,
setWasmInitFailed,
}: {
setProgramMemory: (arg: ProgramMemory) => void
setVariables: (arg: VariableMap) => void
setAst: (arg: Node<Program>) => void
setLogs: (arg: string[]) => void
setErrors: (errors: KCLError[]) => void
@ -217,7 +220,7 @@ export class KclManager {
setIsExecuting: (arg: boolean) => void
setWasmInitFailed: (arg: boolean) => void
}) {
this._programMemoryCallBack = setProgramMemory
this._variablesCallBack = setVariables
this._astCallBack = setAst
this._logsCallBack = setLogs
this._kclErrorsCallBack = setErrors
@ -329,6 +332,7 @@ export class KclManager {
ast,
path: codeManager.currentFilePath || undefined,
engineCommandManager: this.engineCommandManager,
isMock: false,
})
// Program was not interrupted, setup the scene
@ -385,11 +389,11 @@ export class KclManager {
this.addDiagnostics(isInterrupted ? [] : kclErrorsToDiagnostics(errors))
this.execState = execState
if (!errors.length) {
this.lastSuccessfulProgramMemory = execState.memory
this.lastSuccessfulVariables = execState.variables
this.lastSuccessfulOperations = execState.operations
}
this.ast = { ...ast }
// updateArtifactGraph relies on updated executeState/programMemory
// updateArtifactGraph relies on updated executeState/variables
this.engineCommandManager.updateArtifactGraph(execState.artifactGraph)
this._executeCallback()
if (!isInterrupted) {
@ -442,16 +446,15 @@ export class KclManager {
const { logs, errors, execState } = await executeAst({
ast: newAst,
engineCommandManager: this.engineCommandManager,
// We make sure to send an empty program memory to denote we mean mock mode.
programMemoryOverride: ProgramMemory.empty(),
isMock: true,
})
this._logs = logs
this.addDiagnostics(kclErrorsToDiagnostics(errors))
this._execState = execState
this._programMemory = execState.memory
this._variables = execState.variables
if (!errors.length) {
this.lastSuccessfulProgramMemory = execState.memory
this.lastSuccessfulVariables = execState.variables
this.lastSuccessfulOperations = execState.operations
}
}