Skip to content

CST memory optimization#2191

Open
spoenemann wants to merge 10 commits into
mainfrom
cst-memory-opt
Open

CST memory optimization#2191
spoenemann wants to merge 10 commits into
mainfrom
cst-memory-opt

Conversation

@spoenemann

@spoenemann spoenemann commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

This PR aims at improving Langium's memory usage, which was found to be relatively high in this article. The Conrete Syntax Tree (CST) implementation is the main reason for this. These measures have been applied:

  • root is no longer stored in all CST nodes, but derived by walking up the container hierarchy.
  • range is no longer stored in leaf CST nodes, but created on demand from integer values.
  • Optional properties (especially _astNode and _rangeCache) are initialized with undefined so the V8 engine includes them in the finalized object layout (this avoids the overhead of a separate PropertyArray for properties added later).
  • content is now a readonly array and must be manipulated via the CstNodeBuilder so we avoid the CstNodeContainer overhead.

The first two changes save memory by doing a bit more computation for the root, text, and range properties, which mostly affects LSP-related services. The overhead of these computations is virtually negligible, and the benefits of the changes outweigh it by far as shown in the benchmarks below.

I created four benchmarks to measure the impact of these changes on the CST size, parsing, and document building:

  • cst-node-memory-benchmark.ts measures the memory usage of a synthetically generated CST
    • → −49 % memory compared to main 🎉
  • cst-parse-memory-benchmark.ts measures the memory usage of a parsed CST
    • → −45 % memory compared to main 🎉
  • parser-time-benchmark.ts measures the execution time of the parser
    • → −33 % parse time compared to main 🎉
  • workspace-build-benchmark.ts measures the execution time and heap size of a full workspace build
    • → −14 % build time and −19 % heap size compared to main 🎉

@spoenemann
spoenemann requested a review from msujew July 22, 2026 15:58
@cdietrich

cdietrich commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

in my measurements we see only 35% memory gain (on the total heap) but this is still a huge improvement. for pure cst it is almost 50%

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