Skip to content

[NFC] Skip parsing instructions in first parser pass#8601

Open
tlively wants to merge 3 commits intomainfrom
parser-fastscan
Open

[NFC] Skip parsing instructions in first parser pass#8601
tlively wants to merge 3 commits intomainfrom
parser-fastscan

Conversation

@tlively
Copy link
Copy Markdown
Member

@tlively tlively commented Apr 14, 2026

The first parser pass is responsible for two things: finding the locations of definitions of top-level module items like globals and functions and finding the locations of implicit function type definitions. It previously accomplished the latter by fully parsing every instruction in each function. But the IR is not constructed in this phase of parsing, so fully parsing every instruction was largely wasted work. Optimize the parser by parsing only the instructions that might have implicit type definitions and otherwise just blindly match parentheses to skip the function body. Combined with #8597, this speeds up parsing by 30-40%.

tlively added 3 commits April 13, 2026 16:31
The lexer previously used its own internal `LexerCtx` abstraction that allowed it to consume the characters that made up a token without changing the lexer state, then update the state at once when committing to consuming the characters. However, manually resetting the lexer to the original position when giving up on parsing a token is simple enough that this abstraction was not holding its weight. Simplify the lexer by removing internal contexts, and move the simplified method bodies to lexer.h. Generally we try to avoid putting lots of code in headers, but in this case making the code available to the inliner, along with removing the extra layer of abstraction, makes the parser about 20% faster.
The first parser pass is responsible for two things: finding the locations of definitions of top-level module items like globals and functions and finding the locations of implicit function type definitions. It previously accomplished the latter by fully parsing every instruction in each function. But the IR is not constructed in this phase of parsing, so fully parsing every instruction was largely wasted work. Optimize the parser by parsing only the instructions that might have implicit type definitions and otherwise just blindly match parentheses to skip the function body. Combined with #8597, this speeds up parsing by 30-40%.
@tlively tlively requested a review from a team as a code owner April 14, 2026 05:22
@tlively tlively requested review from stevenfontanella and removed request for a team April 14, 2026 05:22
// Consume the next `n` characters.
void take(size_t n) { pos += n; }
void takeAll() { pos = buffer.size(); }

Copy link
Copy Markdown
Contributor

@MaxGraey MaxGraey Apr 14, 2026

Choose a reason for hiding this comment

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

Minor proposal for API:

template<typename F>
inline void takeWhile(F&& pred, size_t n = 1) {
  while (pred()) {
    pos += n;
  }
}

template<typename F>
inline void takeUntil(F&& pred, size_t n = 1) {
  while (!pred()) {
    pos += n;
  }
}

This may simplify some code like this into

takeWhile(idchar);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think there are few enough places where this pattern would apply and they are already simple enough that we probably don't need this abstraction right now. But it's a good idea to keep in mind if we parser more repetitive patterns in the future.

Base automatically changed from parser-slowdown to main April 14, 2026 15:29
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.

2 participants