Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for SemanticSplitRange searching #219

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ struct SemanticSplitRanges<Level>
where
Level: SemanticLevel,
{
/// Current cursor in the ranges list, so that we can skip over items we've
/// already processed.
cursor: usize,
/// Range of each semantic item and its precalculated semantic level
ranges: Vec<(Level, Range<usize>)>,
}
Expand All @@ -138,15 +141,15 @@ where
ranges.sort_unstable_by(|(_, a), (_, b)| {
a.start.cmp(&b.start).then_with(|| b.end.cmp(&a.end))
});
Self { ranges }
Self { cursor: 0, ranges }
}

/// Retrieve ranges for all sections of a given level after an offset
fn ranges_after_offset(
&self,
offset: usize,
) -> impl Iterator<Item = (Level, Range<usize>)> + '_ {
self.ranges
self.ranges[self.cursor..]
.iter()
.filter(move |(_, sep)| sep.start >= offset)
.map(|(l, r)| (*l, r.start..r.end))
Expand Down Expand Up @@ -212,8 +215,11 @@ where
}

/// Clear out ranges we have moved past so future iterations are faster
fn update_ranges(&mut self, cursor: usize) {
self.ranges.retain(|(_, range)| range.start >= cursor);
fn update_cursor(&mut self, cursor: usize) {
self.cursor += self.ranges[self.cursor..]
.iter()
.position(|(_, range)| range.start >= cursor)
.unwrap_or_else(|| self.ranges.len() - self.cursor);
}
}

Expand Down Expand Up @@ -270,7 +276,7 @@ where
fn next_chunk(&mut self) -> Option<(usize, &'text str)> {
// Reset caches so we can reuse the memory allocation
self.chunk_sizer.clear_cache();
self.semantic_split.update_ranges(self.cursor);
self.semantic_split.update_cursor(self.cursor);
self.update_next_sections();

let (start, end) = self.binary_search_next_chunk()?;
Expand Down Expand Up @@ -511,15 +517,27 @@ where
mod tests {
use super::*;

impl SemanticLevel for usize {}

#[test]
fn semantic_ranges_are_sorted() {
impl SemanticLevel for usize {}

let ranges = SemanticSplitRanges::new(vec![(0, 0..1), (1, 0..2), (0, 1..2), (2, 0..4)]);

assert_eq!(
ranges.ranges,
vec![(2, 0..4), (1, 0..2), (0, 0..1), (0, 1..2)]
);
}

#[test]
fn semantic_ranges_skip_previous_ranges() {
let mut ranges = SemanticSplitRanges::new(vec![(0, 0..1), (1, 0..2), (0, 1..2), (2, 0..4)]);

ranges.update_cursor(1);

assert_eq!(
ranges.ranges_after_offset(0).collect::<Vec<_>>(),
vec![(0, 1..2)]
);
}
}
2 changes: 2 additions & 0 deletions src/trim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod tests {
assert_eq!(chunk, "hello world");
}

#[cfg(any(feature = "markdown", feature = "code"))]
#[test]
fn trim_indentation_fallback() {
let chunk = " hello world ";
Expand All @@ -64,6 +65,7 @@ mod tests {
assert_eq!(chunk, "hello world");
}

#[cfg(any(feature = "markdown", feature = "code"))]
#[test]
fn trim_indentation_preserved() {
let chunk = "\n hello\n world ";
Expand Down
4 changes: 4 additions & 0 deletions tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ fn markdown_trim() {
});
}

#[cfg(feature = "markdown")]
#[test]
fn markdown_overlap_trim_false() {
insta::glob!("inputs/markdown/*.md", |path| {
Expand Down Expand Up @@ -337,6 +338,7 @@ fn markdown_overlap_trim_false() {
});
}

#[cfg(feature = "markdown")]
#[test]
fn markdown_overlap_trim() {
insta::glob!("inputs/markdown/*.md", |path| {
Expand Down Expand Up @@ -424,6 +426,7 @@ fn code_trim() {
});
}

#[cfg(feature = "code")]
#[test]
fn code_overlap_trim_false() {
insta::glob!("inputs/code/*.txt", |path| {
Expand Down Expand Up @@ -455,6 +458,7 @@ fn code_overlap_trim_false() {
});
}

#[cfg(feature = "code")]
#[test]
fn code_overlap_trim() {
insta::glob!("inputs/code/*.txt", |path| {
Expand Down
Loading