fee: use array for opcodes
Use less memory and have faster access. name old time/op new time/op delta Opcode1-8 22.4ns ± 6% 3.0ns ± 6% -86.63% (p=0.000 n=10+10)
This commit is contained in:
parent
dfc514eda0
commit
3c1325035e
2 changed files with 21 additions and 2 deletions
|
@ -8,12 +8,12 @@ import (
|
||||||
func Opcode(base int64, opcodes ...opcode.Opcode) int64 {
|
func Opcode(base int64, opcodes ...opcode.Opcode) int64 {
|
||||||
var result int64
|
var result int64
|
||||||
for _, op := range opcodes {
|
for _, op := range opcodes {
|
||||||
result += coefficients[op]
|
result += int64(coefficients[op])
|
||||||
}
|
}
|
||||||
return result * base
|
return result * base
|
||||||
}
|
}
|
||||||
|
|
||||||
var coefficients = map[opcode.Opcode]int64{
|
var coefficients = [256]uint16{
|
||||||
opcode.PUSHINT8: 1 << 0,
|
opcode.PUSHINT8: 1 << 0,
|
||||||
opcode.PUSHINT16: 1 << 0,
|
opcode.PUSHINT16: 1 << 0,
|
||||||
opcode.PUSHINT32: 1 << 0,
|
opcode.PUSHINT32: 1 << 0,
|
||||||
|
|
19
pkg/core/fee/opcode_test.go
Normal file
19
pkg/core/fee/opcode_test.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package fee
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
||||||
|
)
|
||||||
|
|
||||||
|
const feeFactor = 30
|
||||||
|
|
||||||
|
// The most common Opcode() use case is to get price for single opcode.
|
||||||
|
func BenchmarkOpcode1(t *testing.B) {
|
||||||
|
// Just so that we don't always test the same opcode.
|
||||||
|
script := []opcode.Opcode{opcode.NOP, opcode.ADD, opcode.SYSCALL, opcode.APPEND}
|
||||||
|
l := len(script)
|
||||||
|
for n := 0; n < t.N; n++ {
|
||||||
|
_ = Opcode(feeFactor, script[n%l])
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue