]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Reduce allocations for Piece.hash
authorMatt Joiner <anacrolix@gmail.com>
Wed, 30 Jan 2019 06:54:02 +0000 (17:54 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 30 Jan 2019 06:54:02 +0000 (17:54 +1100)
piece.go
torrent.go

index e580754df63cef2037eba8539a66ce87106e2120..6598859523150abd9cf78bcb6df5e577ed0484cd 100644 (file)
--- a/piece.go
+++ b/piece.go
@@ -39,7 +39,7 @@ const (
 
 type Piece struct {
        // The completed piece SHA1 hash, from the metainfo "pieces" field.
-       hash  metainfo.Hash
+       hash  *metainfo.Hash
        t     *Torrent
        index pieceIndex
        files []*File
index 133918b261687106a5e907d15c6d61f29415fff9..92089e94f06c8e94d303101b8094d5deda21a95f 100644 (file)
@@ -13,6 +13,7 @@ import (
        "sync"
        "text/tabwriter"
        "time"
+       "unsafe"
 
        "github.com/anacrolix/dht"
        "github.com/anacrolix/log"
@@ -290,9 +291,9 @@ func (t *Torrent) metadataSize() int {
        return len(t.metadataBytes)
 }
 
-func infoPieceHashes(info *metainfo.Info) (ret []string) {
+func infoPieceHashes(info *metainfo.Info) (ret [][]byte) {
        for i := 0; i < len(info.Pieces); i += sha1.Size {
-               ret = append(ret, string(info.Pieces[i:i+sha1.Size]))
+               ret = append(ret, info.Pieces[i:i+sha1.Size])
        }
        return
 }
@@ -305,7 +306,7 @@ func (t *Torrent) makePieces() {
                piece.t = t
                piece.index = pieceIndex(i)
                piece.noPendingWrites.L = &piece.pendingWritesMutex
-               missinggo.CopyExact(piece.hash[:], hash)
+               piece.hash = (*metainfo.Hash)(unsafe.Pointer(&hash[0]))
                files := *t.files
                beginFile := pieceFirstFileIndex(piece.torrentBeginOffset(), files)
                endFile := pieceEndFileIndex(piece.torrentEndOffset(), files)
@@ -1630,7 +1631,7 @@ func (t *Torrent) verifyPiece(piece pieceIndex) {
        cl.lock()
        p.hashing = false
        t.updatePiecePriority(piece)
-       t.pieceHashed(piece, sum == p.hash)
+       t.pieceHashed(piece, sum == *p.hash)
        t.publishPieceChange(piece)
 }