fixes #14409; fixes #10674 VM callbacks switch to table-index seqs (#21297)

* fixes #14409; fixes#10674 VM callbacks switch to table-index seqs

* fixes package name

* reduce runtime cost
This commit is contained in:
ringabout 2023-01-27 06:49:04 +08:00 • committed by GitHub
commit 4647c7b596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 23 deletions

View file

@ -10,7 +10,7 @@
## This module contains the type definitions for the new evaluation engine.
## An instruction is 1-3 int32s in memory, it is a register based VM.
import tables
import std/[tables, strutils]
import ast, idents, options, modulegraphs, lineinfos
@ -259,7 +259,8 @@ type
traceActive*: bool
loopIterations*: int
comesFromHeuristic*: TLineInfo # Heuristic for better macro stack traces
callbacks*: seq[tuple[key: string, value: VmCallback]]
callbacks*: seq[VmCallback]
callbackIndex*: Table[string, int]
errorFlag*: string
cache*: IdentCache
config*: ConfigRef
@ -292,7 +293,7 @@ proc newCtx*(module: PSym; cache: IdentCache; g: ModuleGraph; idgen: IdGenerator
PCtx(code: @[], debug: @[],
globals: newNode(nkStmtListExpr), constants: newNode(nkStmtList), types: @[],
prc: PProc(blocks: @[]), module: module, loopIterations: g.config.maxLoopIterationsVM,
comesFromHeuristic: unknownLineInfo, callbacks: @[], errorFlag: "",
comesFromHeuristic: unknownLineInfo, callbacks: @[], callbackIndex: initTable[string, int](), errorFlag: "",
cache: cache, config: g.config, graph: g, idgen: idgen)
proc refresh*(c: PCtx, module: PSym; idgen: IdGenerator) =
@ -301,9 +302,18 @@ proc refresh*(c: PCtx, module: PSym; idgen: IdGenerator) =
c.loopIterations = c.config.maxLoopIterationsVM
c.idgen = idgen
proc reverseName(s: string): string =
result = newStringOfCap(s.len)
let y = s.split('.')
for i in 1..y.len:
result.add y[^i]
if i != y.len:
result.add '.'
proc registerCallback*(c: PCtx; name: string; callback: VmCallback): int {.discardable.} =
result = c.callbacks.len
c.callbacks.add((name, callback))
c.callbacks.add(callback)
c.callbackIndex[reverseName(name)] = result
const
firstABxInstr* = opcTJmp