AllocRingBuffer: separate size from capacity and remove power of 2 types#114
Merged
Merged
Conversation
tertsdiepraam
commented
Jun 9, 2023
Comment on lines
+40
to
46
| pub struct AllocRingBuffer<T> { | ||
| buf: *mut T, | ||
| size: usize, | ||
| capacity: usize, | ||
| readptr: usize, | ||
| writeptr: usize, | ||
| mode: PhantomData<SIZE>, | ||
| } |
Contributor
Author
There was a problem hiding this comment.
I added a size field here and removed mode.
1867655 to
4b0613e
Compare
Collaborator
|
I measure 57% speedup on non-powers of 2 and +1.2% (likely noise) on powers of two. As well as cleaning up the API a ton, I think this is a good change. |
tertsdiepraam
commented
Sep 15, 2023
| @@ -408,22 +296,25 @@ impl<T> AllocRingBuffer<T, PowerOfTwo> { | |||
| /// Panics when capacity is zero or not a power of two | |||
Contributor
Author
There was a problem hiding this comment.
Docs out of date here (and probably in other places too)
4951949 to
169a34a
Compare
Collaborator
|
and THAT'S why we have MIRI in the CI lol |
Contributor
Author
|
Memory leaks are safe, I don't see any issues here |
NULLx76
pushed a commit
that referenced
this pull request
Jul 11, 2025
`AllocRingBuffer`: separate size from capacity and remove power of 2 types
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.
I've already talked about this with @jonay2000, but to reiterate: this is just an idea. Your current implementation is fine and feel free to just close this.
This patch removes the distinction between
PowerOfTwoandNonPowerOfTwoand gives a performance improvement of ~30% to the non power of 2 benchmarks, while making powers of 2 10% slower.This is achieved by adding a field called
sizetoAllocRingBuffer. Thesizeis always set to the next power of 2 of thecapacity. The idea is that thecapacityis not the size of the allocation. Instead, an allocation ofsizeis made, so we always mask with the size. But the capacity still forms a hard limit on the length of the buffer.The upsides are:
with_capacitynow always succeeds for a non-zero capacity. This is (in my opinion) a more predictable behaviour and there is less opportunity to accidentally misuse the library.PowerOfTwo,NonPowerOfTwoandRingbufferSizeare all removedwith_capacity_non_power_of_twois removed (because it's now equivalent towith_capacity)AllocRingBufferonly has one type parameter, which also simplifies a lot of method signatures throughout the code base.The downsides are:
2^x + 1then the buffer will be almost twice as big as strictly necessary.Summary of benchmarks:
Full benchmarks (click to open)
Because this diff has a lot of type signature changes I'll add comments to point you to the important bits.