generate: quadratic comparisons during lexer state minimization
Author: jkbz64Created Sep 30, 2026Updated Sep 30, 2026
Labelsparser-generationperf
AI Policy
- I have read the AI Policy and this issue complies with it.
Problem
The current split_state_id_groups code compares every pair of states in a group, making n(n−1)/2 comparisons and causing quadratic work. For equivalent lexer states, comparing each state with the first one needs only n−1 comparisons.
After trying to slop fix it, here are the results:
| Parser | Before | After | Generation time change |
|---|---|---|---|
| ABL | 117.224s | 88.100s | 24.8% less |
| Swift | 1.435s | 1.431s | 0.3% less |
| F# | 1.358s | 1.340s | 1.4% less |
| Scala | 2.915s | 2.918s | 0.1% more |
| Kotlin | 8.692s | 8.663s | 0.3% less |
| Julia | 5.916s | 5.850s | 1.1% less |
| C# | 3.650s | 3.656s | 0.1% more |
| Lean | 142.209s | 141.708s | 0.3% less |
| Vim | 0.257s | 0.273s | 6.3% more |
Optimizing this seems to affect mainly ABL, other grammars seem to not be affected at all.
Slop fixdiff --git a/crates/generate/src/build_tables/build_lex_table.rs b/crates/generate/src/build_tables/build_lex_table.rs
index ef4d162..64b0cc7 100644
--- a/crates/generate/src/build_tables/build_lex_table.rs
+++ b/crates/generate/src/build_tables/build_lex_table.rs
@@ -9,7 +9,7 @@ use log::debug;
use super::{coincident_tokens::CoincidentTokenIndex, token_conflicts::TokenConflictMap};
use crate::{
- dedup::split_state_id_groups,
+ dedup::split_state_id_groups_by_equivalence,
grammars::{LexicalGrammar, SyntaxGrammar},
nfa::{CharacterSet, NfaCursor},
rules::{Symbol, SymbolView, TokenSet},
@@ -396,7 +396,9 @@ fn minimize_lex_table(table: &mut LexTable, parse_table: &mut ParseTable) {
}
}
- while split_state_id_groups(
+ // All states in each initial group have identical transition characters and
+ // lengths, so equality of their successor groups is transitive.
+ while split_state_id_groups_by_equivalence(
&table.states,
&mut state_ids_by_group_id,
&mut group_ids_by_state_id,
diff --git a/crates/generate/src/dedup.rs b/crates/generate/src/dedup.rs
index 095dda4..a608069 100644
--- a/crates/generate/src/dedup.rs
+++ b/crates/generate/src/dedup.rs
@@ -1,5 +1,41 @@
#[inline]
pub fn split_state_id_groups<S>(
+ states: &[S],
+ state_ids_by_group_id: &mut Vec<Vec<u32>>,
+ group_ids_by_state_id: &mut [u32],
+ start_group_id: u32,
+ should_split: impl FnMut(&S, &S, &[u32]) -> bool,
+) -> bool {
+ split_state_id_groups_impl::<S, false>(
+ states,
+ state_ids_by_group_id,
+ group_ids_by_state_id,
+ start_group_id,
+ should_split,
+ )
+}
+
+/// Split groups whose compatibility predicate is an equivalence relation.
+/// Comparing each member with the first member is sufficient in this case.
+#[inline]
+pub fn split_state_id_groups_by_equivalence<S>(
+ states: &[S],
+ state_ids_by_group_id: &mut Vec<Vec<u32>>,
+ group_ids_by_state_id: &mut [u32],
+ start_group_id: u32,
+ should_split: impl FnMut(&S, &S, &[u32]) -> bool,
+) -> bool {
+ split_state_id_groups_impl::<S, true>(
+ states,
+ state_ids_by_group_id,
+ group_ids_by_state_id,
+ start_group_id,
+ should_split,
+ )
+}
+
+#[inline]
+fn split_state_id_groups_impl<S, const TRANSITIVE: bool>(
states: &[S],
state_ids_by_group_id: &mut Vec<Vec<u32>>,
group_ids_by_state_id: &mut [u32],
@@ -45,6 +81,9 @@ pub fn split_state_id_groups<S>(
j += 1;
}
+ if TRANSITIVE {
+ break;
+ }
i += 1;
}
@@ -67,3 +106,64 @@ pub fn split_state_id_groups<S>(
result
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn equivalence_refinement_matches_pairwise_refinement() {
+ // Exercise successor-dependent partitions, including splits that require
+ // another pass and multiple initial groups whose order must be preserved.
+ for seed in 0..256usize {
+ let states = (0..8)
+ .map(|i| ((seed >> i) & 1, (seed + i * 3) % 8))
+ .collect::<Vec<_>>();
+ let differs = |a: &(usize, usize), b: &(usize, usize), groups: &[u32]| {
+ a.0 != b.0 || groups[a.1] != groups[b.1]
+ };
+ let mut pairwise = vec![vec![0], vec![1, 3, 5, 7], vec![2, 4, 6]];
+ let mut equivalent = pairwise.clone();
+ let mut pairwise_ids = vec![0, 1, 2, 1, 2, 1, 2, 1];
+ let mut equivalent_ids = pairwise_ids.clone();
+ loop {
+ let changed =
+ split_state_id_groups(&states, &mut pairwise, &mut pairwise_ids, 1, differs);
+ assert_eq!(
+ split_state_id_groups_by_equivalence(
+ &states,
+ &mut equivalent,
+ &mut equivalent_ids,
+ 1,
+ differs,
+ ),
+ changed,
+ );
+ assert_eq!(equivalent, pairwise);
+ assert_eq!(equivalent_ids, pairwise_ids);
+ if !changed {
+ break;
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn equivalent_group_needs_only_linear_comparisons() {
+ let states = vec![(); 1024];
+ let mut groups = vec![(0..1024).collect()];
+ let mut ids = vec![0; 1024];
+ let mut comparisons = 0;
+ assert!(!split_state_id_groups_by_equivalence(
+ &states,
+ &mut groups,
+ &mut ids,
+ 0,
+ |_, _, _| {
+ comparisons += 1;
+ false
+ },
+ ));
+ assert_eq!(comparisons, 1023);
+ }
+}Steps to reproduce
git clone https://github.com/usagi-coffee/tree-sitter-abl --depth=2
cd tree-sitter-abl
tree-sitter generateExpected behavior
Avoids checking every pair when a lexer-state group is already equivalent.
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