Skip to content

Conversation

Copy link

Copilot AI commented Nov 27, 2025

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 AI and others added 2 commits November 27, 2025 00:36
Co-authored-by: LeeEirc <32626707+LeeEirc@users.noreply.github.com>
…stants

Co-authored-by: LeeEirc <32626707+LeeEirc@users.noreply.github.com>
@sonarqubecloud
Copy link

Copilot AI changed the title [WIP] Analyze and optimize potential memory issues Optimize memory allocation with buffer pools and slice reuse Nov 27, 2025
Copilot AI requested a review from LeeEirc November 27, 2025 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants