Repository navigation
perf: avoid per-invocation allocations in the parser builder and value converter - #2268
Conversation
…-langium#2262) The methods built for alternatives, `*`, `+`, `?`, guarded groups, unordered groups and infix rules allocated a new Chevrotain options object (or array of alternatives) and new `ALT`/`DEF`/`GATE` closures on every invocation, only to capture the rule arguments. The options are now built once per grammar site. Their callbacks read the arguments from a slot of that site, which the method sets for the duration of the call and restores afterwards, so a recursive invocation of the same site does not affect the outer one. Chevrotain calls the callbacks synchronously within the call. Also: - rule calls without arguments share one frozen empty arguments object instead of allocating `{}` per call - `ChevrotainWrapper.wrapSubrule` reuses one `{ ARGS: [args] }` object; Chevrotain spreads `ARGS` into the rule invocation synchronously - `buildGroup` iterates with `for...of` instead of a `forEach` closure - `assignWithoutOverride` uses `for...in` with an own-property check instead of `Object.entries` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
…ipse-langium#2262) `runConverter` called `rule.name.toUpperCase()` and, for most rules, `getRuleType(rule)?.toLowerCase()` for every converted token. Both allocate a new string, although the result only depends on the rule. The decision is now computed once per rule and cached in a WeakMap. The precedence (rule name INT/STRING/ID first, then the rule type) is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
|
Rough benchmark numbers for this branch vs.
The workspace benchmark spends most of its time in linking and validation, so the parser saving barely shows there; its build times vary by about ±3 % between runs. 🤖 Generated with Claude Code |
msujew
left a comment
There was a problem hiding this comment.
Thanks, looks good. Just one suggestion, see below.
As suggested in review: the cache and the functions that compute it move into the class (`converterKinds`, `getConverterKind`, `computeConverterKind`, all protected), so subclasses can reuse or override them. Since the cache now lives as long as the converter, it is a plain Map. The kind type is exported as ValueConverterKind for the protected signatures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
Fixes #2262
The parser builder allocated throwaway objects and closures every time a grammar element ran, not once per grammar element.
DefaultValueConverterallocated two strings per converted token. This PR removes both. There are two commits: the parser builder, then the value converter.Parser builder (
parser-builder-base.ts,langium-parser.ts)*,+,?, guarded groups, unordered groups and infix rules used to build a new Chevrotain options object (or array of alternatives) and newALT/DEF/GATEclosures on every call, only so the closures could captureargs. Now the options are built once, when the parser is built. Their callbacks readargsfrom a slot that belongs to that site. A small helper,withArgsSlot, sets the slot for the duration of the call and restores the previous value in afinally.EMPTY_ARGSobject instead of allocating{}per call.ChevrotainWrapper.wrapSubrulereuses one{ ARGS: [args] }object per wrapper instead of allocating it per call.buildGroupiterates withfor...ofinstead of creating aforEachclosure per call.assignWithoutOverrideusesfor...inwith an own-property check instead ofObject.entries, the same pattern as perf: remove per-node allocations on the parse and validation hot paths #2234.The issue's prototype covered alternatives,
*and?. This PR uses the same approach for+(with and without a guard), guarded groups without a cardinality, unordered groups and infix rules, because they had the same per-call allocation.Value converter (
value-converter.ts)runConvertercalledrule.name.toUpperCase()and, for most rules,getRuleType(rule)?.toLowerCase()for every converted token. The result only depends on the rule, so it is now computed once per rule and cached in the converter:DefaultValueConvertergets a protectedconverterKindsmap plus protectedgetConverterKindandcomputeConverterKindmethods, so subclasses can reuse or override the caching and the decision (new exported typeValueConverterKind). The order of the checks is unchanged: first the rule name (INT,STRING,ID, case-insensitive), then the rule type (number,boolean,bigint,date, case-insensitive), otherwise the input is returned unchanged. Subclasses that overriderunConverterare not affected.Why the behaviour is unchanged
ALT,DEFandGATEsynchronously, inside theor/many/option/atLeastOnecall that our method makes. The slot holds the current call'sargsfor exactly that time. In-repetition error recovery re-entersmany/atLeastOnesynchronously too, so it also sees the rightargs.GATE/DEFthen reads its own arguments again. Thefinallyalso restores the slot when a recognition exception propagates.subruleInternalreadsoptions.ARGSand spreads it intocoreRule.apply(this, args)straight away. OnlyLABELis read from the options afterwards, and the recorder only readsLABELas well. So one options object can be reused even for nested subrule calls.chevrotain-allstarand LL(k)) readGATEfrom the passed alternatives on each call and do not cache anything by the identity of the options. So reusing the same options object changes nothing there.GATEis only set on alternatives that have a guard, andGATE: undefinedis still passed for*/?without a guard. As a result, Chevrotain's recording (hasPredicates) sees the same grammar.Behaviour change (for the changelog)
Rule calls without arguments now share one frozen
EMPTY_ARGSobject; before, each got a fresh{}. Code outside Langium that writes to these arguments (custom parser builder code, overridden predicates) used to work and now throws aTypeError, since ES modules run in strict mode. Langium itself never writes toargs, so this only affects such custom code, but it is visible to adopters.Tests
langium-parser-builder.test.ts, new block "Rule arguments in nested and recursive rule calls". Its grammar has one rule per construct (alternatives in*, alternatives in+, guarded*, guarded+, guarded?, guarded group, unordered group, infix rule). Each rule flips a boolean parameter on every nesting level, and a keyword is only accepted for one value of it. The inputs nest each construct in itself and keep parsing the outer construct after the nested one returns. For valid and invalid inputs, the tests check the AST (all properties), the CST (each token with the depth of the AST node it belongs to) and the exact parser errors (offset and message), including the AST and CST after error recovery. The expected values were produced with the code onmain, and the tests pass both onmainand on this branch. A further test parses a valid input, then an invalid one, then the valid one again, and expects the same result.value-converter.test.ts, new block forrunConverter. It covers every rule kind: by name (INT,STRING,ID, and the mixed-caseInt,String,Id), by explicit type (terminal and data type rule), by implicit type (a rule namedNumberorBoolean), and no conversion. Each case is checked on a repeated call, on a fresh converter instance, and with several rules mixed through one instance. It also pins that the name takes precedence over the type:Int returns numberconverts4.5to4.Mutation checks. Each change was applied on its own, the package was rebuilt, and
packages/langium/test/parser/was run:withArgsSlot(all sites)*/+/ guarded+/ infix*DEF, alternativesGATE, unorderedGATE,?GATEor infix operand readsEMPTY_ARGSinstead of the slotwrapSubruledoesn't setARGS[0]assignWithoutOverrideskips every property?, guarded group or unordered groupassignWithoutOverrideThe undetected restores are expected. For those sites Chevrotain reads
GATE/ALT/DEFonly before any nested call, and the enclosing site sets the slot again on every entry. So during normal parsing nothing reads a stale slot. The restore stays for uniformity and so the code doesn't depend on that. The own-property check matchesObject.entriesexactly, but the parser only passes plain object literals there, so it makes no difference in practice.npm run build,npm run lintandnpm test -- packages/langium/pass (62 files, 1165 tests).Effect
From the issue, measured on Langium 4.4.2 with a 7,137-document workspace: one full parse allocates about 1.65 GB before and about 0.96 GB after (−40 %). ASTs, CSTs and errors are byte-identical.
Benchmark numbers for
mainvs. this branch will follow in a comment.The change cuts garbage, not retained memory, so
cst-parse-memory-benchmark.tsis not expected to change.🤖 Generated with Claude Code
https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m