1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-03-08 15:21:13 +02:00

Optimize bufpool

This commit is contained in:
DarthSim 2023-06-12 22:00:55 +03:00
parent 9b6074094a
commit 8227592451
2 changed files with 23 additions and 5 deletions

View File

@ -106,7 +106,7 @@ func (p *Pool) calibrateAndClean() {
for i := 0; i < steps; i++ {
callsSum += p.tmpCalls[i]
if callsSum > defSum || defStep < 0 {
if defStep < 0 && callsSum > defSum {
defStep = i
}
@ -219,7 +219,7 @@ func (p *Pool) Get(size int, grow bool) *bytes.Buffer {
growSize := p.defaultSize
if grow {
growSize = imath.Max(size, growSize)
growSize = imath.Max(p.normalizeCap(size), growSize)
}
// Grow the buffer only if we know the requested size and it is smaller than
@ -261,6 +261,26 @@ func (p *Pool) Put(buf *bytes.Buffer) {
p.insert(buf)
}
// GrowBuffer growth capacity of the buffer to the normalized provided value
func (p *Pool) GrowBuffer(buf *bytes.Buffer, cap int) {
cap = p.normalizeCap(cap)
if buf.Cap() < cap {
buf.Grow(cap - buf.Len())
}
}
func (p *Pool) normalizeCap(cap int) int {
// Don't normalize cap if it's larger than maxSize
// since we'll throw this buf out anyway
maxSize := int(atomic.LoadUint64(&p.maxSize))
if maxSize > 0 && cap > maxSize {
return cap
}
ind := index(cap)
return imath.Max(cap, minSize<<ind)
}
func saveEntry(e *entry) {
e.buf = nil
e.next = nil

View File

@ -52,9 +52,7 @@ func readAndCheckImage(r io.Reader, contentLength int, secopts security.Options)
return nil, wrapError(err)
}
if contentLength > buf.Cap() {
buf.Grow(contentLength - buf.Len())
}
downloadBufPool.GrowBuffer(buf, contentLength)
if err = br.Flush(); err != nil {
buf.Reset()