From a63edcd2dc3ef795712cf4422ee7dadc2bb99667 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Wed, 25 Sep 2013 17:38:20 +0100 Subject: [PATCH] preallocate a few arrays. When we do append loops, make an educated guess as to the size and make an array with that capacity to avoid extra copying. Doesn't affect the speed of the tests, over 4 runs the difference was lost in the noise. --- opcode.go | 16 ++++++++++------ script.go | 8 ++++---- test_coverage.txt | 10 +++++----- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/opcode.go b/opcode.go index ac7feef5..bb32eeb9 100644 --- a/opcode.go +++ b/opcode.go @@ -688,7 +688,15 @@ func (pop *parsedOpcode) print(oneline bool) string { } func (pop *parsedOpcode) bytes() []byte { - retbytes := []byte{pop.opcode.value} + var retbytes []byte + if pop.opcode.length > 0 { + retbytes = make([]byte, 1, pop.opcode.length) + } else { + retbytes = make([]byte, 1, 1 + len(pop.data) - + pop.opcode.length) + } + + retbytes[0] = pop.opcode.value if pop.opcode.length == 1 { return retbytes } @@ -708,11 +716,7 @@ func (pop *parsedOpcode) bytes() []byte { } } - for i := range pop.data { - retbytes = append(retbytes, pop.data[i]) - } - - return retbytes + return append(retbytes, pop.data...) } // opcode implementation functions from here diff --git a/script.go b/script.go index 581cf1eb..a74d19ef 100644 --- a/script.go +++ b/script.go @@ -274,7 +274,7 @@ func parseScript(script []byte) ([]parsedOpcode, error) { // template list for testing purposes. On error we return the list of parsed // opcodes so far. func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpcode, error) { - retScript := []parsedOpcode{} + retScript := make([]parsedOpcode, 0, len(script)) for i := 0; i < len(script); { instr := script[i] op, ok := opcodemap[instr] @@ -329,7 +329,7 @@ func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpc // unparseScript reversed the action of parseScript and returns the // parsedOpcodes as a list of bytes func unparseScript(pops []parsedOpcode) []byte { - script := []byte{} + script := make([]byte, 0, len(pops)) for _, pop := range pops { script = append(script, pop.bytes()...) } @@ -574,7 +574,7 @@ func (s *Script) subScript() []parsedOpcode { // removeOpcode will remove any opcode matching ``opcode'' from the opcode // stream in pkscript func removeOpcode(pkscript []parsedOpcode, opcode byte) []parsedOpcode { - retScript := []parsedOpcode{} + retScript := make([]parsedOpcode, 0, len(pkscript)) for _, pop := range pkscript { if pop.opcode.value != opcode { retScript = append(retScript, pop) @@ -586,7 +586,7 @@ func removeOpcode(pkscript []parsedOpcode, opcode byte) []parsedOpcode { // removeOpcodeByData will return the pkscript minus any opcodes that would // push the data in ``data'' to the stack. func removeOpcodeByData(pkscript []parsedOpcode, data []byte) []parsedOpcode { - retScript := []parsedOpcode{} + retScript := make([]parsedOpcode, 0, len(pkscript)) for _, pop := range pkscript { if !bytes.Equal(pop.data, data) { retScript = append(retScript, pop) diff --git a/test_coverage.txt b/test_coverage.txt index a9469f3e..84fc677d 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -2,13 +2,13 @@ github.com/conformal/btcscript/address.go scriptToAddressTemplate 100.00% (58/58) github.com/conformal/btcscript/script.go Script.Step 100.00% (37/37) github.com/conformal/btcscript/script.go parseScriptTemplate 100.00% (30/30) -github.com/conformal/btcscript/script.go NewScript 100.00% (19/19) +github.com/conformal/btcscript/script.go NewScript 100.00% (18/18) github.com/conformal/btcscript/stack.go asInt 100.00% (18/18) github.com/conformal/btcscript/stack.go fromInt 100.00% (14/14) +github.com/conformal/btcscript/opcode.go parsedOpcode.bytes 100.00% (14/14) github.com/conformal/btcscript/opcode.go opcodeWithin 100.00% (13/13) -github.com/conformal/btcscript/script.go GetPreciseSigOpCount 100.00% (13/13) github.com/conformal/btcscript/script.go isMultiSig 100.00% (13/13) -github.com/conformal/btcscript/opcode.go parsedOpcode.bytes 100.00% (12/12) +github.com/conformal/btcscript/script.go GetPreciseSigOpCount 100.00% (13/13) github.com/conformal/btcscript/stack.go Stack.nipN 100.00% (12/12) github.com/conformal/btcscript/opcode.go parsedOpcode.print 100.00% (12/12) github.com/conformal/btcscript/opcode.go opcodeNotIf 100.00% (11/11) @@ -138,9 +138,9 @@ github.com/conformal/btcscript/opcode.go opcodeCheckMultiSig 98.21% (55/56) github.com/conformal/btcscript/opcode.go opcodeCheckSig 96.15% (25/26) github.com/conformal/btcscript/script.go Script.CheckErrorCondition 78.57% (11/14) github.com/conformal/btcscript/opcode.go opcodeCheckSigVerify 75.00% (3/4) -github.com/conformal/btcscript/script.go Script.calcScriptHash 71.43% (25/35) +github.com/conformal/btcscript/script.go Script.calcScriptHash 64.10% (25/39) github.com/conformal/btcscript/script.go Script.Execute 44.44% (8/18) github.com/conformal/btcscript/log.go SetLogWriter 0.00% (0/7) github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1) -github.com/conformal/btcscript -------------------------- 96.34% (896/930) +github.com/conformal/btcscript -------------------------- 95.94% (897/935)