From 2837dabf7818666a9366d659d2da3b9055140740 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 23 Jan 2026 11:16:12 +0100 Subject: 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. --- vm/vm.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'vm/vm.go') 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: -- cgit v1.2.3