Skip to content

Commit 0db0de4

Browse files
address comments
1 parent 6c5abfe commit 0db0de4

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

core/metainfo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"encoding/json"
1919
"errors"
2020
"fmt"
21-
"hash/crc32"
2221
"io"
2322

2423
"github.com/jackpal/bencode-go"
@@ -216,14 +215,14 @@ func calcPieceSumsFromBytes(data []byte, pieceLength int64) (int64, []uint32, er
216215
if n == 0 {
217216
return 0, nil, nil
218217
}
219-
numPieces := (n + pieceLength - 1) / pieceLength
218+
numPieces := (n-1)/pieceLength + 1
220219
pieceSums := make([]uint32, 0, numPieces)
221220
for offset := int64(0); offset < n; offset += pieceLength {
222221
end := offset + pieceLength
223222
if end > n {
224223
end = n
225224
}
226-
pieceSums = append(pieceSums, crc32.ChecksumIEEE(data[offset:end]))
225+
pieceSums = append(pieceSums, PieceSum(data[offset:end]))
227226
}
228227
return n, pieceSums, nil
229228
}

core/metainfo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func TestNewMetaInfoFromBytes_MatchesReader(t *testing.T) {
170170
}
171171
}
172172

173-
func BenchmarkNewMetaInfo(b *testing.B) {
173+
func BenchmarkNewMetaInfoFromBytes(b *testing.B) {
174174
cases := []struct {
175175
name string
176176
blobSize uint64

core/piece_hash.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ import (
2222
func PieceHash() hash.Hash32 {
2323
return crc32.NewIEEE()
2424
}
25+
26+
// PieceSum returns the checksum of b using the same algorithm as PieceHash.
27+
// It is equivalent to creating a PieceHash, writing b, and calling Sum32,
28+
// but avoids allocating a hash.Hash32 object.
29+
func PieceSum(b []byte) uint32 {
30+
return crc32.ChecksumIEEE(b)
31+
}

0 commit comments

Comments
 (0)