Skip to content

Track parser bracket depth without rescanning consumed tokens - #11

Draft
youknowone wants to merge 1 commit into
rustpython-0.17.5from
parser-bracket-depth-dec06
Draft

youknowone wants to merge 1 commit into
rustpython-0.17.5from
parser-bracket-depth-dec06

Conversation

@youknowone

Copy link
Copy Markdown
Member

Summary

Replacement fields in f-strings and t-strings need the consumed token stream's bracket level. Recomputing it by scanning every preceding token makes parsing many independent fields quadratic. Track the same saturating depth when consuming brackets, store it alongside the token cursor in parser checkpoints, and restore it on rewind. Newline relexing trims only trivia, so it does not change the depth. Lexer lookahead remains separate.

Validation

  • Linux optimized parser comparison at the exact fork pin: 1,290 inputs matched for ASTs, diagnostics and tokens, including 528 additional mixed delimiter, recovery and f-/t-string cases.
  • In an isolated parser microbenchmark with 40,000 fields, f-string parsing went from 1.83 s to 7.85 ms and t-string parsing from 1.83 s to 6.86 ms (median of five, one Linux host). This is parser-only, not end-to-end RustPython timing.
  • Changed Rust source formatting check passed. Full workspace tests and a RustPython build using this pin have not been run locally; this draft is not a RustPython pin update.

AI assistance: ChatGPT identified the repeated scan, implemented the parser change, and ran differential and microbenchmark checks.

Cache the unmatched bracket count rather than scanning all preceding tokens for each replacement field. Restore it on parser rewind; newline relexing only trims trivia.

Assisted-by: ChatGPT:version-not-exposed
@coderabbitai

coderabbitai Bot commented Dec 6, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch has not been deployed

No deployments
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