refactor: add typed sync.Pool wrapper to eliminate any

Introduce util/pools.Pool[T] as a generic typed wrapper
around sync.Pool. Migrate existing pool usages in
contenthash, converter, and overlay packages to use the
new wrapper, removing unchecked type assertions at each
call site.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2026-03-09 21:57:53 -07:00
parent 769cb0da7b
commit 0a402db53c
4 changed files with 47 additions and 22 deletions

View File

@@ -19,6 +19,7 @@ import (
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/util/cachedigest"
"github.com/moby/buildkit/util/pools"
"github.com/moby/locker"
"github.com/moby/patternmatcher"
digest "github.com/opencontainers/go-digest"
@@ -1261,15 +1262,13 @@ func ensureOriginMetadata(md cache.RefMetadata) cache.RefMetadata {
return em
}
var pool32K = sync.Pool{
New: func() any {
buf := make([]byte, 32*1024) // 32K
return &buf
},
}
var pool32K = pools.New(func() *[]byte {
buf := make([]byte, 32*1024) // 32K
return &buf
})
func poolsCopy(dst io.Writer, src io.Reader) (written int64, err error) {
buf := pool32K.Get().(*[]byte)
buf := pool32K.Get()
written, err = io.CopyBuffer(dst, src, *buf)
pool32K.Put(buf)
return