Skip to main content

LockFreeReadHistoryCache

Struct LockFreeReadHistoryCache 

Source
pub struct LockFreeReadHistoryCache { /* private fields */ }
Expand description

HistoryCache variant with a lock-free read path via RCU/copy-on-write.

Note: readers (e.g. the SEDP tick, heartbeat build, resend iteration) see an Arc-stable snapshot of the BTreeMap storage; they access it without a further lock touch. Writers serialize over an internal [RcuCell] mutex and publish copy-on-write.

§Trade-offs

  • Read path: 1× mutex acquire for the refcount inc + 0 further locks. Concurrent readers see the same Arc; read iteration is essentially lock-free.
  • Write path: O(n) per insert/remove, because the BTreeMap content must be cloned. Acceptable for small caches (<= 1000 samples). For write-heavy paths keep using HistoryCache.
  • Memory: active snapshots consume memory until they are released (reader lifetime). Cache mutations do not invalidate existing reader snapshots.

§When to use

  • Discovery caches that are read frequently by the SEDP tick + match loops, but mutated only on announce_publication/subscription.
  • Monitoring/tooling paths that want to iterate over the cache without a lock touch.

§When NOT to use

  • Reliable-writer cache with a high insert rate (every insert clones the whole BTreeMap). There HistoryCache stays better.

Persistent data structures (im::OrdMap) would push the write-cost effort to O(log n) — that is the natural follow-up optimization once this variant lands in production.

Implementations§

Source§

impl LockFreeReadHistoryCache

Source

pub fn new_with_kind(kind: HistoryKind, max_samples: usize) -> Self

Creates a new lock-free read cache.

Source

pub fn new(max_samples: usize) -> Self

Legacy constructor — KeepAll.

Source

pub fn stats(&self) -> Arc<HistoryCacheStats>

Lock-free read snapshot of stats.

Source

pub fn snapshot(&self) -> Arc<LockFreeInner>

Returns an Arc snapshot of the current cache state for lock-free iteration.

Source

pub fn kind(&self) -> HistoryKind

History kind.

Source

pub fn evicted_count(&self) -> u64

Number of samples discarded by KeepLast eviction.

Source

pub fn len(&self) -> usize

Number of changes (Acquire load of the atomic).

Source

pub fn is_empty(&self) -> bool

True if no changes.

Source

pub fn min_sn(&self) -> Option<SequenceNumber>

Smallest SN from the atom — lock-free.

Source

pub fn max_sn(&self) -> Option<SequenceNumber>

Largest SN from the atom — lock-free.

Source

pub fn capacity(&self) -> usize

Maximum capacity.

Source

pub fn get(&self, sn: SequenceNumber) -> Option<CacheChange>

Fetches a change by SN — cloned (CacheChange is Arc-payload- wrapped, so a refcount inc).

Source

pub fn iter_range_snapshot( &self, lo: SequenceNumber, hi: SequenceNumber, ) -> Vec<CacheChange>

Sample snapshot in the SN range [lo, hi]. Returns a Vec — we cannot return an Iter <'a> over an Arc snapshot when the snapshot is not referenced.

Source

pub fn insert(&self, change: CacheChange) -> Result<(), CacheError>

Inserts a change. Copy-on-write of the BTreeMap.

§Errors

As HistoryCache::insert.

Source

pub fn remove_up_to(&self, sn: SequenceNumber) -> usize

Removes all changes with SN ≤ sn. Returns the number removed.

Trait Implementations§

Source§

impl Debug for LockFreeReadHistoryCache

Source§

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

Formats the value using the given formatter. 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, 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.