Skip to content

Commit 93778cd

Browse files
committed
[eclipse-iceoryx#139] Adjust benchmark; add small optimization
1 parent 668321d commit 93778cd

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

benchmarks/mpmc-bitset/src/main.rs

+53-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Config {
1919
iterations: usize,
2020
}
2121

22-
fn perform_benchmark(config: Config) {
22+
fn perform_benchmark_reset_all(config: Config) {
2323
let sut_a_b = BitSet::new(config.capacity);
2424
let sut_b_a = BitSet::new(config.capacity);
2525
// worst case bit - iterate over all bitset to see if its set
@@ -64,7 +64,50 @@ fn perform_benchmark(config: Config) {
6464

6565
let stop = start.elapsed();
6666
println!(
67-
"Capacity: {}, Iterations: {}, Time: {}, Latency: {} ns",
67+
"Bitset::reset_all() ::: Capacity: {}, Iterations: {}, Time: {}, Latency: {} ns",
68+
config.capacity,
69+
config.iterations,
70+
stop.as_secs_f64(),
71+
stop.as_nanos() / (config.iterations as u128 * 2)
72+
);
73+
});
74+
}
75+
76+
fn perform_benchmark_reset_next(config: Config) {
77+
let sut_a_b = BitSet::new(config.capacity);
78+
let sut_b_a = BitSet::new(config.capacity);
79+
let bit = 0;
80+
let barrier = Barrier::new(3);
81+
82+
std::thread::scope(|s| {
83+
let t1 = s.spawn(|| {
84+
barrier.wait();
85+
sut_a_b.set(bit);
86+
87+
for _ in 0..config.iterations {
88+
while sut_b_a.reset_next().is_none() {}
89+
sut_a_b.set(bit);
90+
}
91+
});
92+
93+
let t2 = s.spawn(|| {
94+
barrier.wait();
95+
for _ in 0..config.iterations {
96+
while sut_a_b.reset_next().is_none() {}
97+
sut_b_a.set(bit);
98+
}
99+
});
100+
101+
std::thread::sleep(std::time::Duration::from_millis(100));
102+
let start = Instant::now();
103+
barrier.wait();
104+
105+
t1.join().expect("thread failure");
106+
t2.join().expect("thread failure");
107+
108+
let stop = start.elapsed();
109+
println!(
110+
"Bitset::reset_next() ::: Capacity: {}, Iterations: {}, Time: {}, Latency: {} ns",
68111
config.capacity,
69112
config.iterations,
70113
stop.as_secs_f64(),
@@ -74,8 +117,13 @@ fn perform_benchmark(config: Config) {
74117
}
75118

76119
fn main() {
77-
perform_benchmark(Config {
120+
perform_benchmark_reset_all(Config {
78121
capacity: 256,
79-
iterations: 1000000,
80-
})
122+
iterations: 2000000,
123+
});
124+
125+
perform_benchmark_reset_next(Config {
126+
capacity: 256,
127+
iterations: 2000000,
128+
});
81129
}

iceoryx2-bb/lock-free/src/mpmc/bit_set.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use std::{
4040
alloc::Layout,
4141
fmt::Debug,
42-
sync::atomic::{AtomicBool, AtomicU8, Ordering},
42+
sync::atomic::{AtomicBool, AtomicIsize, AtomicU8, AtomicUsize, Ordering},
4343
};
4444

4545
use iceoryx2_bb_elementary::{
@@ -59,7 +59,6 @@ pub type RelocatableBitSet = details::BitSet<RelocatablePointer<details::BitsetE
5959

6060
#[doc(hidden)]
6161
pub mod details {
62-
use std::sync::atomic::AtomicUsize;
6362

6463
use super::*;
6564

@@ -87,7 +86,7 @@ pub mod details {
8786
capacity: usize,
8887
array_capacity: usize,
8988
reset_position: AtomicUsize,
90-
active_bits: AtomicUsize,
89+
active_bits: AtomicIsize,
9190
is_memory_initialized: AtomicBool,
9291
}
9392

@@ -115,7 +114,7 @@ pub mod details {
115114
array_capacity,
116115
is_memory_initialized: AtomicBool::new(true),
117116
reset_position: AtomicUsize::new(0),
118-
active_bits: AtomicUsize::new(0),
117+
active_bits: AtomicIsize::new(0),
119118
}
120119
}
121120
}
@@ -128,7 +127,7 @@ pub mod details {
128127
array_capacity: Self::array_capacity(capacity),
129128
is_memory_initialized: AtomicBool::new(false),
130129
reset_position: AtomicUsize::new(0),
131-
active_bits: AtomicUsize::new(0),
130+
active_bits: AtomicIsize::new(0),
132131
}
133132
}
134133

@@ -176,7 +175,7 @@ pub mod details {
176175
array_capacity: Self::array_capacity(capacity),
177176
is_memory_initialized: AtomicBool::new(true),
178177
reset_position: AtomicUsize::new(0),
179-
active_bits: AtomicUsize::new(0),
178+
active_bits: AtomicIsize::new(0),
180179
}
181180
}
182181
}
@@ -277,14 +276,17 @@ pub mod details {
277276
/// [`None`].
278277
pub fn reset_next(&self) -> Option<usize> {
279278
self.verify_init("reset_next");
280-
if self.active_bits.load(Ordering::Relaxed) == 0 {
279+
let active_bits = self.active_bits.load(Ordering::Relaxed);
280+
if active_bits <= 0 {
281281
return None;
282282
}
283283

284284
let current_position = self.reset_position.load(Ordering::Relaxed);
285285
for pos in (current_position..self.capacity).chain(0..current_position) {
286286
if self.clear_bit(Id::new(pos)) {
287-
self.reset_position.store(pos + 1, Ordering::Relaxed);
287+
if active_bits != 1 {
288+
self.reset_position.store(pos + 1, Ordering::Relaxed);
289+
}
288290
return Some(pos);
289291
}
290292
}
@@ -308,7 +310,7 @@ pub mod details {
308310
}
309311
}
310312

311-
if self.active_bits.fetch_sub(counter, Ordering::Relaxed) == counter {
313+
if self.active_bits.fetch_sub(counter, Ordering::Relaxed) <= counter {
312314
return;
313315
}
314316
}

0 commit comments

Comments
 (0)