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.
This commit is contained in:
Owain G. Ainsworth 2013-09-25 17:38:20 +01:00
parent 87e976a732
commit a63edcd2dc
3 changed files with 19 additions and 15 deletions

View file

@ -688,7 +688,15 @@ func (pop *parsedOpcode) print(oneline bool) string {
} }
func (pop *parsedOpcode) bytes() []byte { 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 { if pop.opcode.length == 1 {
return retbytes return retbytes
} }
@ -708,11 +716,7 @@ func (pop *parsedOpcode) bytes() []byte {
} }
} }
for i := range pop.data { return append(retbytes, pop.data...)
retbytes = append(retbytes, pop.data[i])
}
return retbytes
} }
// opcode implementation functions from here // opcode implementation functions from here

View file

@ -274,7 +274,7 @@ func parseScript(script []byte) ([]parsedOpcode, error) {
// template list for testing purposes. On error we return the list of parsed // template list for testing purposes. On error we return the list of parsed
// opcodes so far. // opcodes so far.
func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpcode, error) { func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpcode, error) {
retScript := []parsedOpcode{} retScript := make([]parsedOpcode, 0, len(script))
for i := 0; i < len(script); { for i := 0; i < len(script); {
instr := script[i] instr := script[i]
op, ok := opcodemap[instr] 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 // unparseScript reversed the action of parseScript and returns the
// parsedOpcodes as a list of bytes // parsedOpcodes as a list of bytes
func unparseScript(pops []parsedOpcode) []byte { func unparseScript(pops []parsedOpcode) []byte {
script := []byte{} script := make([]byte, 0, len(pops))
for _, pop := range pops { for _, pop := range pops {
script = append(script, pop.bytes()...) script = append(script, pop.bytes()...)
} }
@ -574,7 +574,7 @@ func (s *Script) subScript() []parsedOpcode {
// removeOpcode will remove any opcode matching ``opcode'' from the opcode // removeOpcode will remove any opcode matching ``opcode'' from the opcode
// stream in pkscript // stream in pkscript
func removeOpcode(pkscript []parsedOpcode, opcode byte) []parsedOpcode { func removeOpcode(pkscript []parsedOpcode, opcode byte) []parsedOpcode {
retScript := []parsedOpcode{} retScript := make([]parsedOpcode, 0, len(pkscript))
for _, pop := range pkscript { for _, pop := range pkscript {
if pop.opcode.value != opcode { if pop.opcode.value != opcode {
retScript = append(retScript, pop) 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 // removeOpcodeByData will return the pkscript minus any opcodes that would
// push the data in ``data'' to the stack. // push the data in ``data'' to the stack.
func removeOpcodeByData(pkscript []parsedOpcode, data []byte) []parsedOpcode { func removeOpcodeByData(pkscript []parsedOpcode, data []byte) []parsedOpcode {
retScript := []parsedOpcode{} retScript := make([]parsedOpcode, 0, len(pkscript))
for _, pop := range pkscript { for _, pop := range pkscript {
if !bytes.Equal(pop.data, data) { if !bytes.Equal(pop.data, data) {
retScript = append(retScript, pop) retScript = append(retScript, pop)

View file

@ -2,13 +2,13 @@
github.com/conformal/btcscript/address.go scriptToAddressTemplate 100.00% (58/58) 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 Script.Step 100.00% (37/37)
github.com/conformal/btcscript/script.go parseScriptTemplate 100.00% (30/30) 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 asInt 100.00% (18/18)
github.com/conformal/btcscript/stack.go fromInt 100.00% (14/14) 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/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/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/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 parsedOpcode.print 100.00% (12/12)
github.com/conformal/btcscript/opcode.go opcodeNotIf 100.00% (11/11) 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/opcode.go opcodeCheckSig 96.15% (25/26)
github.com/conformal/btcscript/script.go Script.CheckErrorCondition 78.57% (11/14) 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/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/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 SetLogWriter 0.00% (0/7)
github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1) 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)