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 9 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
34 changes: 31 additions & 3 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![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(|| {
Expand Down Expand Up @@ -73,9 +73,31 @@ 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 {
rb.push(i)
}
jdonszelmann marked this conversation as resolved.
Show resolved Hide resolved

rb
})
}

fn benchmark_power_of_two<const L: usize>(b: &mut Bencher) {
b.iter(|| {
let mut rb = AllocRingBuffer::with_capacity(L);

for i in 0..1_000_000 {
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 +175,12 @@ fn criterion_benchmark(c: &mut Criterion) {
4096,
8192
];

c.bench_function("power of two 16", benchmark_power_of_two::<16>);
c.bench_function("non power of two 16", benchmark_non_power_of_two::<16>);

c.bench_function("power of two 1024", benchmark_power_of_two::<1024>);
c.bench_function("non power of two 1024", benchmark_non_power_of_two::<1024>);
jdonszelmann marked this conversation as resolved.
Show resolved Hide resolved
}

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