Skip to content

perf: answer isRecording() from a field once self-analysis is done - #2271

Merged
cdietrich merged 3 commits into
eclipse-langium:mainfrom
cdietrich:perf/2266-is-recording
Nov 18, 2026
Merged

cdietrich merged 3 commits into
eclipse-langium:mainfrom
cdietrich:perf/2266-is-recording

Conversation

@cdietrich

@cdietrich cdietrich commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2266

The parser checks isRecording() on every token and rule call (9 call sites in langium-parser.ts, 2 in parser-builder-base.ts), and each check asked the Chevrotain wrapper for IS_RECORDING. Chevrotain only records while performSelfAnalysis() runs, which Langium calls in finalize(). Afterwards the answer is always false.

AbstractLangiumParser now remembers whether finalize() has completed the self-analysis and answers isRecording() from that field alone (!this.selfAnalysisDone); the wrapper's IS_RECORDING getter is removed. This is equivalent because every caller of isRecording() sits in a rule body or parser action, and rule bodies only run while Chevrotain records the grammar during finalize(), or while parsing afterwards. A repeated finalize() resets the field first, so it records again. Subclasses that override finalize() must call super.finalize().

No dedicated tests: the existing parser tests cover isRecording() broadly. Making it always false, or true after finalize(), fails 408 of them.

Effect, measured on Langium 4.4.2 with a large DSL (details in #2266): about −2 % CPU for building a workspace and −1 to −4 % for parsing single documents, no change in allocation. It's a small gain; the rough numbers for this branch are in the comment below.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m

cdietrich and others added 2 commits November 17, 2026 13:14
The parser checks isRecording() on every token and rule call. Chevrotain only
records while finalize() runs its self-analysis, so remember when that is done
and stop asking the Chevrotain wrapper afterwards. Before and during finalize()
the wrapper is still asked; a repeated finalize() resets the field first.

Fixes eclipse-langium#2266

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
…() asking the wrapper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
@cdietrich

Copy link
Copy Markdown
Contributor Author

Rough benchmark numbers for this branch vs. main (da1beac): Node 24 on a laptop that wasn't idle, so ballpark figures rather than a careful benchmark. Allocation, scavenges and GC time are process totals, recorded with a small --import preload using v8.GCProfiler (allocation estimated as the heap before each GC minus the heap after the previous one).

packages/langium/test/parser/parser-time-benchmark.ts (~1.5 MB input, 30 measured parses; totals include 10 warm-up parses and 10 lexer runs), 2 runs each:

median parse allocated scavenges
main 625 ms 38.7 GB 629
this branch 628–634 ms 38.7 GB 630

No measurable effect in this benchmark: the difference is within run-to-run noise. Consistent with the description, it's a small gain at best, and it doesn't change allocation.

🤖 Generated with Claude Code

… suite

As suggested in review: rule bodies only run while Chevrotain records the grammar during the
self-analysis in finalize(), or while parsing afterwards, so `!selfAnalysisDone` alone answers
isRecording() and the wrapper's IS_RECORDING getter is no longer needed. Subclasses that
override finalize() must call super.finalize(). The dedicated test suite is removed: the existing
parser tests fail broadly if isRecording() answers wrongly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m

@msujew msujew left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@cdietrich
cdietrich merged commit d8942d3 into eclipse-langium:main Nov 18, 2026
4 checks passed
@cdietrich
cdietrich deleted the perf/2266-is-recording branch November 18, 2026 10:32
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.

Perf: LangiumParser.isRecording() asks the Chevrotain wrapper on every token and rule call

2 participants