Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
perf(splitter): cut the split time #2716 added, with identical chunks (#2803)
LJ π₯₯π΄e60bf50a194a
diff --git a/rust/ops_text/src/pattern_matcher.rs b/rust/ops_text/src/pattern_matcher.rsindex 11b8f119..e4f24880 100644--- a/rust/ops_text/src/pattern_matcher.rs+++ b/rust/ops_text/src/pattern_matcher.rs@@ -230,14 +230,9 @@ impl PatternMatcher {// where the parent directories would otherwise be pruned by the exclusion but// need to be traversed to reach the negation-exempt file.let dir_prefix = format!("{}/", path);- if self- .negation_patterns_raw+ self.negation_patterns_raw.iter().any(|p| p.starts_with(&dir_prefix))- {- return true;- }- false}/// Check if a file should be included based on both include and exclude patterns.diff --git a/rust/ops_text/src/split/recursive.rs b/rust/ops_text/src/split/recursive.rsindex abd58f42..34ea870f 100644--- a/rust/ops_text/src/split/recursive.rs+++ b/rust/ops_text/src/split/recursive.rs@@ -313,7 +313,9 @@ struct AtomChunksCollector<'s> {}impl<'s> AtomChunksCollector<'s> {- fn collect(&mut self, range: TextRange, boundary_cut_cost: u32) {+ /// Collects the atom at `range`, trimmed; `boundary_cut_cost` gives the+ /// cost of a cut at its first content, from that position.+ fn collect(&mut self, range: TextRange, boundary_cut_cost: impl FnOnce(usize) -> u32) {// Trim trailing whitespaces.let end_trimmed_text = &self.full_text[range.start..range.end].trim_end();if end_trimmed_text.is_empty() {@@ -341,7 +343,7 @@ impl<'s> AtomChunksCollector<'s> {boundary_syntax_level: self.min_level,internal_lb_level: line_break_level(trimmed_text),boundary_lb_level,- boundary_cut_cost,+ boundary_cut_cost: boundary_cut_cost(new_start),});self.min_level = self.curr_level;}@@ -406,17 +408,29 @@ fn node_header(let body = body_field_ids.iter().find_map(|&field| node.child_by_field_id(field.get()))+ // tree-sitter finds a node's siblings through its parent, which it+ // finds by walking down from the root, so these index into `node`..or_else(|| {- let last = node.named_child(node.named_child_count().checked_sub(1)?)?;+ let mut index = node.child_count();+ let last = loop {+ index = index.checked_sub(1)?;+ let child = node.child(index)?;+ if child.is_named() {+ break child;+ }+ };let opens_with_bracket =matches!(bytes.get(last.start_byte()), Some(b'{' | b'(' | b'['));- (opens_with_bracket && last.prev_sibling().is_some_and(|token| !token.is_named()))- .then_some(last)+ let follows_token = index+ .checked_sub(1)+ .and_then(|before| node.child(before))+ .is_some_and(|token| !token.is_named());+ (opens_with_bracket && follows_token).then_some(last)}).or_else(|| {let first = node.named_child(0)?;if first.kind().ends_with("heading") {- first.next_named_sibling()+ node.named_child(1)} else {None}@@ -468,8 +482,33 @@ struct WalkFrame<'t, 's: 't> {header: Option<NodeHeader>,/// The content of that syntax node, trimmed, if it is the body of a node/// that does not fit, or all of such a body's text: it may fit, but is not- /// kept whole (see `node_frame`).+ /// kept whole (see `push_node_frame`).unfitting_body: Option<TextRange>,+ /// The outermost `fitting_node` of this frame and the frames it is in.+ /// Frames nest, so it contains every other one: a position inside any of+ /// them is inside it, and one check per atom does for all of them.+ outermost_fitting_node: Option<TextRange>,+ /// The index in the frame stack of the innermost frame with a `header`,+ /// this one or one it is in. The frames with a header form a chain from+ /// it: the next is the one `header_frame` of the frame below names.+ header_frame: Option<usize>,+}++impl<'t, 's: 't> WalkFrame<'t, 's> {+ /// Pushes onto `frames` a frame that walks text split by regex, inside+ /// the frame on top, if any.+ fn push_text(frames: &mut Vec<WalkFrame<'t, 's>>, iter: ChunkIterator<'t, 's>) {+ let parent = frames.last();+ let frame = WalkFrame {+ iter,+ fitting_node: None,+ header: None,+ unfitting_body: None,+ outermost_fitting_node: parent.and_then(|parent| parent.outermost_fitting_node),+ header_frame: parent.and_then(|parent| parent.header_frame),+ };+ frames.push(frame);+ }}impl<'t, 's: 't> InternalRecursiveChunker<'s> {@@ -498,7 +537,9 @@ impl<'t, 's: 't> InternalRecursiveChunker<'s> {TextRange::new(start, range.start + text.trim_end().len())}- /// The frame that walks inside `node`, a child of the `parent` frame.+ /// Pushes onto `frames` the frame that walks inside `node`, a child of the+ /// frame on top, if any. Pushing here keeps `header_frame` right: a frame+ /// with a header names its own index in `frames`.////// The body of a node that does not fit is not kept whole, even if it fits/// on its own: the unit is the header with its body, and keeping the body@@ -508,13 +549,14 @@ impl<'t, 's: 't> InternalRecursiveChunker<'s> {/// the `inline` text of a Markdown paragraph, which splits at sentences./// A body with a header of its own, such as a decorated `def`, is a unit/// itself and stays whole where it fits.- fn node_frame(+ fn push_node_frame(&self,+ frames: &mut Vec<WalkFrame<'t, 's>>,iter: ChunkIterator<'t, 's>,node: tree_sitter::Node<'_>,splits_as_text: bool,- parent: Option<&WalkFrame<'t, 's>>,- ) -> WalkFrame<'t, 's> {+ ) {+ let parent = frames.last();let range = TextRange::new(node.start_byte(), node.end_byte());let content = self.trimmed(range);let header = node_header(self.full_text, &self.body_field_ids, node);@@ -524,35 +566,83 @@ impl<'t, 's: 't> InternalRecursiveChunker<'s> {|| (splits_as_text && parent.unfitting_body == Some(content))})).then_some(content);- WalkFrame {+ let fitting_node = if unfitting_body.is_some() {+ None+ } else {+ self.fitting_node(range)+ };+ let frame = WalkFrame {iter,- fitting_node: if unfitting_body.is_some() {- None- } else {- self.fitting_node(range)- },+ fitting_node,header,unfitting_body,- }+ outermost_fitting_node: parent+ .and_then(|parent| parent.outermost_fitting_node)+ .or(fitting_node),+ header_frame: if header.is_some() {+ Some(frames.len())+ } else {+ parent.and_then(|parent| parent.header_frame)+ },+ };+ frames.push(frame);}- /// The cost of cutting at the start of the atom at `range`, given the+ /// The cost of cutting at `pos`, the first content of an atom, given the/// frames of the syntax walk that the atom is inside of.////// A cut inside a node that fits in `chunk_size` costs/// `CUT_FITTING_NODE_COST`, a cut inside a node's header costs/// `CUT_INSIDE_HEADER_COST`, and one just after it `CUT_AFTER_HEADER_COST`,/// however many such nodes there are.- fn boundary_cut_cost(&self, frames: &[WalkFrame<'t, 's>], range: TextRange) -> u32 {- let text = &self.full_text[range.start..range.end];- let pos = range.start + (text.len() - text.trim_start().len());- let mut cost = 0;+ fn boundary_cut_cost(&self, frames: &[WalkFrame<'t, 's>], pos: usize) -> u32 {+ let Some(top) = frames.last() else {+ return 0;+ };+ let (mut inside_header, mut after_header) = (false, false);+ let mut header_frame = top.header_frame;+ while let Some(index) = header_frame {+ let header = frames[index]+ .header+ .expect("a header frame has a header")+ .range;+ inside_header |= header.start < pos && pos < header.end;+ after_header |= pos == header.end;+ header_frame = index+ .checked_sub(1)+ .and_then(|below| frames[below].header_frame);+ }+ let mut cost = if inside_header {+ CUT_INSIDE_HEADER_COST+ } else if after_header {+ CUT_AFTER_HEADER_COST+ } else {+ 0+ };+ if top+ .outermost_fitting_node+ .is_some_and(|node| node.start < pos && pos < node.end)+ {+ cost += CUT_FITTING_NODE_COST;+ }+ debug_assert_eq!(cost, Self::boundary_cut_cost_by_scan(frames, pos));+ cost+ }++ /// `boundary_cut_cost` computed by scanning every frame, without the+ /// cumulative `outermost_fitting_node` and `header_frame`: debug builds,+ /// and so every test, check that the two agree. It is not behind+ /// `cfg(debug_assertions)`, as `debug_assert_eq!` type-checks its operands+ /// in release builds too; there the check, and the call, compile away.+ fn boundary_cut_cost_by_scan(frames: &[WalkFrame<'t, 's>], pos: usize) -> u32 {let headers = || frames.iter().filter_map(|frame| frame.header);- if headers().any(|h| h.range.start < pos && pos < h.range.end) {- cost += CUT_INSIDE_HEADER_COST;+ let mut cost = if headers().any(|h| h.range.start < pos && pos < h.range.end) {+ CUT_INSIDE_HEADER_COST} else if headers().any(|h| pos == h.range.end) {- cost += CUT_AFTER_HEADER_COST;- }+ CUT_AFTER_HEADER_COST+ } else {+ 0+ };if frames.iter().filter_map(|frame| frame.fitting_node)@@ -573,23 +663,20 @@ impl<'t, 's: 't> InternalRecursiveChunker<'s> {ChunkKind::RegexpSepChunk { .. } => None,};let root_iter = ChunkIterator::Once(std::iter::once(chunk));- let mut frames = vec![match root_node {- Some(node) => self.node_frame(root_iter, node, false, None),- None => WalkFrame {- iter: root_iter,- fitting_node: None,- header: None,- unfitting_body: None,- },- }];+ let mut frames = Vec::new();+ match root_node {+ Some(node) => self.push_node_frame(&mut frames, root_iter, node, false),+ None => WalkFrame::push_text(&mut frames, root_iter),+ }while !frames.is_empty() {atom_collector.curr_level = frames.len();if let Some(current_chunk) = frames.last_mut().unwrap().iter.next() {if current_chunk.range.len() <= self.min_atom_chunk_size {- let cost = self.boundary_cut_cost(&frames, current_chunk.range);- atom_collector.collect(current_chunk.range, cost);+ atom_collector.collect(current_chunk.range, |pos| {+ self.boundary_cut_cost(&frames, pos)+ });} else {match current_chunk.kind {ChunkKind::TreeSitterNode {@@ -611,8 +698,7 @@ impl<'t, 's: 't> InternalRecursiveChunker<'s> {next_start_pos: current_chunk.range.start,end_pos: current_chunk.range.end,});- let frame = self.node_frame(iter, node, false, frames.last());- frames.push(frame);+ self.push_node_frame(&mut frames, iter, node, false);continue;}}@@ -626,28 +712,24 @@ impl<'t, 's: 't> InternalRecursiveChunker<'s> {next_regexp_sep_id: 0,},}));- let frame = self.node_frame(iter, node, true, frames.last());- frames.push(frame);+ self.push_node_frame(&mut frames, iter, node, true);}ChunkKind::RegexpSepChunk {lang_config,next_regexp_sep_id,} => {if next_regexp_sep_id >= lang_config.separator_regex.len() {- let cost = self.boundary_cut_cost(&frames, current_chunk.range);- atom_collector.collect(current_chunk.range, cost);- } else {- frames.push(WalkFrame {- iter: ChunkIterator::Text(TextChunksIter::new(- lang_config,- current_chunk.full_text,- current_chunk.range,- next_regexp_sep_id,- )),- fitting_node: None,- header: None,- unfitting_body: None,+ atom_collector.collect(current_chunk.range, |pos| {+ self.boundary_cut_cost(&frames, pos)});+ } else {+ let iter = ChunkIterator::Text(TextChunksIter::new(+ lang_config,+ current_chunk.full_text,+ current_chunk.range,+ next_regexp_sep_id,+ ));+ WalkFrame::push_text(&mut frames, iter);}}}