optimize generated parser code via ZST, static slices, and direct vec! initialization#47
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the parser generation to make the parser struct lightweight by referencing static parser tables instead of holding owned vectors. The parser tables are now lazily initialized once using std::sync::OnceLock and returned as 'static slices. Two critical compilation issues were identified in the regenerated files (calculator.rs and calculator_u8.rs) where OnceLock::get_or_init returns &'static Vec<T> but the struct fields expect &'static [T]. Since Rust does not perform implicit deref coercion in struct initializers, calling .as_slice() explicitly is required to resolve the type mismatch.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| ]; | ||
| states.into_iter().map(|state| state.into()).collect() | ||
| }); | ||
| Self { rules, states } |
There was a problem hiding this comment.
Rust does not perform implicit deref coercion in struct initializers. Since rules and states are of type &'static Vec<T> (returned by OnceLock::get_or_init), but the struct fields expect &'static [T], this will result in a compilation error. Calling .as_slice() explicitly resolves this type mismatch.
Self {
rules: rules.as_slice(),
states: states.as_slice(),
}| ]; | ||
| states.into_iter().map(|state| state.into()).collect() | ||
| }); | ||
| Self { rules, states } |
There was a problem hiding this comment.
Rust does not perform implicit deref coercion in struct initializers. Since rules and states are of type &'static Vec<T> (returned by OnceLock::get_or_init), but the struct fields expect &'static [T], this will result in a compilation error. Calling .as_slice() explicitly resolves this type mismatch.
Self {
rules: rules.as_slice(),
states: states.as_slice(),
}
Summary
This PR optimizes the code generated by the parser generator (
rusty_lr_parser::emit::Grammar) to reduce runtime allocation overhead, remove atomic lock checks from the hot path during parsing, and dramatically improve compilation times for generated files.Key Changes
OnceLockConstructor InitializationGrammarParser) is now a Zero-Sized Type (ZST) holding'staticreferences torulesandstatesslices (rules: &'static [GrammarRule],states: &'static [GrammarState]).OnceLocklazy initialization has been moved into the parser'snew()constructor. Static tables are built and cached only once upon the first parser instantiation.Parser::get_rules()andParser::get_states()now return fields directly without checking theOnceLockatomic state, providing lock-free and atomic-free hot-path lookups.Clone,Copy, and explicitunsafe impl Sendandunsafe impl Syncfor the parser struct to guarantee thread safety regardless of the user's custom token type thread safety constraints.rulesetandreduce_mapzip().map().collect()) for states'rulesetwith directvec![ShiftedRuleRef { ... }]initialization.BTreeMapinsertion and conversion (into_iter().collect()) for states'reduce_mapwith clean, directvec![(TermClass, Vec<RuleIndex>)]initialization.__RUSTYLR_TSETx), significantly cleaning up the generated code and reducing compilation burden onrustc.Benefits
OnceLockatomic load operations on every token feed/reduction step.rustcto type check and compile generated files much faster with less memory.