Skip to main content

CachedTokenCounter

Struct CachedTokenCounter 

Source
pub struct CachedTokenCounter<T> { /* private fields */ }
Expand description

Bounded, thread-safe, exact-text memoization for an immutable token counter.

Counts are never estimated or added across blocks. Equality checks the complete string, not only a digest. FIFO eviction changes performance only. Errors are never cached. The cache retains source text in memory: own one per privacy boundary and clear/drop it when appropriate. Concurrent misses may compute twice; the expensive tokenizer never runs under the cache lock.

Implementations§

Source§

impl<T: TokenCounter> CachedTokenCounter<T>

Source

pub fn new(inner: T, limits: TokenCacheLimits) -> Self

Wraps an immutable tokenizer with an empty, instance-local cache.

Source

pub fn cache_stats(&self) -> Result<TokenCacheStats, ContextError>

Returns counters without exposing retained text.

Source

pub fn clear_cache(&self) -> Result<(), ContextError>

Drops all retained strings and resets counters. Does not promise allocator zeroization.

Trait Implementations§

Source§

impl<T: TokenCounter> TokenCounter for CachedTokenCounter<T>

Source§

fn identity(&self) -> &str

Algorithm/vocabulary identity; changes invalidate snapshot reuse.
Source§

fn count(&self, text: &str) -> Result<usize, ContextError>

Counts text as ordinary data, including strings resembling special model tokens.

Auto Trait Implementations§

§

impl<T> !Freeze for CachedTokenCounter<T>

§

impl<T> Forget for CachedTokenCounter<T>

§

impl<T> Move for CachedTokenCounter<T>

§

impl<T> RefUnwindSafe for CachedTokenCounter<T>
where T: RefUnwindSafe,

§

impl<T> Send for CachedTokenCounter<T>
where T: Send,

§

impl<T> Sync for CachedTokenCounter<T>
where T: Sync,

§

impl<T> Unpin for CachedTokenCounter<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for CachedTokenCounter<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for CachedTokenCounter<T>
where T: UnwindSafe,

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

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.