[Core] Fix the issues in BinaryPacket.GrowSize#910
Open
Executor-Cheng wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates BinaryPacket.GrowSize to avoid an infinite loop when a BinaryPacket is default-initialized (capacity = 0), and to correct buffer pooling behavior during growth.
Changes:
- Replaces the
_capacity *= 2growth loop with a computed target capacity based on rounding. - Adjusts buffer rent/return behavior when resizing to use
ArrayPool<byte>.Shared. - Updates internal state (
_capacity,_bytesToReturnToPool,_span,_buffer) during growth.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
293
to
+297
| private void GrowSize(int additional) | ||
| { | ||
| while (_offset + additional > _capacity) _capacity *= 2; | ||
| _bytesToReturnToPool = ArrayPool<byte>.Shared.Rent(_capacity); | ||
| int requested = _offset + additional; | ||
| int desired = 1 << -BitOperations.LeadingZeroCount((uint)requested); | ||
| int capacity = desired > requested ? desired : requested; |
…oop and fails to return the shared buffer.
Executor-Cheng
force-pushed
the
fix/GrowSize-bugs
branch
from
July 24, 2026 07:09
9b725ee to
b1f11e9
Compare
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.
Fix the issue where BinaryPacket.GrowSize enters an infinite loop and fails to return the shared buffer.
@Controllerdestiny 没有方法能够禁止调用结构体的默认构造, 总是能通过
default(T)创建数据全零的结构体顺带修复扩容时有借无还的问题
扩容算法摘自Hacker’s Delight 2nd edition, Chapter 3. Power-of-2 Boundaries, 3–2 Rounding Up/Down to the Next Power of 2原来BCL给了
BitOperations.RoundUpToPowerOf2