Skip to content

Commit 03e93cb

Browse files
authored
Merge pull request #78 from NULLx76/remove-power-of-two-requirement
remove power of two check
2 parents 4db03aa + 09b0e76 commit 03e93cb

File tree

8 files changed

+270
-81
lines changed

8 files changed

+270
-81
lines changed

.github/workflows/rust.yml

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ jobs:
1010
name: Lint
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v2
14-
13+
- uses: actions/checkout@v3
1514
- name: Install nightly toolchain with lint tools available
1615
uses: actions-rs/toolchain@v1
1716
with:
@@ -40,8 +39,7 @@ jobs:
4039
name: Test Stable
4140
runs-on: ubuntu-latest
4241
steps:
43-
- uses: actions/checkout@v2
44-
42+
- uses: actions/checkout@v3
4543
- name: Install stable toolchain
4644
uses: actions-rs/toolchain@v1
4745
with:
@@ -54,40 +52,27 @@ jobs:
5452
command: test
5553

5654
test-beta:
57-
name: Test Beta + Coverage
55+
name: Test Beta
5856
runs-on: ubuntu-latest
5957

6058
steps:
61-
- uses: actions/checkout@v2
62-
59+
- uses: actions/checkout@v3
6360
- name: Install beta toolchain
6461
uses: actions-rs/toolchain@v1
6562
with:
6663
profile: minimal
6764
toolchain: beta
6865
override: true
6966

70-
- name: rust-tarpaulin
71-
uses: actions-rs/[email protected]
72-
with:
73-
args: --all-features --out Xml
74-
75-
- name: Upload to codecov.io
76-
uses: codecov/[email protected]
77-
with:
78-
token: ${{ secrets.CODECOV_TOKEN }}
79-
80-
- name: Archive code coverage results
81-
uses: actions/upload-artifact@v1
67+
- name: Run cargo test
68+
uses: actions-rs/cargo@v1
8269
with:
83-
name: code-coverage-report
84-
path: cobertura.xml
85-
70+
command: test
8671
test-nightly:
8772
name: Test Nightly
8873
runs-on: ubuntu-latest
8974
steps:
90-
- uses: actions/checkout@v2
75+
- uses: actions/checkout@v3
9176

9277
- name: Install nightly toolchain
9378
uses: actions-rs/toolchain@v1

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ categories = ["data-structures"]
1414
license = "MIT"
1515

1616
[dev-dependencies]
17-
criterion = "0.1.2"
17+
criterion = "0.4.0"
18+
compiletest_rs = "0.9.0"
1819

1920
[features]
2021
default = ["alloc"]

benches/bench.rs

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#![cfg(not(tarpaulin))]
22
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
3-
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, RingBufferExt};
3+
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, RingBufferExt, RingBufferWrite};
44

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

99
for i in 0..1_000_000 {
10-
rb.push(i)
10+
black_box(rb.push(i));
1111
}
1212

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

2121
for _i in 0..100_000 {
22-
rb.push(1);
23-
rb.push(2);
22+
black_box(rb.push(1));
23+
black_box(rb.push(2));
2424

25-
assert_eq!(rb.dequeue(), Some(1));
26-
assert_eq!(rb.dequeue(), Some(2));
25+
assert_eq!(black_box(rb.dequeue()), Some(1));
26+
assert_eq!(black_box(rb.dequeue()), Some(2));
2727

28-
rb.push(1);
29-
rb.push(2);
28+
black_box(rb.push(1));
29+
black_box(rb.push(2));
3030

31-
assert_eq!(rb.dequeue(), Some(1));
32-
assert_eq!(rb.dequeue(), Some(2));
31+
assert_eq!(black_box(rb.dequeue()), Some(1));
32+
assert_eq!(black_box(rb.dequeue()), Some(2));
3333

34-
rb.push(1);
35-
rb.push(2);
34+
black_box(rb.push(1));
35+
black_box(rb.push(2));
3636

37-
assert_eq!(rb.get(-1), Some(&2));
38-
assert_eq!(rb.get(-2), Some(&1));
37+
assert_eq!(black_box(rb.get(-1)), Some(&2));
38+
assert_eq!(black_box(rb.get(-2)), Some(&1));
3939
}
4040

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

4949
for i in 0..100_000 {
50-
rb.push(i);
50+
black_box(rb.push(i));
5151
black_box(rb.get(-1));
5252
}
5353

@@ -63,6 +63,13 @@ macro_rules! generate_benches {
6363
}));
6464
)*
6565
};
66+
(non_power_two, $c: tt, $rb: tt, $ty: tt, $fn: tt, $bmfunc: tt, $($i:tt),*) => {
67+
$(
68+
$c.bench_function(&format!("{} {} 1M capacity not power of two {}", stringify!($rb), stringify!($bmfunc), stringify!($i)), |b| $bmfunc(b, || {
69+
$rb::<$ty, _>::$fn($i)
70+
}));
71+
)*
72+
};
6673

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

76-
fn criterion_benchmark(c: &mut Criterion) {
77-
c.with_plots();
83+
fn benchmark_non_power_of_two<const L: usize>(b: &mut Bencher) {
84+
b.iter(|| {
85+
let mut rb = AllocRingBuffer::with_capacity_non_power_of_two(L);
7886

87+
for i in 0..1_000_000 {
88+
black_box(rb.push(i));
89+
}
90+
91+
rb
92+
})
93+
}
94+
95+
fn criterion_benchmark(c: &mut Criterion) {
7996
// TODO: Improve benchmarks
8097
// * What are representative operations
8198
// * Make sure it's accurate
@@ -153,6 +170,20 @@ fn criterion_benchmark(c: &mut Criterion) {
153170
4096,
154171
8192
155172
];
173+
generate_benches![
174+
non_power_two,
175+
c,
176+
AllocRingBuffer,
177+
i32,
178+
with_capacity_non_power_of_two,
179+
benchmark_various,
180+
16,
181+
17,
182+
1024,
183+
4096,
184+
8192,
185+
8195
186+
];
156187
}
157188

158189
criterion_group!(benches, criterion_benchmark);

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ const fn mask(cap: usize, index: usize) -> usize {
8181
index & (cap - 1)
8282
}
8383

84+
/// Used internally. Computes the bitmask used to properly wrap the ringbuffers.
85+
#[inline]
86+
const fn mask_modulo(cap: usize, index: usize) -> usize {
87+
index % cap
88+
}
89+
8490
#[cfg(test)]
8591
#[allow(non_upper_case_globals)]
8692
mod tests {

0 commit comments

Comments
 (0)