ring/buffered: replace linked-list ring with slice-based circular buffer#146
ring/buffered: replace linked-list ring with slice-based circular buffer#146
Conversation
There was a problem hiding this comment.
Pull request overview
This PR replaces the linked-list-based (container/ring) circular buffer implementation of Buffered in the ring package with a slice-based circular buffer. The new implementation uses doubling on growth and halving at 1/4 capacity for shrinking, and tracks minCap so the buffer never shrinks below its initial allocation. Tests are updated to match the new internals and new test cases are added for shrink behavior, wraparound correctness, and edge cases.
Changes:
- Replaced the
Ring-backed struct fields with a slice (buf),headindex,count, andminCap, implementinggrow()andshrink()methods that linearize the circular contents into a new allocation. - Updated all existing tests to assert against the new internal representation (
len(b.buf)andb.Len()) and added three new test functions covering shrink-below-minCap, empty-front, and wraparound scenarios. - Cleaned up the
NewBuffereddocstring (removed joke) and updated all method docstrings to reflect the new implementation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| ring/buffered.go | Replaced linked-list ring with slice-based circular buffer; added grow/shrink methods; bufferSize parameter is now unused. |
| ring/buffered_test.go | Updated existing tests for new internals; added tests for shrink-below-minCap, empty-front, and wraparound edge cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Use a slice-based circular buffer instead of container/ring, adding dynamic growing (double on full) and shrinking (halve at 1/4 capacity). Track minCap so the buffer never shrinks below its initial allocation. Update tests for the new internals and add coverage for shrink, wraparound, and edge cases. Signed-off-by: joshvanl <me@joshvanl.dev>
6eb7927 to
79da3c9
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Signed-off-by: joshvanl <me@joshvanl.dev>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Signed-off-by: joshvanl <me@joshvanl.dev>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Josh van Leeuwen <me@joshvanl.dev>
Use a slice-based circular buffer instead of container/ring, adding dynamic growing (double on full) and shrinking (halve at 1/4 capacity). Track minCap so the buffer never shrinks below its initial allocation. Update tests for the new internals and add coverage for shrink, wraparound, and edge cases.