From a3be38127c87ef8827a8dc3561caca3866de85d2 Mon Sep 17 00:00:00 2001 From: lmittmann <3458786+lmittmann@users.noreply.github.com> Date: Thu, 2 Nov 2023 07:54:28 +0100 Subject: [PATCH] core/vm: performance tweak of `OpCode.String()` (#28453) make `opCodeToString` a `[256]string` array Co-authored-by: lmittmann --- core/vm/opcodes.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go index a11cf05a1..c7a3a163b 100644 --- a/core/vm/opcodes.go +++ b/core/vm/opcodes.go @@ -224,8 +224,7 @@ const ( SELFDESTRUCT OpCode = 0xff ) -// Since the opcodes aren't all in order we can't use a regular slice. -var opCodeToString = map[OpCode]string{ +var opCodeToString = [256]string{ // 0x0 range - arithmetic ops. STOP: "STOP", ADD: "ADD", @@ -399,12 +398,10 @@ var opCodeToString = map[OpCode]string{ } func (op OpCode) String() string { - str := opCodeToString[op] - if len(str) == 0 { - return fmt.Sprintf("opcode %#x not defined", int(op)) + if s := opCodeToString[op]; s != "" { + return s } - - return str + return fmt.Sprintf("opcode %#x not defined", int(op)) } var stringToOp = map[string]OpCode{