Skip to main content

Consumer

Struct Consumer 

Source
pub struct Consumer<'a> { /* private fields */ }

Implementations§

Source§

impl Consumer<'_>

Source

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}
Source

pub fn capacity(&self) -> usize

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Trait Implementations§

Source§

impl Send for Consumer<'_>

Source§

impl Sync for Consumer<'_>

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.