fix: connection.Read copies directly from buffer nodes#413
Open
fix: connection.Read copies directly from buffer nodes#413
Conversation
The old implementation called Next(n) then copy(p, src). For multi-node reads, Next allocates an intermediate buffer and copies data into it, then copy duplicates it again into p — a double copy plus an unnecessary allocation. Additionally, Next+Release in Read could invalidate zero-copy slices that the caller still holds from prior Reader.Next calls, since Release frees all consumed nodes from head to read indiscriminately. Replace Next+copy+Release with readTo, which: - Copies directly from underlying LinkBuffer nodes into p (single copy) - Releases fully-consumed nodes inline only when safe (head == node), meaning no outstanding zero-copy references exist - Skips inline release when head != read, leaving nodes for the caller's eventual Release call Also fix ioReadWriter to embed io.Reader/io.Writer interfaces and add BUG/deprecation comments for ioReader.Read.
ppzqh
reviewed
Mar 3, 2026
| } | ||
| old := b.read | ||
| b.read = b.read.next | ||
| if b.head == old { |
Contributor
There was a problem hiding this comment.
假设 head == read,然后通过 Next 单节点读了 read 的一部分。之后 Read 走到这里,好像会把 read 给 release 掉,导致 Next 读的那部分内存回收了。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The old implementation called Next(n) then copy(p, src). For multi-node reads, Next allocates an intermediate buffer and copies data into it, then copy duplicates it again into p — a double copy plus an unnecessary allocation.
Additionally, Next+Release in Read could invalidate zero-copy slices that the caller still holds from prior Reader.Next calls, since Release frees all consumed nodes from head to read indiscriminately.
Replace Next+copy+Release with readTo, which:
Also fix ioReadWriter to embed io.Reader/io.Writer interfaces and add BUG/deprecation comments for ioReader.Read.