]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Switch to xxHash for smartban cache
authorMatt Joiner <anacrolix@gmail.com>
Sun, 18 Feb 2024 01:59:42 +0000 (12:59 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 18 Feb 2024 01:59:42 +0000 (12:59 +1100)
client.go
smartban.go

index 62c0d2b4c52c5678990405fa0b779ad84ed041b3..7aab0402a70fc5333e44e759ac96b306ae07c696 100644 (file)
--- a/client.go
+++ b/client.go
@@ -4,12 +4,12 @@ import (
        "bufio"
        "context"
        "crypto/rand"
-       "crypto/sha1"
        "encoding/binary"
        "encoding/hex"
        "errors"
        "expvar"
        "fmt"
+       "github.com/cespare/xxhash"
        "io"
        "math"
        "net"
@@ -1301,7 +1301,14 @@ func (cl *Client) newTorrentOpt(opts AddTorrentOpts) (t *Torrent) {
                webSeeds:     make(map[string]*Peer),
                gotMetainfoC: make(chan struct{}),
        }
-       t.smartBanCache.Hash = sha1.Sum
+       var salt [8]byte
+       rand.Read(salt[:])
+       t.smartBanCache.Hash = func(b []byte) uint64 {
+               h := xxhash.New()
+               h.Write(salt[:])
+               h.Write(b)
+               return h.Sum64()
+       }
        t.smartBanCache.Init()
        t.networkingEnabled.Set()
        t.logger = cl.logger.WithDefaultLevel(log.Debug)
index 034a702d950372400c8dddb002fa1121304073e5..5515ded51fa3ef94f145a57439ef9e877dee6719 100644 (file)
@@ -2,7 +2,6 @@ package torrent
 
 import (
        "bytes"
-       "crypto/sha1"
        "net/netip"
 
        g "github.com/anacrolix/generics"
@@ -12,7 +11,7 @@ import (
 
 type bannableAddr = netip.Addr
 
-type smartBanCache = smartban.Cache[bannableAddr, RequestIndex, [sha1.Size]byte]
+type smartBanCache = smartban.Cache[bannableAddr, RequestIndex, uint64]
 
 type blockCheckingWriter struct {
        cache        *smartBanCache