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
This commit is contained in:
Roman Khimov 2020-10-15 16:10:48 +03:00
parent 7808ab2dde
commit fe0ec91636

View file

@ -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
}