perf: More efficient computation of used symbols

As the call to `symbol_is_used` does not depend
on the production, it is more efficient to call it
only once outside the loop over productions.

I'm not sure if `rustc` is able to do this optimization
on its own (it would need to know that the function
is pure, which sounds difficult in general).
This commit is contained in:
Antonin Delpeuch 2025-07-07 21:37:47 +02:00 committed by Will Lillis
parent 1e7d77c517
commit 36d93aeff3

View file

@ -263,9 +263,10 @@ pub(super) fn flatten_grammar(
for (i, variable) in variables.iter().enumerate() {
let symbol = Symbol::non_terminal(i);
let used = symbol_is_used(&variables, symbol);
for production in &variable.productions {
if production.steps.is_empty() && symbol_is_used(&variables, symbol) {
if production.steps.is_empty() && used {
Err(FlattenGrammarError::EmptyString(variable.name.clone()))?;
}