pub struct CheckpointCache { /* private fields */ }Expand description
A checkpoint cache for efficient incremental parsing
Implementations§
Source§impl CheckpointCache
impl CheckpointCache
Sourcepub fn add(&mut self, checkpoint: LexerCheckpoint)
pub fn add(&mut self, checkpoint: LexerCheckpoint)
Add a checkpoint to the cache.
If a checkpoint already exists at the same byte position, it is
replaced in place. Otherwise the new checkpoint is inserted at the
correct sorted index so Self::find_before and Self::find_after
can use binary search.
When the cache exceeds max_checkpoints, entries are evicted while
preserving the earliest and latest checkpoints as boundary anchors for
incremental parsing windows.
Sourcepub fn find_before(&self, position: usize) -> Option<&LexerCheckpoint>
pub fn find_before(&self, position: usize) -> Option<&LexerCheckpoint>
Find the nearest checkpoint at or before a given position.
Uses binary search over the sorted checkpoints vector (invariant
maintained by Self::add) for O(log N) rather than the previous
O(N) linear scan. This matters when the checkpoint limit is large
(50+) for big documents (#2080).
Sourcepub fn find_after(&self, position: usize) -> Option<&LexerCheckpoint>
pub fn find_after(&self, position: usize) -> Option<&LexerCheckpoint>
Find the nearest checkpoint at or after a given position.
Uses binary search over the sorted checkpoints vector (invariant
maintained by Self::add) for O(log N) performance. This is the
counterpart to Self::find_before and is needed for two-sided
checkpoint windows in incremental parsing (#3527).
§Arguments
position- The byte position to search from
§Returns
Some(&LexerCheckpoint)- The checkpoint at or after the given positionNone- If no checkpoint exists at or after the position
§Examples
let mut cache = CheckpointCache::new(10);
cache.add(LexerCheckpoint::at_position(100));
cache.add(LexerCheckpoint::at_position(200));
cache.add(LexerCheckpoint::at_position(300));
// Find checkpoint at or after position 150
let cp = cache.find_after(150);
assert!(matches!(cp, Some(found) if found.position == 200));
// Find checkpoint at exact position
let cp = cache.find_after(200);
assert!(matches!(cp, Some(found) if found.position == 200));
// Position beyond last checkpoint returns None
let cp = cache.find_after(400);
assert!(cp.is_none());Sourcepub fn apply_edit(&mut self, start: usize, old_len: usize, new_len: usize)
pub fn apply_edit(&mut self, start: usize, old_len: usize, new_len: usize)
Apply an edit to all cached checkpoints, shifting or invalidating each entry and re-sorting to preserve the binary-search invariant.