Skip to main content

EntropyCache

Struct EntropyCache 

Source
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: bool to 0.0 or 1.0 for arithmetic selection
  • Uses sell_vol += vol * is_buyer_mask to 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

Source

pub fn new() -> Self

Create new empty entropy cache with LRU eviction and metrics tracking (Task #135)

Source

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

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)

Source

pub fn insert(&mut self, prices: &[f64], entropy: f64)

Cache entropy result (O(1) operation, quick_cache handles LRU eviction)

Source

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)

Source

pub fn reset_metrics(&mut self)

Reset metrics counters (useful for per-symbol analysis)

Trait Implementations§

Source§

impl Debug for EntropyCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EntropyCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.