From 62c6fbc8f8537a562baf96d75b088447e042e327 Mon Sep 17 00:00:00 2001 From: YenForYang Date: Thu, 9 Sep 2021 07:27:16 -0500 Subject: [PATCH] Clarify ownership of (*Torrent).chunkPool (#583) Basically bind the lifetime of chunkPool to the torrent by using `sync.Pool` in lieu of `*sync.Pool`. Gives the GC ever so slightly less work to do. --- peerconn.go | 2 +- torrent.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/peerconn.go b/peerconn.go index 5d1f718b..fda3b95f 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1030,7 +1030,7 @@ func (c *PeerConn) mainReadLoop() (err error) { decoder := pp.Decoder{ R: bufio.NewReaderSize(c.r, 1<<17), MaxLength: 256 * 1024, - Pool: t.chunkPool, + Pool: &t.chunkPool, } for { var msg pp.Message diff --git a/torrent.go b/torrent.go index 70a8a9e1..b719529f 100644 --- a/torrent.go +++ b/torrent.go @@ -66,7 +66,7 @@ type Torrent struct { // The size of chunks to request from peers over the wire. This is // normally 16KiB by convention these days. chunkSize pp.Integer - chunkPool *sync.Pool + chunkPool sync.Pool // Total length of the torrent in bytes. Stored because it's not O(1) to // get this from the info dict. length *int64 @@ -233,7 +233,7 @@ func (t *Torrent) KnownSwarm() (ks []PeerInfo) { func (t *Torrent) setChunkSize(size pp.Integer) { t.chunkSize = size - t.chunkPool = &sync.Pool{ + t.chunkPool = sync.Pool{ New: func() interface{} { b := make([]byte, size) return &b