From 27a01c7759e66933d61abe20fb8437a96279caa7 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 15 Oct 2020 16:12:03 +0300 Subject: [PATCH] vm: optimize stack traversal on exception handling Before: BenchmarkOpcodes/THROW/0/1-8 10000 506 ns/op BenchmarkOpcodes/THROW/0/16-8 10000 524 ns/op BenchmarkOpcodes/THROW/255/0-8 10000 49363 ns/op BenchmarkOpcodes/THROW/1023/0-8 10000 1628480 ns/op After: BenchmarkOpcodes/THROW/0/1-8 10000 575 ns/op BenchmarkOpcodes/THROW/0/16-8 10000 516 ns/op BenchmarkOpcodes/THROW/255/0-8 10000 8290 ns/op BenchmarkOpcodes/THROW/1023/0-8 10000 34605 ns/op --- pkg/vm/vm.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 33be7df7b..a39c27365 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -1521,7 +1521,8 @@ func (v *VM) calcJumpOffset(ctx *Context, parameter []byte) (int, int, error) { func (v *VM) handleException() { pop := 0 - ictx := v.istack.Peek(0).Value().(*Context) + ictxv := v.istack.Peek(0) + ictx := ictxv.Value().(*Context) for ictx != nil { e := ictx.tryStack.Peek(0) for e != nil { @@ -1547,7 +1548,8 @@ func (v *VM) handleException() { return } pop++ - ictx = v.istack.Peek(pop).Value().(*Context) + ictxv = ictxv.Next() + ictx = ictxv.Value().(*Context) } }