From fe0ec91636b89c3293b6cb629b0accfc84b838da Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 15 Oct 2020 16:10:48 +0300 Subject: [PATCH] vm: optimize reversing items on the stack Before: BenchmarkOpcodes/REVERSE3/null-8 10000 335 ns/op BenchmarkOpcodes/REVERSE3/integer-8 10000 355 ns/op BenchmarkOpcodes/REVERSE3/big_bytes-8 10000 344 ns/op BenchmarkOpcodes/REVERSE4/null-8 10000 353 ns/op BenchmarkOpcodes/REVERSE4/integer-8 10000 336 ns/op BenchmarkOpcodes/REVERSE4/big_bytes-8 10000 324 ns/op BenchmarkOpcodes/REVERSEN/5/null-8 10000 350 ns/op BenchmarkOpcodes/REVERSEN/5/integer-8 10000 358 ns/op BenchmarkOpcodes/REVERSEN/5/big_bytes-8 10000 351 ns/op BenchmarkOpcodes/REVERSEN/1024/null-8 10000 982413 ns/op BenchmarkOpcodes/REVERSEN/1024/integer-8 10000 994926 ns/op BenchmarkOpcodes/REVERSEN/1024/big_bytes-8 10000 992951 ns/op After: BenchmarkOpcodes/REVERSE3/null-8 10000 241 ns/op BenchmarkOpcodes/REVERSE3/integer-8 10000 255 ns/op BenchmarkOpcodes/REVERSE3/big_bytes-8 10000 249 ns/op BenchmarkOpcodes/REVERSE4/null-8 10000 249 ns/op BenchmarkOpcodes/REVERSE4/integer-8 10000 267 ns/op BenchmarkOpcodes/REVERSE4/big_bytes-8 10000 292 ns/op BenchmarkOpcodes/REVERSEN/5/null-8 10000 337 ns/op BenchmarkOpcodes/REVERSEN/5/integer-8 10000 327 ns/op BenchmarkOpcodes/REVERSEN/5/big_bytes-8 10000 293 ns/op BenchmarkOpcodes/REVERSEN/1024/null-8 10000 5414 ns/op BenchmarkOpcodes/REVERSEN/1024/integer-8 10000 5487 ns/op BenchmarkOpcodes/REVERSEN/1024/big_bytes-8 10000 5609 ns/op --- pkg/vm/stack.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index 62f45022a..1b1130f53 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -351,10 +351,11 @@ func (s *Stack) ReverseTop(n int) error { return nil } - for i, j := 0, n-1; i < j; { - s.swap(i, j) - i++ - j-- + a, b := s.Peek(0), s.Peek(n-1) + for i := 0; i < n/2; i++ { + a.value, b.value = b.value, a.value + a = a.Next() + b = b.Prev() } return nil }