generate: quadratic range-vector edits in `CharacterSet::remove_intersection`
Author: jkbz64Created Sep 29, 2026Updated Sep 30, 2026
Labelsparser-generationperf
AI Policy
- I have read the AI Policy and this issue complies with it.
Problem
CharacterSet::remove_intersection walks two sorted range vectors but repeatedly calls Vec::remove and Vec::insert inside the walk. Each edit shifts the remaining tail. For sets with many overlapping disjoint ranges, the cumulative copying is quadratic in the number of ranges.
After trying to slop fix it here are the results:
| Parser | Before | After | Generation time change |
|---|---|---|---|
| ABL | 117.634s | 45.760s | 61.1% less |
| Swift | 1.439s | 0.954s | 33.7% less |
| F# | 1.377s | 1.294s | 6.0% less |
| Scala | 2.993s | 2.902s | 3.0% less |
| Kotlin | 8.955s | 8.725s | 2.6% less |
| Julia | 5.911s | 5.912s | ≈ unchanged |
| C# | 3.706s | 3.751s | 1.2% more |
It seems grammars with many overlapping Unicode token rules benefit most; grammars spending time elsewhere see little change.
Slop fixdiff --git a/crates/generate/src/nfa.rs b/crates/generate/src/nfa.rs
index e51eecd..03456ab 100644
--- a/crates/generate/src/nfa.rs
+++ b/crates/generate/src/nfa.rs
@@ -2,7 +2,7 @@ use std::{
cmp::{Ordering, max},
fmt,
iter::ExactSizeIterator,
- mem::{self, swap},
+ mem,
ops::{Range, RangeInclusive},
};
@@ -64,7 +64,7 @@ impl CharacterSet {
#[cfg(test)]
fn from_range(mut first: char, mut last: char) -> Self {
if first > last {
- swap(&mut first, &mut last);
+ mem::swap(&mut first, &mut last);
}
Self {
ranges: vec![(first as u32)..(last as u32 + 1)],
@@ -181,112 +181,50 @@ impl CharacterSet {
/// of the operands.
#[allow(clippy::return_self_not_must_use)]
pub fn remove_intersection(&mut self, other: &mut Self) -> Self {
+ if !self.does_intersect(other) {
+ return Self::empty();
+ }
let mut intersection = Vec::new();
+ let mut left_only = Vec::new();
+ let mut right_only = Vec::new();
let mut left_i = 0;
let mut right_i = 0;
+
+ // Consume sorted ranges once. Removing or inserting ranges in the middle
+ // of either vector repeatedly shifts its tail, making Unicode sets quadratic.
while left_i < self.ranges.len() && right_i < other.ranges.len() {
let left = &mut self.ranges[left_i];
let right = &mut other.ranges[right_i];
-
- match left.start.cmp(&right.start) {
- Ordering::Less => {
- // [ L ]
- // [ R ]
- if left.end <= right.start {
- left_i += 1;
- continue;
- }
-
- match left.end.cmp(&right.end) {
- // [ L ]
- // [ R ]
- Ordering::Less => {
- intersection.push(right.start..left.end);
- swap(&mut left.end, &mut right.start);
- left_i += 1;
- }
-
- // [ L ]
- // [ R ]
- Ordering::Equal => {
- intersection.push(right.clone());
- left.end = right.start;
- other.ranges.remove(right_i);
- }
-
- // [ L ]
- // [ R ]
- Ordering::Greater => {
- intersection.push(right.clone());
- let new_range = left.start..right.start;
- left.start = right.end;
- self.ranges.insert(left_i, new_range);
- other.ranges.remove(right_i);
- left_i += 1;
- }
- }
- }
- // [ L ]
- // [ R ]
- Ordering::Equal if left.end < right.end => {
- intersection.push(left.start..left.end);
- right.start = left.end;
- self.ranges.remove(left_i);
+ if left.end <= right.start {
+ left_only.push(left.clone());
+ left_i += 1;
+ } else if right.end <= left.start {
+ right_only.push(right.clone());
+ right_i += 1;
+ } else {
+ let start = left.start.max(right.start);
+ let end = left.end.min(right.end);
+ if left.start < start {
+ left_only.push(left.start..start);
}
- // [ L ]
- // [ R ]
- Ordering::Equal if left.end == right.end => {
- intersection.push(left.clone());
- self.ranges.remove(left_i);
- other.ranges.remove(right_i);
+ if right.start < start {
+ right_only.push(right.start..start);
}
- // [ L ]
- // [ R ]
- Ordering::Equal if left.end > right.end => {
- intersection.push(right.clone());
- left.start = right.end;
- other.ranges.remove(right_i);
+ intersection.push(start..end);
+ left.start = end;
+ right.start = end;
+ if left.start == left.end {
+ left_i += 1;
}
- Ordering::Equal => {}
- Ordering::Greater => {
- // [ L ]
- // [ R ]
- if left.start >= right.end {
- right_i += 1;
- continue;
- }
-
- match left.end.cmp(&right.end) {
- // [ L ]
- // [ R ]
- Ordering::Less => {
- intersection.push(left.clone());
- let new_range = right.start..left.start;
- right.start = left.end;
- other.ranges.insert(right_i, new_range);
- self.ranges.remove(left_i);
- right_i += 1;
- }
-
- // [ L ]
- // [ R ]
- Ordering::Equal => {
- intersection.push(left.clone());
- right.end = left.start;
- self.ranges.remove(left_i);
- }
-
- // [ L ]
- // [ R ]
- Ordering::Greater => {
- intersection.push(left.start..right.end);
- swap(&mut left.start, &mut right.end);
- right_i += 1;
- }
- }
+ if right.start == right.end {
+ right_i += 1;
}
}
}
+ left_only.extend_from_slice(&self.ranges[left_i..]);
+ right_only.extend_from_slice(&other.ranges[right_i..]);
+ self.ranges = left_only;
+ other.ranges = right_only;
Self {
ranges: intersection,
}
@@ -914,6 +852,44 @@ mod tests {
}
}
+ #[test]
+ fn test_remove_intersection_exhaustive() {
+ let from_bits = |bits: u32| {
+ (0..8).fold(CharacterSet::empty(), |set, bit| {
+ if bits & (1 << bit) != 0 {
+ set.add_char(char::from_u32(bit).unwrap())
+ } else {
+ set
+ }
+ })
+ };
+ for a in 0..256 {
+ for b in 0..256 {
+ let mut left = from_bits(a);
+ let mut right = from_bits(b);
+ assert_eq!(left.remove_intersection(&mut right), from_bits(a & b));
+ assert_eq!(left, from_bits(a & !b));
+ assert_eq!(right, from_bits(b & !a));
+ }
+ }
+ }
+
+ #[test]
+ fn test_remove_intersection_many_ranges() {
+ let original = CharacterSet {
+ ranges: (0..4096).map(|i| (4 * i)..(4 * i + 3)).collect(),
+ };
+ let mut left = original.clone();
+ let mut right = CharacterSet {
+ ranges: (0..4096).map(|i| (4 * i + 1)..(4 * i + 2)).collect(),
+ };
+ let expected = right.clone();
+ assert_eq!(left.remove_intersection(&mut right), expected);
+ assert!(right.is_empty());
+ assert_eq!(left.range_count(), 8192);
+ assert_eq!(left.add(&expected), original);
+ }
+
#[test]
fn test_character_set_intersection_difference_ops() {
struct Row {Steps to reproduce
git clone https://github.com/usagi-coffee/tree-sitter-abl --depth=2
cd tree-sitter-abl
tree-sitter generateExpected behavior
Generates quickly
Tree-sitter version (tree-sitter --version)
tree-sitter 0.27.0
Operating system/version
Linux 7.1.13-asahi-fairydust aarch64 unknown unknown
Source: tree-sitter/tree-sitter