MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / spsc_cross_thread

Function spsc_cross_thread

nodedb-bridge/benches/throughput.rs:82–113  ·  view source on GitHub ↗
(b: &mut Bencher)

Source from the content-addressed store, hash-verified

80 tags = "core"
81)]
82fn spsc_cross_thread(b: &mut Bencher) {
83 const COUNT: usize = 100_000;
84
85 b.iter(|| {
86 let (mut prod, mut cons) = RingBuffer::channel::<Vec<u8>>(8192);
87 let payload = vec![0xABu8; MSG_SIZE];
88
89 let consumer = std::thread::spawn(move || {
90 let mut received = 0usize;
91 while received < COUNT {
92 if cons.try_pop().is_ok() {
93 received += 1;
94 } else {
95 std::hint::spin_loop();
96 }
97 }
98 received
99 });
100
101 let mut sent = 0usize;
102 while sent < COUNT {
103 if prod.try_push(payload.clone()).is_ok() {
104 sent += 1;
105 } else {
106 std::hint::spin_loop();
107 }
108 }
109
110 let received = consumer.join().unwrap();
111 black_box(received)
112 });
113}
114
115// 100K messages * 256 bytes = 25.6 MB per iteration.
116#[synthetic(

Callers

nothing calls this directly

Calls 7

is_okMethod · 0.80
try_popMethod · 0.80
try_pushMethod · 0.80
joinMethod · 0.80
spawnFunction · 0.50
iterMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected