mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 09:42:22 +00:00
opcode: optimize IsValid
Map access costs much more than array access. name old time/op new time/op delta IsValid-8 17.6ns ± 2% 1.1ns ± 2% -93.68% (p=0.008 n=5+5)
This commit is contained in:
parent
3c1325035e
commit
2c2ccdca74
2 changed files with 25 additions and 2 deletions
15
pkg/vm/opcode/isvalid_test.go
Normal file
15
pkg/vm/opcode/isvalid_test.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package opcode
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// IsValid() is called for every VM instruction.
|
||||
func BenchmarkIsValid(t *testing.B) {
|
||||
// Just so that we don't always test the same opcode.
|
||||
script := []Opcode{NOP, ADD, SYSCALL, APPEND, 0xff, 0xf0}
|
||||
l := len(script)
|
||||
for n := 0; n < t.N; n++ {
|
||||
_ = IsValid(script[n%l])
|
||||
}
|
||||
}
|
|
@ -222,8 +222,16 @@ const (
|
|||
CONVERT Opcode = 0xDB
|
||||
)
|
||||
|
||||
var validCodes [256]bool
|
||||
|
||||
func init() {
|
||||
// We rely on stringer here, it has a map anyway.
|
||||
for op := range _Opcode_map {
|
||||
validCodes[int(op)] = true
|
||||
}
|
||||
}
|
||||
|
||||
// IsValid returns true if the opcode passed is valid (defined in the VM).
|
||||
func IsValid(op Opcode) bool {
|
||||
_, ok := _Opcode_map[op] // We rely on stringer here, it has a map anyway.
|
||||
return ok
|
||||
return validCodes[int(op)]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue