summaryrefslogtreecommitdiff
path: root/vm
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2026-01-23 11:16:12 +0100
committerMarc Vertes <mvertes@free.fr>2026-01-23 11:16:12 +0100
commit2837dabf7818666a9366d659d2da3b9055140740 (patch)
tree3d4fce18fe750044b519e571845d4b476f958656 /vm
parentabd8581bac36018be2f090e05fdd00ea74f6ca4b (diff)
feat: make Next iterator instruction faster and more efficient
Branching control is delegated directly to the Next instruction, which now takes the location of loop exit as first argument. It avoids the use of JumpFalse, plus the stack storage for the condition.
Diffstat (limited to 'vm')
-rw-r--r--vm/vm.go13
1 files changed, 7 insertions, 6 deletions
diff --git a/vm/vm.go b/vm/vm.go
index f6dc6fc..7ab5a25 100644
--- a/vm/vm.go
+++ b/vm/vm.go
@@ -108,8 +108,8 @@ func (m *Machine) Run() (err error) {
defer func() { m.mem, m.ip, m.fp, m.ic = mem, ip, fp, ic }()
for {
- sp = len(mem) // stack pointer
- c := m.code[ip]
+ sp = len(mem) // stack pointer
+ c := m.code[ip] // current instruction
if debug {
log.Printf("ip:%-3d sp:%-3d fp:%-3d op:[%-20v] mem:%v\n", ip, sp, fp, c, Vstring(mem))
}
@@ -247,11 +247,12 @@ func (m *Machine) Run() (err error) {
case Negate:
mem[sp-1] = ValueOf(-mem[sp-1].Int())
case Next:
- v, ok := mem[sp-2].Interface().(func() (reflect.Value, bool))()
- if ok {
- mem[c.Arg[0]].Set(v)
+ if v, ok := mem[sp-2].Interface().(func() (reflect.Value, bool))(); ok {
+ mem[c.Arg[1]].Set(v)
+ } else {
+ ip += c.Arg[0]
+ continue
}
- mem = append(mem, ValueOf(ok))
case Not:
mem[sp-1] = ValueOf(!mem[sp-1].Bool())
case Pop: