Repository navigation
perf: avoid per-node promises, closures and arrays in the validation walk - #2269
Conversation
`interruptAndCheck` is async, so every call creates a promise and every await costs a microtask, although it only yields once per interruption period and only throws once the token is cancelled. `interruptionDue` evaluates the same conditions synchronously, so that loops can await `interruptAndCheck` only when it would yield or throw. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
…walk Fixes eclipse-langium#2264. - `DefaultDocumentValidator.validateAstNodes` awaits `interruptAndCheck` only when `interruptionDue` says it would yield or throw. It yields and stops on cancellation at the same nodes as before. - `ValidationRegistry.wrapValidationException` calls the check directly. The wrapped check returns a promise only if the check returned one or threw; errors still go through `handleException` and are reported as before, and `OperationCancelled` is still rethrown. - The walk awaits a check only if it returned something, and pushes the children of a node directly onto the stack and reverses that part of the stack, instead of building a temporary array per node. The visiting order is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
…d interruptionDue Both functions now read the "interruption period elapsed" condition from one private helper, so that they can't drift apart. `interruptAndCheck` behaves as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
…ode in document order `pushChildrenInDocumentOrder` pushes the children of a node onto an explicit stack and reverses that part of the stack, so that they are popped in the order of `streamContents`, without a temporary array. It is meant for the stack-based walks of the validator and, later, the linker and the reference index, so that they share one implementation. The helper lives in the new module `utils/ast-walk.ts`, which is not re-exported from `utils/index.ts`, so it does not become part of `AstUtils` or the public API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m
Replaces the inline child-push block of `DefaultDocumentValidator.validateAstNodes` with the shared internal helper. The visiting order is unchanged. 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 gain is small in this benchmark, and the time difference is within its noise (about ±3 % between runs). Each of its builds allocates about 41 GB and takes about 19 s, mostly because of the deliberately unresolved references, which is not per-node overhead. The −17 % in #2264 was measured on a workspace with much more validation per AST node, where the per-node promises and closures make up a larger share. 🤖 Generated with Claude Code |
|
I'll have a closer look at this once #2273 lands, that should reduce noise from the changes. |
…walk # Conflicts: # packages/langium/test/utils/ast-walk.test.ts
Fixes #2264
DefaultDocumentValidator.validateAstNodescreated a promise per AST node forawait interruptAndCheck(), two async frames, two promises and a closure per node and check for the check wrapper, and a temporarychildrenarray per node. Almost all checks are synchronous, so nearly all of this was garbage.With #2273 merged, the shared building blocks are already on
main:interruptionDue(token)and its shared period check inpromise-utils.ts, andAstUtils.pushChildrenInDocumentOrder. This PR now only changes the validator and the check wrapper.Changes:
ValidationRegistry.wrapValidationExceptioncalls the check directly. The wrapped check returnsundefinedif the check returned anything that is not a thenable, and a promise if the check returned a thenable or threw. Errors still go throughhandleException, with the same message and logging, andOperationCancelledis still rethrown. A synchronous error is reported before the wrapper returns, as before.validateAstNodesawaitsinterruptAndCheckonly wheninterruptionDueis true, and awaits a check only if it returned something. It pushes the children of a node withpushChildrenInDocumentOrder, directly onto the stack, so it needs no temporary array.What stays the same:
validateSingleNodeOptionsprunes the same subtrees.OperationCancelledat the next node after the token was cancelled.Errorthrows andOperationCancelled(sync or async) are reported or passed on as before.Observable differences:
handleExceptionstill sees every error. It no longer sees successful synchronous checks. In the issue I asked whether that matters; routing the errors throughhandleExceptionkeeps the override point that matters for error reporting.getChecks/getCheckArrayreturn checks that may returnundefined. Their type wasMaybePromise<void>already. Code that calls.thenon a check's result without checking would break; I found no such caller in this repository.Tests:
test/validation/document-validator-walk.test.ts: visit order with a node that has two child properties, and pruning; reporting of sync throws, async results, async rejections, async throws and string throws, with the walk continuing; an async check finishing before the next check and the next node;OperationCancelledthrown by a check, sync and async; cancellation during the walk; an event-loop turn during a 40 ms walk; the whole walk running without awaiting when all checks are synchronous (withCancellationToken.Noneand with a token while no interruption is due); the wrapper returning a promise only for async checks; errors going to an overriddenhandleException;OperationCancelledrethrown by the wrapper. All of them except the "without awaiting" ones and "returns a promise only if the check returned one" also pass onmain, so they pin the existing behaviour; those three fail onmain, so they guard the allocations. (The tests forinterruptionDueandpushChildrenInDocumentOrderlanded with #2273.)Effect: measured on Langium 4.4.2 with a large DSL (details in #2264), the validator changes together with
interruptionDuesave about 1.5 GB of allocations per full pass (−17 %), with identical diagnostics. Rough benchmark numbers formainvs. this branch are in the comment below.🤖 Generated with Claude Code
https://claude.ai/code/session_01XkvVhAAk5NRLH75qMHMc3m