Files
filebrowser/backend/internal/preview/buffer_pool.go
T
+10 3c02eedca6 Beta/v2.0.0 (#2746)
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Gabriel Górski <me@glaeqen.eu>
Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com>
Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com>
Co-authored-by: carloda <35763669+carloda@users.noreply.github.com>
Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com>
Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com>
Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com>
Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com>
Co-authored-by: Juan Soler <juansoler@gmail.com>
Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com>
Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net>
Co-authored-by: Neil <kylelzzz02@icloud.com>
Co-authored-by: Graham <info@quantumx-apps.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 16:24:38 -05:00

40 lines
1010 B
Go

package preview
import (
"bytes"
"sync"
)
// bufferPool manages reusable buffers to reduce allocations
// Buffers start at 128KB (first retry level) and can grow up to 512KB
var bufferPool = sync.Pool{
New: func() interface{} {
// Create 128KB buffers by default to match first retry level
return bytes.NewBuffer(make([]byte, 0, 128*1024))
},
}
// getBuffer gets a buffer from the pool
func getBuffer() *bytes.Buffer {
buf, ok := bufferPool.Get().(*bytes.Buffer)
if !ok {
// This should never happen, but provide a fallback
buf = bytes.NewBuffer(make([]byte, 0, 128*1024))
}
buf.Reset()
return buf
}
// putBuffer returns a buffer to the pool
func putBuffer(buf *bytes.Buffer) {
// Don't pool buffers that grew too large (>1MB)
// This prevents memory bloat from images with large headers (256KB/512KB retries)
// Typical case: 128KB buffers are reused
// Edge case: 256KB/512KB buffers are discarded and GC'd
if buf.Cap() > 1024*1024 {
return
}
buf.Reset()
bufferPool.Put(buf)
}