Skip to content

parser: reduce unnecessary backtracking #11334

@camchenry

Description

@camchenry

This is a follow-up to #11194.

Now that we are using lookahead in more places in the parser (which is a relatively expensive operation), we want to ensure that we call it as little as possible. Similarly, we also want to avoid try_parse whenever possible. This can be achieved in a few different ways:

  1. Add a fast check before doing lookahead
    • Example: self.is_ts && self.lookahead(...) for checking TypeScript-specific syntax
    • Example: self.at(Kind::LCurly) && self.lookahead(...) for checking for a keyword that is only ever preceded by a few specific tokens
  2. Only parse forwards: remove backtracking
    • Ideally, we only ever need to look at a single token at a time in order to get the AST. This might not be possible everywhere currently but there are surely some functions that can be written in this way. In code this looks only using eat, at, bump, bump_any methods, and other similar functions and not using: rewind, checkpoint, try_parse, and lookahead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea - ParserE-Help WantedExperience level - For the experienced collaborators

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions