pub struct EntropyCache { /* private fields */ }Expand description
Branchless conditional accumulation for buy/sell volume (Issue #96 Task #177)
Uses arithmetic selection to avoid branch mispredictions in tight loops where is_buyer_maker
determines which accumulator (buy_vol or sell_vol) gets incremented.
Epsilon Branch Prediction Optimization: Traditional branch (if/else) causes pipeline flushes when prediction fails, especially when trade direction patterns change (common in market microstructure). Branchless approach uses pure arithmetic (multiply by 0.0 or 1.0) to distribute volume to the correct accumulator without branches.
§Implementation
- Converts
is_buyer_maker: boolto0.0 or 1.0for arithmetic selection - Uses
sell_vol += vol * is_buyer_maskto conditionally accumulate - Complement
buy_vol += vol * (1.0 - is_buyer_mask)for the alternate path - CPU executes both operations speculatively (no misprediction penalty)
§Performance
- Single-threaded: 0.8-1.2% speedup (reduced branch mispredictions)
- Multi-symbol streaming: 1.0-1.8% cumulative improvement on long lookback windows
- Register efficient: Uses 2x multiplies (CPU-friendly, pipelined)
§Example
let (buy, sell) = accumulate_buy_sell_branchless(trades);Implementations§
Source§impl EntropyCache
impl EntropyCache
Sourcepub fn new() -> Self
pub fn new() -> Self
Create new empty entropy cache with LRU eviction and metrics tracking (Task #135)
Sourcepub fn with_capacity(capacity: u64) -> Self
pub fn with_capacity(capacity: u64) -> Self
Create entropy cache with custom capacity (Issue #145: Global cache sizing)
Used by global entropy cache to support larger capacity (512-1024 entries) for improved hit ratio on multi-symbol workloads.
§Memory Usage
Approximate memory per entry: ~24 bytes (quick_cache overhead + u64 key + f64 value)
- 128 entries ≈ 3KB (default, per-processor)
- 512 entries ≈ 12KB (4x improvement)
- 1024 entries ≈ 24KB (8x improvement, global cache)
Sourcepub fn get(&self, prices: &[f64]) -> Option<f64>
pub fn get(&self, prices: &[f64]) -> Option<f64>
Get cached entropy result if available (O(1) operation) Tracks hit/miss metrics for cache effectiveness analysis (Task #135)
Sourcepub fn insert(&mut self, prices: &[f64], entropy: f64)
pub fn insert(&mut self, prices: &[f64], entropy: f64)
Cache entropy result (O(1) operation, quick_cache handles LRU eviction)
Sourcepub fn metrics(&self) -> (usize, usize, f64)
pub fn metrics(&self) -> (usize, usize, f64)
Get cache metrics: (hits, misses, hit_ratio) Returns hit ratio as percentage (0-100) for analysis (Task #135)
Sourcepub fn reset_metrics(&mut self)
pub fn reset_metrics(&mut self)
Reset metrics counters (useful for per-symbol analysis)
Trait Implementations§
Source§impl Debug for EntropyCache
impl Debug for EntropyCache
Auto Trait Implementations§
impl Freeze for EntropyCache
impl !RefUnwindSafe for EntropyCache
impl Send for EntropyCache
impl Sync for EntropyCache
impl Unpin for EntropyCache
impl UnsafeUnpin for EntropyCache
impl !UnwindSafe for EntropyCache
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more