From 064cc8e7c37e825d80628404e08fc9cd87fcc1a1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 28 Sep 2015 12:56:25 -0500 Subject: [PATCH] txscript: Optimize DisasmString function. This commit modifies the DisasmString function to use a bytes buffer for constructing the disassembled string instead of naive string concatenation. This makes a huge difference when disassembling scripts with large numbers of opcodes. --- txscript/script.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/txscript/script.go b/txscript/script.go index a9c74939..1b0d6466 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -198,18 +198,19 @@ func unparseScript(pops []parsedOpcode) ([]byte, error) { // appended. In addition, the reason the script failed to parse is returned // if the caller wants more information about the failure. func DisasmString(buf []byte) (string, error) { - disbuf := "" + var disbuf bytes.Buffer opcodes, err := parseScript(buf) for _, pop := range opcodes { - disbuf += pop.print(true) + " " + disbuf.WriteString(pop.print(true)) + disbuf.WriteByte(' ') } - if disbuf != "" { - disbuf = disbuf[:len(disbuf)-1] + if disbuf.Len() > 0 { + disbuf.Truncate(disbuf.Len() - 1) } if err != nil { - disbuf += "[error]" + disbuf.WriteString("[error]") } - return disbuf, err + return disbuf.String(), err } // removeOpcode will remove any opcode matching ``opcode'' from the opcode