From 36d93aeff3f8e8698d91ebb0bef7f8965b51b8ba Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Mon, 7 Jul 2025 21:37:47 +0200 Subject: [PATCH] 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). --- crates/generate/src/prepare_grammar/flatten_grammar.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/generate/src/prepare_grammar/flatten_grammar.rs b/crates/generate/src/prepare_grammar/flatten_grammar.rs index 954902e3..66f16fc0 100644 --- a/crates/generate/src/prepare_grammar/flatten_grammar.rs +++ b/crates/generate/src/prepare_grammar/flatten_grammar.rs @@ -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()))?; }