Grow ensures the buffer has enough capacity for n additional bytes. If the buffer doesn't have enough spare capacity, a new larger buffer is allocated with doubled capacity plus n bytes. The old buffer is returned to the pool. Growth Strategy: New capacity = 2 * old capacity + n This exponential g
(buf []byte, n int)
| 119 | // buf = pool.Grow(buf, 256) // Ensure 256 bytes available |
| 120 | // buf = append(buf, data...) // Append without reallocation |
| 121 | func (p *BufferPool) Grow(buf []byte, n int) []byte { |
| 122 | if cap(buf)-len(buf) < n { |
| 123 | // Create new buffer with doubled capacity |
| 124 | newBuf := make([]byte, len(buf), 2*cap(buf)+n) |
| 125 | copy(newBuf, buf) |
| 126 | p.Put(buf) // Return old buffer to pool |
| 127 | return newBuf |
| 128 | } |
| 129 | return buf |
| 130 | } |