Skip to content

feat: persistent buffer on connection #902

Open
Migorithm wants to merge 7 commits intomasterfrom
feat/connection-buffer
Open

feat: persistent buffer on connection #902
Migorithm wants to merge 7 commits intomasterfrom
feat/connection-buffer

Conversation

@Migorithm
Copy link
Owner

Implement Persistent Buffer for TCP Stream Reading

Refactors the TCP reading logic to use a persistent BytesMut buffer instead of creating new allocations for every incoming packet. This change resolves potential framing bugs where messages are split across packets and significantly reduces memory allocation overhead.

The Problem

Previously, the reading logic had two critical issues:

Memory Churn: We were allocating a new buffer for every single read operation.

Framing Bugs: If a TCP packet contained a partial message (e.g., the last 3 bytes of a header), or multiple messages back-to-back, the previous logic risked discarding the "overflow" bytes or failing to stitch the message together correctly.

Solution

I have introduced a persistent BytesMut buffer stored in the connection struct.

  • Zero-Init & App-Level Zero-Copy: Uses read_buf to write directly into uninitialized memory capacity, avoiding the "double write" cost.
  • Automatic Framing: The buffer retains "leftover" bytes from previous reads. If we receive a partial header, it sits in the buffer until the next packet arrives to complete it.
  • Efficient Slicing: Uses split_to() to peel off completed messages without copying data, leveraging Bytes handles.

@Migorithm Migorithm self-assigned this Dec 8, 2025
@Migorithm Migorithm added enhancement New feature or request refactoring labels Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant