perf: simplify state machine and eliminate code duplication #117
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Simplifies the state machine implementation by removing helper functions and eliminating duplicated logic. This change dramatically improves code generation while maintaining identical behavior.
Key Changes
consume_comment_whitespace_until_maybe_bracket: This function duplicated the entire state machine logic just for comma handlingconsume_line_comments,consume_block_comments,top,in_string,in_comment,maybe_comment_end(84 lines)Option<usize>to track comma position instead of re-scanningPerformance Impact
Assembly code improvements:
Source code:
Benchmark Results
No performance regression - all benchmarks remain stable:
Testing
✅ All 36 tests passing
✅ All doctests passing
✅ Handles streaming/partial reads correctly
✅ Preserves newlines and all existing behavior
Technical Details
The previous implementation had a fundamental issue: when encountering a comma, it would call
consume_comment_whitespace_until_maybe_bracketwhich essentially reimplemented the entire state machine to look ahead and determine if the comma was trailing. This created:The new implementation:
Option<usize>}/]This is a great example of how simpler source code leads to better machine code generation.
🤖 Generated with Claude Code