sync.Pool
allocates memory SCC-SA6002669// it only operates the first one of mutil-upload form file field.
670func (c *Controller) SaveToFile(fromFile, toFile string) error {
671 buf := copyBufferPool.Get().([]byte)
672 defer copyBufferPool.Put(buf)673 return c.SaveToFileWithBuffer(fromFile, toFile, buf)
674}
675
A sync.Pool
is used to avoid unnecessary allocations and reduce the amount of
work the garbage collector has to do.
When passing a value that is not a pointer to a function that accepts an
interface, the value needs to be placed on the heap, which means an additional
allocation. Slices are a common thing to put in sync.Pool
. One should store a
pointer to the slice instead to avoid the extra allocation.
import (
"sync"
)
var pool = sync.Pool{
New: func() any {
return bytes.Buffer{}
}
}
func foo() []byte {
b := pool.Get.(bytes.Buffer)
b.Reset()
b.WriteString("something")
b.WriteByte(' ')
b.WriteString("something")
res := b.Bytes()
pool.Put(b)
return res
}
import (
"sync"
)
var pool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
}
}
func foo() []byte {
b := pool.Get.(*bytes.Buffer)
b.Reset()
b.WriteString("something")
b.WriteByte(' ')
b.WriteString("something")
res := b.Bytes()
pool.Put(b)
return res
}