Skip to content

Pool LZ4 working buffers to reduce compression allocations#132

Open
Isusami wants to merge 1 commit intomasterking32:mainfrom
Isusami:feature/lz4-buffer-pooling
Open

Pool LZ4 working buffers to reduce compression allocations#132
Isusami wants to merge 1 commit intomasterking32:mainfrom
Isusami:feature/lz4-buffer-pooling

Conversation

@Isusami
Copy link
Copy Markdown

@Isusami Isusami commented Apr 13, 2026

Summary

Add sync.Pool-backed scratch buffers for LZ4 compress/decompress to avoid allocating a fresh buffer on every call.

Changes

  • Add lz4BufferPool with getLZ4Buffer/putLZ4Buffer
  • compressLZ4: use pooled buffer for compression output, bytes.Clone the result before returning the buffer to the pool
  • decompressLZ4: use pooled buffer for decompression output, bytes.Clone the result before returning the buffer to the pool

Why

LZ4 compress/decompress are called on every packet. Reusing scratch buffers reduces allocation pressure. bytes.Clone ensures the returned slice is decoupled from the pool.

Test plan

  • go test -race ./internal/compression/... -- all 4 tests pass, race-clean

Add lz4BufferPool with getLZ4Buffer/putLZ4Buffer to reuse scratch
buffers across compress/decompress calls. Output is bytes.Clone'd
to decouple from the pooled buffer before returning it to the pool.
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.

1 participant