#[repr(align(64))]pub struct BybitFeeder { /* private fields */ }Expand description
Bybit market data feeder optimized for HFT applications
This feeder implementation is specifically designed for high-frequency trading (HFT) scenarios with the following performance characteristics:
- Zero-allocation critical paths using SmallVec and stack allocation
- Lock-minimization with fine-grained locking for maximum concurrency
- Cache-line alignment for optimal CPU cache efficiency
- CPU pinning for dedicated core processing
- Batch processing for efficient trade and orderbook updates
- Comprehensive performance statistics tracking
- Real-time shared orderbooks with minimal contention
- Time synchronization with nanosecond precision
- Efficient statistics and monitoring
§Performance Settings
Performance can be tuned via the FeederOptions:
channel_buffer_size: Size of message buffers (default: 1024)max_depth_levels: Maximum depth levels for orderbooks (default: 100)batch_size: Batch message processing size (default: 32)cpu_affinity: Pin to specific CPU core (default: -1, no pinning)use_zero_copy: Enable zero-copy processing where possible (default: true)max_backlog: Maximum message backlog before dropping (default: 10000)
§Performance Notes
Software prefetching has been removed as it provided minimal benefit:
- Sequential access patterns are already optimized by CPU hardware prefetcher
- Immediate usage after prefetch negates benefits
- Small data structures (Decimal ~16 bytes) fit multiple elements per cache line
Focus areas for HFT optimization:
- CPU pinning to dedicated cores
- Cache-line aligned data structures
- Minimizing lock contention with SkipMap
- Batch processing for large updates
- Zero-copy where possible
§Examples
use rusty_feeder::bybit::feeder::BybitFeeder;
use rusty_feeder::feeder::{Feeder, FeederOptions};
use rusty_model::instruments::InstrumentId;
use rusty_model::venues::Venue;
async fn subscribe_spot_trades() {
// Create new Bybit feeder
let feeder = BybitFeeder::new();
// Configure options for low latency
let options = FeederOptions {
channel_buffer_size: 4096,
cpu_affinity: 5, // Pin to CPU core 5
batch_size: 64,
use_zero_copy: true,
max_depth_levels: 20,
max_backlog: 50000,
..Default::default()
};
// Get a receiver for normalized trades
let instrument = InstrumentId::new("BTCUSDT".to_string(), Venue::Bybit);
let trade_rx = feeder.subscribe_trades(instrument.clone()).await.unwrap();
let normalized_trades_rx = feeder.start_feed_trades(
instrument,
trade_rx,
Some(options)
).await.unwrap();
}Implementations§
Source§impl BybitFeeder
impl BybitFeeder
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new Bybit feeder with default settings
This constructor initializes a new Bybit feeder with a default high-precision clock and empty state containers. The feeder is immediately ready to process market data.
§Example
use rusty_feeder::bybit::feeder::BybitFeeder;
let feeder = BybitFeeder::new();Sourcepub fn with_clock(clock: Clock) -> Self
pub fn with_clock(clock: Clock) -> Self
Create a new Bybit feeder with a custom high-precision clock
This constructor allows providing a pre-configured clock, which can be useful for:
- Testing with a controlled clock source
- Synchronizing multiple feeders with the same clock
- Using a clock with specific adjustments for latency compensation
§Parameters
clock- A pre-configuredquanta::Clockinstance for high-precision timing
§Example
use rusty_feeder::bybit::feeder::BybitFeeder;
use quanta::Clock;
// Create a clock with specific configuration
let clock = Clock::new();
// Create feeder with the custom clock
let feeder = BybitFeeder::with_clock(clock);Sourcepub fn get_all_stats(&self) -> FxHashMap<String, FeedStats>
pub fn get_all_stats(&self) -> FxHashMap<String, FeedStats>
Get the current feed statistics for all active feeds
Returns a copy of the current statistics map, which can be used for monitoring and debugging the performance of the feeder.
§Returns
- A HashMap mapping feed identifiers to their statistics
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Get the memory usage of this feeder instance in bytes
This method provides an estimate of the memory used by the feeder’s internal data structures, which can be useful for monitoring resource usage.
§Returns
- Estimated memory usage in bytes
Trait Implementations§
Source§impl Debug for BybitFeeder
impl Debug for BybitFeeder
Source§impl Default for BybitFeeder
impl Default for BybitFeeder
Source§impl Feeder for BybitFeeder
impl Feeder for BybitFeeder
Get a realtime shared orderbook for a symbol
Source§fn get_bar_cache<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
instrument_id: &'life1 InstrumentId,
bar_type: &'life2 BarType,
max_bars: usize,
) -> Pin<Box<dyn Future<Output = Result<Arc<RwLock<BarCache>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn get_bar_cache<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
instrument_id: &'life1 InstrumentId,
bar_type: &'life2 BarType,
max_bars: usize,
) -> Pin<Box<dyn Future<Output = Result<Arc<RwLock<BarCache>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Get a bar cache for a symbol and bar type
Source§fn get_stats<'life0, 'life1, 'async_trait>(
&'life0 self,
instrument_id: &'life1 InstrumentId,
) -> Pin<Box<dyn Future<Output = Result<FeedStats>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_stats<'life0, 'life1, 'async_trait>(
&'life0 self,
instrument_id: &'life1 InstrumentId,
) -> Pin<Box<dyn Future<Output = Result<FeedStats>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Get feed statistics for a specific instrument Returns performance data for the instrument’s feeds
Source§fn reset_stats<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn reset_stats<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Reset all feed statistics Clears all collected performance statistics