From b6e4ae44419ab5efa232eb32bfcb2d45023918be Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 26 Oct 2013 14:31:15 -0500 Subject: [PATCH] Optimize duplicate transaction input check. Profiling showed the duplicate transaction input check was taking around 6% of the total CheckTransactionSanity processing time. This was largely due to using fmt.Sprintf to generate the map key. This commit modifies the check instead to use the actual output as a map key. The following benchmark results show the difference: Before: BenchmarkOldDuplicatInputCheck 100000 21787 ns/op After: BenchmarkNewDuplicatInputCheck 2000000 937 ns/op Closes #2 --- validate.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/validate.go b/validate.go index 2b51fbd0..00ed8ff2 100644 --- a/validate.go +++ b/validate.go @@ -230,14 +230,12 @@ func CheckTransactionSanity(tx *btcwire.MsgTx) error { } // Check for duplicate transaction inputs. - existingTxOut := make(map[string]bool) + existingTxOut := make(map[btcwire.OutPoint]bool) for _, txIn := range tx.TxIn { - prevOut := &txIn.PreviousOutpoint - key := fmt.Sprintf("%v%v", prevOut.Hash, prevOut.Index) - if _, exists := existingTxOut[key]; exists { + if _, exists := existingTxOut[txIn.PreviousOutpoint]; exists { return RuleError("transaction contains duplicate outpoint") } - existingTxOut[key] = true + existingTxOut[txIn.PreviousOutpoint] = true } // Coinbase script length must be between min and max length.