pub struct Consumer<'a> { /* private fields */ }Implementations§
Source§impl Consumer<'_>
impl Consumer<'_>
Sourcepub fn try_pop(&mut self) -> Option<L2Update>
pub fn try_pop(&mut self) -> Option<L2Update>
Examples found in repository?
examples/bench.rs (line 98)
63fn bench_spsc_throughput() -> f64 {
64 let mut ring = SpscRingBuffer::default();
65 let mut slots = vec![L2Update::default(); SPSC_CAPACITY];
66
67 let (mut producer, mut consumer) =
68 split(&mut ring, &mut slots).expect("failed to initialize SPSC ring buffer");
69
70 let start = Instant::now();
71
72 thread::scope(|scope| {
73 let producer_handle = scope.spawn(move || {
74 for i in 0..SPSC_MESSAGES {
75 let mut msg = L2Update {
76 market_id: i & 0x1fff,
77 mid_price: 0.45 + ((i % 10_000) as f64) * 0.00001,
78 implied_vol: 0.08 + ((i % 1_000) as f64) * 0.0001,
79 };
80
81 loop {
82 match producer.try_push(msg) {
83 Ok(()) => break,
84 Err(m) => {
85 msg = m;
86 spin_loop();
87 }
88 }
89 }
90 }
91 });
92
93 let consumer_handle = scope.spawn(move || {
94 let mut received = 0_u64;
95 let mut checksum = 0.0_f64;
96
97 while received < SPSC_MESSAGES {
98 if let Some(msg) = consumer.try_pop() {
99 received += 1;
100 checksum += msg.mid_price + msg.implied_vol + (msg.market_id as f64) * 1e-12;
101 } else {
102 spin_loop();
103 }
104 }
105
106 black_box(checksum);
107 received
108 });
109
110 producer_handle
111 .join()
112 .expect("producer thread panicked during benchmark");
113
114 let received = consumer_handle
115 .join()
116 .expect("consumer thread panicked during benchmark");
117 assert_eq!(received, SPSC_MESSAGES, "message loss in SPSC benchmark");
118 });
119
120 let elapsed = start.elapsed();
121 let secs = elapsed.as_secs_f64();
122 (SPSC_MESSAGES as f64 / 1_000_000.0) / secs
123}pub fn capacity(&self) -> usize
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Consumer<'a>
impl<'a> RefUnwindSafe for Consumer<'a>
impl<'a> Unpin for Consumer<'a>
impl<'a> UnsafeUnpin for Consumer<'a>
impl<'a> UnwindSafe for Consumer<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more