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.
This commit is contained in:
Dave Collins 2015-09-28 12:56:25 -05:00
parent 79aac01b02
commit 064cc8e7c3

View file

@ -198,18 +198,19 @@ func unparseScript(pops []parsedOpcode) ([]byte, error) {
// appended. In addition, the reason the script failed to parse is returned // appended. In addition, the reason the script failed to parse is returned
// if the caller wants more information about the failure. // if the caller wants more information about the failure.
func DisasmString(buf []byte) (string, error) { func DisasmString(buf []byte) (string, error) {
disbuf := "" var disbuf bytes.Buffer
opcodes, err := parseScript(buf) opcodes, err := parseScript(buf)
for _, pop := range opcodes { for _, pop := range opcodes {
disbuf += pop.print(true) + " " disbuf.WriteString(pop.print(true))
disbuf.WriteByte(' ')
} }
if disbuf != "" { if disbuf.Len() > 0 {
disbuf = disbuf[:len(disbuf)-1] disbuf.Truncate(disbuf.Len() - 1)
} }
if err != nil { 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 // removeOpcode will remove any opcode matching ``opcode'' from the opcode