(size: number)
| 3252 | private pools = new Map<number, Buffer[]>(); |
| 3253 | private readonly MAX_POOL_SIZE = 10; // Maximum buffers per size pool |
| 3254 | getBuffer(size: number): Buffer { |
| 3255 | // Round up to nearest power of 2 for better reuse (with minimum of 1KB) |
| 3256 | const poolSize = Math.max(1024, Math.pow(2, Math.ceil(Math.log2(size)))); |
| 3257 | let pool = this.pools.get(poolSize); |
| 3258 | if (!pool) { |
| 3259 | pool = []; |
| 3260 | this.pools.set(poolSize, pool); |
| 3261 | } |
| 3262 | // Try to reuse an existing buffer |
| 3263 | const buffer = pool.pop(); |
| 3264 | if (buffer) { |
| 3265 | return buffer; // Return full buffer, caller uses only what they need |
| 3266 | } |
| 3267 | // Create new buffer if none available |
| 3268 | return Buffer.allocUnsafe(poolSize); |
| 3269 | } |
| 3270 | returnBuffer(buffer: Buffer, originalSize: number): void { |
| 3271 | const poolSize = Math.max(1024, Math.pow(2, Math.ceil(Math.log2(originalSize)))); |
| 3272 | // Only return if it's the right size for the pool |
no test coverage detected