Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Fixed
- `subset`, on `FastAutomaton` and on `Term`, no longer explores a pair of a state and a set of states once it has met the same state with a smaller set. The sets of a state are met in the order of the words that reach them, not smallest first, and each set met before the smallest one was explored. `a(a|b){20}` ⊆ `(a|b)*a(a|b){20}` takes 11 µs instead of 33 s, and the other benchmarked patterns of the `(a|b)*a(a|b){n}` family 13 to 470 times less time. The other benchmarked patterns take up to 12% more time.

## [1.3.0] - 2026-11-20

### Added
Expand Down
21 changes: 18 additions & 3 deletions benches/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ fn bench_decision(c: &mut Criterion) {
});
};

// Nothing to skip: the 2^(N+1) reachable sets of states all accept
// different languages, so the walk is as long as determinization.
// Nothing for `equivalent` to skip: the 2^(N+1) reachable sets of states
// accept different languages, so its walk is as long as determinization.
// `subset` leaves most of its pairs unexplored, as the same state is met
// with a smaller set after them.
for n in [8, 12] {
let left = automaton(&blowup_pattern(n));
let right = automaton(&format!("(a*b*)*a(a|b){{{n}}}"));
Expand Down Expand Up @@ -337,13 +339,26 @@ fn bench_decision(c: &mut Criterion) {
bench_pair("spanning_sets", &left, &right);

// Different languages: the walk stops at the first counterexample, which
// here is a dozen letters in, so part of the blow-up is still walked.
// here is a dozen letters in, so `equivalent` walks part of the blow-up.
// `subset` leaves most of it unexplored, as for `blowup_N`.
let left = automaton(&blowup_pattern(12));
let right = automaton(&blowup_pattern(11));
assert!(!left.equivalent(&right).unwrap());
assert!(!left.subset(&right).unwrap());
bench_pair("counterexample", &left, &right);

// `subset` only: the left operand reaches the 2^N sets of the right one
// that record where the `a`s were, which all contain the set `ab…b`
// reaches. Larger sets are met first and must be dropped once it is.
for n in [12, 20] {
let left = automaton(&format!("a(a|b){{{n}}}"));
let right = automaton(&blowup_pattern(n));
assert!(left.subset(&right).unwrap());
group.bench_function(format!("subset/blowup_{n}_minuend"), |b| {
b.iter(|| black_box(&left).subset(black_box(&right)).unwrap())
});
}

let left = automaton(".*abc.*");
let right = automaton(".*cba.*");
group.bench_function("has_intersection", |b| {
Expand Down
12 changes: 11 additions & 1 deletion src/fast_automaton/analyze/reference_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,23 @@ fn respects_the_state_budget_and_the_timeout() {
let a = automaton("(a|b)*a(a|b){11}");
let b = automaton("(a*b*)*a(a|b){11}");
let (a_dfa, b_dfa) = (a.determinize().unwrap(), b.determinize().unwrap());
// `a ⊆ b` drops the pairs met with a superset and fits the budget. Here
// the sets of states `(a|b)*` reaches record the last 11 letters and are
// pairwise incomparable: none can be dropped.
let universal = automaton("(a|b)*");
let last_letters = FastAutomaton::union_all(&[
automaton("(a|b)*a(a|b){11}"),
automaton("(a|b)*b(a|b){11}"),
automaton("(a|b){0,11}"),
])
.unwrap();
ExecutionProfileBuilder::new()
.max_number_of_states(100)
.build()
.run(|| {
let err = EngineError::AutomatonHasTooManyStates;
assert_eq!(a.equivalent(&b).unwrap_err(), err);
assert_eq!(a.subset(&b).unwrap_err(), err);
assert_eq!(universal.subset(&last_letters).unwrap_err(), err);
assert_eq!(a_dfa.equivalent(&b_dfa).unwrap_err(), err);
assert_eq!(a.difference(&b).unwrap_err(), err);
assert_eq!(a.symmetric_difference(&b).unwrap_err(), err);
Expand Down
66 changes: 66 additions & 0 deletions src/fast_automaton/analyze/state_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,44 @@ impl<T> SetTrie<T> {
}
false
}

/// Returns `true` if a set stored below `root` is contained in `set`
/// and smaller than it. `stack` is reused as in
/// [`has_subset_of`](Self::has_subset_of), with the depth of each node.
pub(super) fn has_strict_subset_of(
&self,
root: usize,
set: &Marks,
stack: &mut Vec<(usize, usize)>,
) -> bool {
// A stored subset as large as `set` is `set` itself, which ends at
// the depth of its size.
let size = set.members().len();
stack.clear();
stack.push((root, 0));
while let Some((node, depth)) = stack.pop() {
let node = &self.nodes[node];
if depth < size && !node.values.is_empty() {
return true;
}
let depth = depth + 1;
match node.few_children() {
Some(children) => stack.extend(
children
.iter()
.filter(|(id, _)| set.contains(*id))
.map(|&(_, child)| (child, depth)),
),
None => stack.extend(
set.members()
.iter()
.filter_map(|&id| node.child(id))
.map(|child| (child, depth)),
),
}
}
false
}
}

/// A set of states with membership in constant time, and reset in time
Expand Down Expand Up @@ -336,6 +374,34 @@ mod tests {
assert!(!has_subset_of(&trie, first, &[5, wide + 5]));
}

#[test]
fn has_strict_subset_of_ignores_the_set_itself() {
let mut trie = SetTrie::new();
let (root, other) = (trie.new_root(), trie.new_root());
let mut marks = Marks::new(8);
let mut has_strict_subset_of = |trie: &SetTrie<()>, root: usize, set: &[Id]| {
set.iter().for_each(|&id| assert!(marks.insert(id)));
let found = trie.has_strict_subset_of(root, &marks, &mut Vec::new());
marks.clear();
found
};

trie.insert(root, &[1, 2, 3], ());
trie.insert(root, &[2, 4], ());
trie.insert(other, &[2], ());
assert!(!has_strict_subset_of(&trie, root, &[1, 2, 3]));
assert!(!has_strict_subset_of(&trie, root, &[2, 4]));
assert!(has_strict_subset_of(&trie, root, &[1, 2, 3, 4]));
assert!(has_strict_subset_of(&trie, root, &[0, 2, 4]));
assert!(!has_strict_subset_of(&trie, root, &[1, 3, 4]));

trie.insert(root, &[2], ());
assert!(has_strict_subset_of(&trie, root, &[1, 2, 3]));
assert!(!has_strict_subset_of(&trie, root, &[2]));
assert!(!has_strict_subset_of(&trie, other, &[2]));
assert!(has_strict_subset_of(&trie, other, &[2, 4]));
}

#[test]
fn get_returns_the_values_of_the_exact_set() {
let mut trie = SetTrie::new();
Expand Down
54 changes: 49 additions & 5 deletions src/fast_automaton/analyze/subset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ impl FastAutomaton {
/// The walk visits the pairs `(x, Y)` of a state of `self` and the set of
/// `other`'s states that one word reaches. A pair is a counterexample
/// when `x` accepts and `Y` does not, or when `Y` is empty (`x` is live).
/// It is skipped when `(x, Y')` was met for some `Y' ⊆ Y`. The pairs
/// kept are not a strict antichain: a pair met earlier with a superset
/// of `Y` is not removed, which costs memory but skips the same pairs.
/// It is skipped when `(x, Y')` was met for some `Y' ⊆ Y`, and, once
/// queued, it is left unexplored when `(x, Y')` has been met since for
/// some `Y' ⊊ Y`: `(x, Y')` leads to every counterexample it leads to.
/// The sets of a state are met in the order of the words that reach
/// them, not smallest first: without that second check, the walk
/// explores every set met before the smallest, 2^n of them on
/// `a(a|b){n}` ⊆ `(a|b)*a(a|b){n}`. The pairs left unexplored stay
/// stored: a pair they skip, `(x, Y')` skips.
///
/// Inclusion is also the equivalence `self ∪ other ≡ other`, but
/// [`equivalent_over_state_sets`](Self::equivalent_over_state_sets)
Expand Down Expand Up @@ -72,15 +77,27 @@ impl FastAutomaton {

let mut successors = vec![Vec::new(); bases];
let mut query = Marks::new(other.transitions.len());
// The nodes `has_subset_of` has to visit, reused by each call.
// The nodes `has_subset_of` and `has_strict_subset_of` have to
// visit, reused by each call.
let mut stack = Vec::new();
let mut strict_stack = Vec::new();
let mut pairs = 1;

while let Some((state, set)) = worklist.pop_front() {
let set = &sets[set];
set.iter().for_each(|&id| {
query.insert(id);
});
let root = roots[state as usize].expect("a queued pair is stored");
let dominated = visited.has_strict_subset_of(root, &query, &mut strict_stack);
query.clear();
if dominated {
continue;
}
execution_profile.assert_not_timed_out()?;
execution_profile.assert_max_number_of_states(pairs)?;

right.successors(&sets[set], &mut successors);
right.successors(set, &mut successors);

for (condition, to_state) in left.transitions_from(state) {
for base in condition.iter_set_bits() {
Expand Down Expand Up @@ -149,6 +166,33 @@ mod tests {
assert!(!other.subset(&a_star).unwrap());
}

// After `a(a|b){k}`, `other` is in a set of states that records where
// the `a`s were: 2^k sets, all containing the one reached by `ab…b`.
// The `a` successors are queued first, so the larger sets are met before
// it, and their pairs must be dropped once it is met.
#[test]
fn subset_drops_pairs_met_with_a_superset() {
use crate::execution_profile::ExecutionProfileBuilder;

let n = 30;
let left = RegularExpression::new(&format!("a(a|b){{{n}}}"))
.unwrap()
.to_automaton()
.unwrap();
let right = RegularExpression::new(&format!("(a|b)*a(a|b){{{n}}}"))
.unwrap()
.to_automaton()
.unwrap();

ExecutionProfileBuilder::new()
.max_number_of_states(10 * n)
.build()
.run(|| {
assert!(left.subset(&right).unwrap());
assert!(!right.subset(&left).unwrap());
});
}

#[test]
fn test_subset() -> Result<(), String> {
assert_subset(
Expand Down
Loading