Skip to content

Commit

Permalink
definitely some UB here oof
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Sep 15, 2023
1 parent 4e3b3ca commit 0a68135
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 290 deletions.
34 changes: 21 additions & 13 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(not(tarpaulin_include))]

use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion, BatchSize};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Bencher, Criterion};
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, RingBuffer};

fn benchmark_push<T: RingBuffer<i32>, F: Fn() -> T>(b: &mut Bencher, new: F) {
Expand Down Expand Up @@ -192,36 +192,44 @@ fn extend_many_too_many(b: &mut Bencher) {
let rb = ConstGenericRingBuffer::new::<8192>();
let input = (0..16384).collect::<Vec<_>>();

b.iter_batched(&|| rb.clone(), |mut r| {
black_box(r.extend(black_box(input.as_slice())))
}, BatchSize::SmallInput);
b.iter_batched(
&|| rb.clone(),
|mut r| black_box(r.extend(black_box(input.as_slice()))),
BatchSize::SmallInput,
);
}

fn extend_too_many(b: &mut Bencher) {
let rb = ConstGenericRingBuffer::new::<8192>();
let input = (0..10000).collect::<Vec<_>>();

b.iter_batched(&|| rb.clone(), |mut r| {
black_box(r.extend(black_box(input.as_slice())))
}, BatchSize::SmallInput);
b.iter_batched(
&|| rb.clone(),
|mut r| black_box(r.extend(black_box(input.as_slice()))),
BatchSize::SmallInput,
);
}

fn extend_exact_cap(b: &mut Bencher) {
let rb = ConstGenericRingBuffer::new::<8192>();
let input = (0..8192).collect::<Vec<_>>();

b.iter_batched(&|| rb.clone(), |mut r| {
black_box(r.extend(black_box(input.as_slice())))
}, BatchSize::SmallInput);
b.iter_batched(
&|| rb.clone(),
|mut r| black_box(r.extend(black_box(input.as_slice()))),
BatchSize::SmallInput,
);
}

fn extend_too_few(b: &mut Bencher) {
let rb = ConstGenericRingBuffer::new::<8192>();
let input = (0..4096).collect::<Vec<_>>();

b.iter_batched(&|| rb.clone(), |mut r| {
black_box(r.extend(black_box(input.as_slice())))
}, BatchSize::LargeInput);
b.iter_batched(
&|| rb.clone(),
|mut r| black_box(r.extend(black_box(input.as_slice()))),
BatchSize::LargeInput,
);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
35 changes: 18 additions & 17 deletions src/ringbuffer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ use alloc::vec::Vec;
/// implementation, since these safety guarantees are necessary for
/// [`iter_mut`](RingBuffer::iter_mut) to work
pub unsafe trait RingBuffer<T>:
Sized +
IntoIterator<Item=T> +
Extend<T> +
Index<usize, Output=T> + IndexMut<usize> {
Sized + IntoIterator<Item = T> + Extend<T> + Index<usize, Output = T> + IndexMut<usize>
{
/// Returns the length of the internal buffer.
/// This length grows up to the capacity and then stops growing.
/// This is because when the length is reached, new items are appended at the start.
Expand Down Expand Up @@ -88,7 +86,7 @@ pub unsafe trait RingBuffer<T>:

/// alias for [`extend`](RingBuffer::extend).
#[inline]
fn enqueue_many<I: IntoIterator<Item=T>>(&mut self, items: I) {
fn enqueue_many<I: IntoIterator<Item = T>>(&mut self, items: I) {
self.extend(items);
}

Expand All @@ -97,7 +95,7 @@ pub unsafe trait RingBuffer<T>:
/// Iterates over the slice `other`, clones each element, and then appends
/// it to this `RingBuffer`. The `other` slice is traversed in-order.
///
/// Depending on the RingBuffer implementation, may be faster than inserting items in a loop
/// Depending on the `RingBuffer` implementation, may be faster than inserting items in a loop
///
/// Note that this function is same as [`extend`] except that it is
/// specialized to work with slices instead. If and when Rust gets
Expand All @@ -117,8 +115,11 @@ pub unsafe trait RingBuffer<T>:
/// ```
///
/// [`extend`]: RingBuffer::extend
fn extend_from_slice(&mut self, other: &[T]) where T: Clone {
self.extend(other.into_iter().cloned())
fn extend_from_slice(&mut self, other: &[T])
where
T: Clone,
{
self.extend(other.iter().cloned());
}

/// dequeues the top item off the ringbuffer, and moves this item out.
Expand Down Expand Up @@ -180,16 +181,16 @@ pub unsafe trait RingBuffer<T>:

/// Sets every element in the ringbuffer to it's default value
fn fill_default(&mut self)
where
T: Default,
where
T: Default,
{
self.fill_with(Default::default);
}

/// Sets every element in the ringbuffer to `value`
fn fill(&mut self, value: T)
where
T: Clone,
where
T: Clone,
{
self.fill_with(|| value.clone());
}
Expand Down Expand Up @@ -289,16 +290,16 @@ pub unsafe trait RingBuffer<T>:
/// Converts the buffer to a vector. This Copies all elements in the ringbuffer.
#[cfg(feature = "alloc")]
fn to_vec(&self) -> Vec<T>
where
T: Clone,
where
T: Clone,
{
self.iter().cloned().collect()
}

/// Returns true if elem is in the ringbuffer.
fn contains(&self, elem: &T) -> bool
where
T: PartialEq,
where
T: PartialEq,
{
self.iter().any(|i| i == elem)
}
Expand Down Expand Up @@ -395,7 +396,7 @@ mod iter {
impl<'rb, T: 'rb, RB: RingBuffer<T> + 'rb> ExactSizeIterator for RingBufferMutIterator<'rb, T, RB> {}

impl<'rb, T: 'rb, RB: RingBuffer<T> + 'rb> DoubleEndedIterator
for RingBufferMutIterator<'rb, T, RB>
for RingBufferMutIterator<'rb, T, RB>
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
Expand Down
Loading

1 comment on commit 0a68135

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.