From f0328abeaae24b4ae5563dc68e067fa0a30376c0 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Sun, 7 Sep 2025 17:00:19 +0200 Subject: [PATCH] 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 --- lib/scanner/blocks.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/scanner/blocks.go b/lib/scanner/blocks.go index 3c420a03f..7aaf0eb97 100644 --- a/lib/scanner/blocks.go +++ b/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