Skip to main content

CheckpointCache

Struct CheckpointCache 

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

A checkpoint cache for efficient incremental parsing

Implementations§

Source§

impl CheckpointCache

Source

pub fn new(max_checkpoints: usize) -> Self

Create a new checkpoint cache.

Source

pub fn len(&self) -> usize

Number of checkpoints currently held in the cache.

Source

pub fn is_empty(&self) -> bool

Whether the cache currently holds zero checkpoints.

Source

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.

Source

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).

Source

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 position
  • None - 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());
Source

pub fn clear(&mut self)

Clear all cached checkpoints.

Source

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.

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more