pub struct AllowedTokensCache { /* private fields */ }Expand description
LRU cache mapping Earley state hashes → token masks.
Default capacity: 256 entries. At 150 k tokens (Qwen3 vocab) × 1 byte each, that is ≈38 MB worst-case; typical grammars cycle through far fewer states.
§Thread safety
AllowedTokensCache is not Sync on its own. In GrammarConstraint
it is wrapped in a std::sync::Mutex to satisfy the &self signature of
TokenConstraint::allowed_tokens.
Implementations§
Source§impl AllowedTokensCache
impl AllowedTokensCache
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a cache with the given capacity.
The capacity is clamped to a minimum of 1 to keep invariants simple.
Sourcepub fn get(&mut self, state_hash: u64) -> Option<Arc<[bool]>>
pub fn get(&mut self, state_hash: u64) -> Option<Arc<[bool]>>
Try to get a cached mask for the given state hash.
On a hit the key is promoted to the back of the LRU queue (most recently used) and the hit counter is incremented. On a miss the miss counter is incremented.
Sourcepub fn insert(&mut self, state_hash: u64, mask: Vec<bool>)
pub fn insert(&mut self, state_hash: u64, mask: Vec<bool>)
Insert a mask for the given state hash, evicting LRU if at capacity.
If the hash is already present the call is a no-op (the existing entry
is kept; this is safe because a single-threaded Mutex prevents races).
Auto Trait Implementations§
impl Freeze for AllowedTokensCache
impl RefUnwindSafe for AllowedTokensCache
impl Send for AllowedTokensCache
impl Sync for AllowedTokensCache
impl Unpin for AllowedTokensCache
impl UnsafeUnpin for AllowedTokensCache
impl UnwindSafe for AllowedTokensCache
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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