koko icon indicating copy to clipboard operation
koko copied to clipboard

Optimize memory allocation with buffer pools and slice reuse

Open Copilot opened this issue 3 months ago • 1 comments

Addresses potential memory issues by reducing allocations in hot paths through buffer pooling and capacity-aware buffer management.

Changes

Buffer Pool Infrastructure (pkg/utils/buffer.go)

  • Added BufferPool type wrapping sync.Pool with configurable sizes
  • Global pools: SmallBufferPool (1KB), MediumBufferPool (32KB), LargeBufferPool (64KB)
  • BytesBufferPool for bytes.Buffer reuse with GetBytesBuffer()/PutBytesBuffer() helpers

FTP File Operations (pkg/proxy/recorder.go)

  • Pool-based 32KB buffer reuse in FTPFileInfo.WriteFromReader() instead of per-call allocation

Zmodem Parsing (pkg/zmodem/zsession.go)

  • Reuse parsedSubPacket slice capacity via s.parsedSubPacket = s.parsedSubPacket[:0]
  • Reset subPacketBuf when capacity exceeds 64KB to prevent memory bloat

Terminal Parser (pkg/proxy/parsercmd.go)

  • Added resetInputBuf() to reset buffers and reclaim memory when capacity exceeds threshold

Example

// Before: allocates 32KB per call
buf := make([]byte, 32*1024)

// After: reuses pooled buffer
bufPtr := ftpReadBufferPool.Get().(*[]byte)
buf := *bufPtr
defer ftpReadBufferPool.Put(bufPtr)

Benchmarks

BenchmarkBytesBufferPoolGet    0 B/op   0 allocs/op
BenchmarkRawBytesBufferAlloc  64 B/op   1 allocs/op
Original prompt

分析可能存在的内存问题,优化内存分配


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 27 '25 00:11 Copilot