bloom: Avoid a few unnecessary hash copies. (#76)

This changes several places which call the Bytes method of a ShaHash to
simply use a slice of it in order to avoid the extra unnecessary copies.
This commit is contained in:
Dave Collins 2016-04-23 03:34:57 -05:00
parent bdf4400eca
commit 2c26dd81a5
2 changed files with 6 additions and 6 deletions

View file

@ -36,7 +36,7 @@ func ExampleNewFilter() {
filter.AddShaHash(txHash) filter.AddShaHash(txHash)
// Show that the filter matches. // Show that the filter matches.
matches := filter.Matches(txHash.Bytes()) matches := filter.Matches(txHash[:])
fmt.Println("Filter Matches?:", matches) fmt.Println("Filter Matches?:", matches)
// Output: // Output:

View file

@ -167,7 +167,7 @@ func (bf *Filter) Matches(data []byte) bool {
func (bf *Filter) matchesOutPoint(outpoint *wire.OutPoint) bool { func (bf *Filter) matchesOutPoint(outpoint *wire.OutPoint) bool {
// Serialize // Serialize
var buf [wire.HashSize + 4]byte var buf [wire.HashSize + 4]byte
copy(buf[:], outpoint.Hash.Bytes()) copy(buf[:], outpoint.Hash[:])
binary.LittleEndian.PutUint32(buf[wire.HashSize:], outpoint.Index) binary.LittleEndian.PutUint32(buf[wire.HashSize:], outpoint.Index)
return bf.matches(buf[:]) return bf.matches(buf[:])
@ -217,9 +217,9 @@ func (bf *Filter) Add(data []byte) {
// AddShaHash adds the passed wire.ShaHash to the Filter. // AddShaHash adds the passed wire.ShaHash to the Filter.
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (bf *Filter) AddShaHash(sha *wire.ShaHash) { func (bf *Filter) AddShaHash(hash *wire.ShaHash) {
bf.mtx.Lock() bf.mtx.Lock()
bf.add(sha.Bytes()) bf.add(hash[:])
bf.mtx.Unlock() bf.mtx.Unlock()
} }
@ -229,7 +229,7 @@ func (bf *Filter) AddShaHash(sha *wire.ShaHash) {
func (bf *Filter) addOutPoint(outpoint *wire.OutPoint) { func (bf *Filter) addOutPoint(outpoint *wire.OutPoint) {
// Serialize // Serialize
var buf [wire.HashSize + 4]byte var buf [wire.HashSize + 4]byte
copy(buf[:], outpoint.Hash.Bytes()) copy(buf[:], outpoint.Hash[:])
binary.LittleEndian.PutUint32(buf[wire.HashSize:], outpoint.Index) binary.LittleEndian.PutUint32(buf[wire.HashSize:], outpoint.Index)
bf.add(buf[:]) bf.add(buf[:])
@ -272,7 +272,7 @@ func (bf *Filter) maybeAddOutpoint(pkScript []byte, outHash *wire.ShaHash, outId
func (bf *Filter) matchTxAndUpdate(tx *btcutil.Tx) bool { func (bf *Filter) matchTxAndUpdate(tx *btcutil.Tx) bool {
// Check if the filter matches the hash of the transaction. // Check if the filter matches the hash of the transaction.
// This is useful for finding transactions when they appear in a block. // This is useful for finding transactions when they appear in a block.
matched := bf.matches(tx.Sha().Bytes()) matched := bf.matches(tx.Sha()[:])
// Check if the filter matches any data elements in the public key // Check if the filter matches any data elements in the public key
// scripts of any of the outputs. When it does, add the outpoint that // scripts of any of the outputs. When it does, add the outpoint that