Skip to content

Optimized out unused states and production rules#27

Merged
ehwan merged 11 commits intomainfrom
reduce_optimize
Aug 23, 2025
Merged

Optimized out unused states and production rules#27
ehwan merged 11 commits intomainfrom
reduce_optimize

Conversation

@ehwan
Copy link
Owner

@ehwan ehwan commented Aug 23, 2025

Optimizes the state machine for single-token-reduce-state:
delete and redirect all of the state that has only single reduce action, and that reduce rule has single token A -> a.

This could reduce runtime by 30%

@ehwan ehwan requested a review from Copilot August 23, 2025 03:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the state machine by identifying and eliminating single-token reduce states and unused production rules. The optimization removes states that only perform a single reduction of a single-token rule (A -> a), redirecting transitions to bypass these states.

  • Added is_used field to production rules to track usage after optimization
  • Implemented state machine optimization algorithm that bypasses single-token reduce states
  • Updated parser state structures to support ShiftTarget with push/no-push semantics
  • Modified reduce action signatures to return boolean indicating whether data was pushed

Reviewed Changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
rusty_lr_parser/src/pattern.rs Adds is_used: true initialization to all newly created rules
rusty_lr_parser/src/nonterminal_info.rs Adds is_used field to Rule struct for optimization tracking
rusty_lr_parser/src/grammar.rs Implements main optimization algorithm and unused rule detection
rusty_lr_parser/src/emit.rs Updates code generation to handle optimized states and unused rules
rusty_lr_core/src/parser/state.rs Adds ShiftTarget struct and updates state interfaces
rusty_lr_core/src/parser/nondeterministic/context.rs Updates shift operations to use ShiftTarget
rusty_lr_core/src/parser/deterministic/context.rs Updates shift operations to use ShiftTarget
rusty_lr_core/src/parser/data_stack.rs Changes reduce action return type to Result<bool, _>
rusty_lr_core/src/builder/state.rs Updates state conversion to use ShiftTarget

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +1562 to +1564
let mut states: Vec<
rusty_lr_core::parser::state::IntermediateState<usize, usize, usize, usize>,
> = states.into_iter().map(Into::into).collect();
Copy link

Copilot AI Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The type annotation on line 1562-1564 is unnecessarily verbose and could be simplified using type inference, making the code more readable.

Suggested change
let mut states: Vec<
rusty_lr_core::parser::state::IntermediateState<usize, usize, usize, usize>,
> = states.into_iter().map(Into::into).collect();
let mut states = states.into_iter().map(Into::into).collect();

Copilot uses AI. Check for mistakes.
Comment on lines +1364 to +1365
reduce_action_case_streams.extend(quote! {
#rule_index => unreachable!("{rule_index}: this production rule was optimized out"),
Copy link

Copilot AI Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses string interpolation syntax {rule_index} but should use {rule_index} within the unreachable! macro. This will not interpolate the variable correctly.

Suggested change
reduce_action_case_streams.extend(quote! {
#rule_index => unreachable!("{rule_index}: this production rule was optimized out"),
#rule_index => unreachable!("{}: this production rule was optimized out", #rule_index),

Copilot uses AI. Check for mistakes.
Comment on lines +1481 to +1482
reduce_action_case_streams.extend(quote! {
#rule_index => unreachable!("{rule_index}: this production rule was optimized out"),
Copy link

Copilot AI Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above - the error message uses string interpolation syntax incorrectly within the unreachable! macro.

Suggested change
reduce_action_case_streams.extend(quote! {
#rule_index => unreachable!("{rule_index}: this production rule was optimized out"),
#rule_index => unreachable!("{}: this production rule was optimized out", #rule_index),

Copilot uses AI. Check for mistakes.
Comment on lines +1557 to +1558
reduce_action_case_streams.extend(quote! {
#rule_index => unreachable!("{rule_index}: this production rule was optimized out"),
Copy link

Copilot AI Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above - the error message uses string interpolation syntax incorrectly within the unreachable! macro.

Suggested change
reduce_action_case_streams.extend(quote! {
#rule_index => unreachable!("{rule_index}: this production rule was optimized out"),
#rule_index => unreachable!("{}: this production rule was optimized out", #rule_index),

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@ehwan ehwan merged commit 23f350e into main Aug 23, 2025
1 check passed
@ehwan ehwan deleted the reduce_optimize branch August 23, 2025 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant