Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove power of two check #78

Merged
merged 14 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ jobs:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/checkout@v3
- name: Install nightly toolchain with lint tools available
uses: actions-rs/toolchain@v1
with:
Expand Down Expand Up @@ -40,8 +39,7 @@ jobs:
name: Test Stable
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/checkout@v3
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
Expand All @@ -54,40 +52,27 @@ jobs:
command: test

test-beta:
name: Test Beta + Coverage
name: Test Beta
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/checkout@v3
- name: Install beta toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: beta
override: true

- name: rust-tarpaulin
uses: actions-rs/[email protected]
with:
args: --all-features --out Xml

- name: Upload to codecov.io
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Archive code coverage results
uses: actions/upload-artifact@v1
- name: Run cargo test
uses: actions-rs/cargo@v1
with:
name: code-coverage-report
path: cobertura.xml

command: test
test-nightly:
name: Test Nightly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ categories = ["data-structures"]
license = "MIT"

[dev-dependencies]
criterion = "0.1.2"
criterion = "0.4.0"
compiletest_rs = "0.9.0"

[features]
default = ["alloc"]
Expand Down
65 changes: 48 additions & 17 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![cfg(not(tarpaulin))]
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, RingBufferExt};
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, RingBufferExt, RingBufferWrite};

fn benchmark_push<T: RingBufferExt<i32>, F: Fn() -> T>(b: &mut Bencher, new: F) {
b.iter(|| {
let mut rb = new();

for i in 0..1_000_000 {
rb.push(i)
black_box(rb.push(i));
}

rb
Expand All @@ -19,23 +19,23 @@ fn benchmark_push_dequeue<T: RingBufferExt<i32>, F: Fn() -> T>(b: &mut Bencher,
let mut rb = new();

for _i in 0..100_000 {
rb.push(1);
rb.push(2);
black_box(rb.push(1));
black_box(rb.push(2));

assert_eq!(rb.dequeue(), Some(1));
assert_eq!(rb.dequeue(), Some(2));
assert_eq!(black_box(rb.dequeue()), Some(1));
assert_eq!(black_box(rb.dequeue()), Some(2));

rb.push(1);
rb.push(2);
black_box(rb.push(1));
black_box(rb.push(2));

assert_eq!(rb.dequeue(), Some(1));
assert_eq!(rb.dequeue(), Some(2));
assert_eq!(black_box(rb.dequeue()), Some(1));
assert_eq!(black_box(rb.dequeue()), Some(2));

rb.push(1);
rb.push(2);
black_box(rb.push(1));
black_box(rb.push(2));

assert_eq!(rb.get(-1), Some(&2));
assert_eq!(rb.get(-2), Some(&1));
assert_eq!(black_box(rb.get(-1)), Some(&2));
assert_eq!(black_box(rb.get(-2)), Some(&1));
}

rb
Expand All @@ -47,7 +47,7 @@ fn benchmark_various<T: RingBufferExt<i32>, F: Fn() -> T>(b: &mut Bencher, new:
let mut rb = new();

for i in 0..100_000 {
rb.push(i);
black_box(rb.push(i));
black_box(rb.get(-1));
}

Expand All @@ -63,6 +63,13 @@ macro_rules! generate_benches {
}));
)*
};
(non_power_two, $c: tt, $rb: tt, $ty: tt, $fn: tt, $bmfunc: tt, $($i:tt),*) => {
$(
$c.bench_function(&format!("{} {} 1M capacity not power of two {}", stringify!($rb), stringify!($bmfunc), stringify!($i)), |b| $bmfunc(b, || {
$rb::<$ty, _>::$fn($i)
}));
)*
};

(typed, $c: tt, $rb: tt, $ty: tt, $fn: tt, $bmfunc: tt, $($i:tt),*) => {
$(
Expand All @@ -73,9 +80,19 @@ macro_rules! generate_benches {
};
}

fn criterion_benchmark(c: &mut Criterion) {
c.with_plots();
fn benchmark_non_power_of_two<const L: usize>(b: &mut Bencher) {
b.iter(|| {
let mut rb = AllocRingBuffer::with_capacity_non_power_of_two(L);

for i in 0..1_000_000 {
black_box(rb.push(i));
}

rb
})
}

fn criterion_benchmark(c: &mut Criterion) {
// TODO: Improve benchmarks
// * What are representative operations
// * Make sure it's accurate
Expand Down Expand Up @@ -153,6 +170,20 @@ fn criterion_benchmark(c: &mut Criterion) {
4096,
8192
];
generate_benches![
non_power_two,
c,
AllocRingBuffer,
i32,
with_capacity_non_power_of_two,
benchmark_various,
16,
17,
1024,
4096,
8192,
8195
];
}

criterion_group!(benches, criterion_benchmark);
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ const fn mask(cap: usize, index: usize) -> usize {
index & (cap - 1)
}

/// Used internally. Computes the bitmask used to properly wrap the ringbuffers.
#[inline]
const fn mask_modulo(cap: usize, index: usize) -> usize {
index % cap
}

#[cfg(test)]
#[allow(non_upper_case_globals)]
mod tests {
Expand Down
Loading