瀏覽代碼

chore(scanner): always return values to the pools when hashing blocks (#10377)

There are some return statements in between, but putting back the values
isn't my motivation (hardly ever happens), I just find this more
readable. Same with moving `hashLength`: Placed next to the pool the
connection with `sha256.New()` is closer.

Followup to:
chore(scanner): reduce memory pressure by using pools inside hasher #10222
6e26fab3a0a17018079e3161c8115f72caa26653

Signed-off-by: Simon Frei <[email protected]>
Simon Frei 1 月之前
父節點
當前提交
f0328abeaa
共有 1 個文件被更改,包括 8 次插入6 次删除
  1. 8 6
      lib/scanner/blocks.go

+ 8 - 6
lib/scanner/blocks.go

@@ -31,6 +31,8 @@ var bufPool = sync.Pool{
 	},
 }
 
+const hashLength = sha256.Size
+
 var hashPool = sync.Pool{
 	New: func() any {
 		return sha256.New()
@@ -43,9 +45,6 @@ func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, cou
 		counter = &noopCounter{}
 	}
 
-	hf := hashPool.Get().(hash.Hash) //nolint:forcetypeassert
-	const hashLength = sha256.Size
-
 	var blocks []protocol.BlockInfo
 	var hashes, thisHash []byte
 
@@ -62,8 +61,14 @@ func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, cou
 		hashes = make([]byte, 0, hashLength*numBlocks)
 	}
 
+	hf := hashPool.Get().(hash.Hash) //nolint:forcetypeassert
 	// A 32k buffer is used for copying into the hash function.
 	buf := bufPool.Get().(*[bufSize]byte)[:] //nolint:forcetypeassert
+	defer func() {
+		bufPool.Put((*[bufSize]byte)(buf))
+		hf.Reset()
+		hashPool.Put(hf)
+	}()
 
 	var offset int64
 	lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
@@ -102,9 +107,6 @@ func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, cou
 
 		hf.Reset()
 	}
-	bufPool.Put((*[bufSize]byte)(buf))
-	hf.Reset()
-	hashPool.Put(hf)
 
 	if len(blocks) == 0 {
 		// Empty file