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>
impl<T: TokenCounter> CachedTokenCounter<T>
Sourcepub fn new(inner: T, limits: TokenCacheLimits) -> Self
pub fn new(inner: T, limits: TokenCacheLimits) -> Self
Wraps an immutable tokenizer with an empty, instance-local cache.
Sourcepub fn cache_stats(&self) -> Result<TokenCacheStats, ContextError>
pub fn cache_stats(&self) -> Result<TokenCacheStats, ContextError>
Returns counters without exposing retained text.
Sourcepub fn clear_cache(&self) -> Result<(), ContextError>
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>
impl<T: TokenCounter> TokenCounter for CachedTokenCounter<T>
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> 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
Mutably borrows from an owned value. Read more