Skip to main content

Module ast_path_trie

Module ast_path_trie 

Source
Expand description

A compact, shared representation of [AstParentKind] paths.

Code generation identifies the AST node it wants to patch by the path of [AstParentKind]s leading to it from the program root. Stored flat, those paths can be quadratic in expression nesting depth: a left-leaning a() + b() + c() + ... chain of N terms produces N paths of lengths 2, 4, ..., 2N, because every additional term nests one level deeper. Pathological, often generated, code can hit this. This module removes the redundancy by storing

Even in typical code shared prefixes are common:

import {a,b} from '...'
...
export function foo() {
  if (a()) return b();
}

Silly but the binding usage location for a and b share a common prefix. That prefix can get arbitrarily long and this structure fixes.

Paths are added through AstPathTrieBuilder, which holds the index needed to deduplicate them, and it is then frozen into an immutable AstPathTrie. Only requested paths are interned, so the trie stays sparse: it holds the handful of nodes leading to code-generated locations, not every node in the file.

Structsยง

AstPathId
A reference to a path interned in an AstPathTrie.
AstPathTrie
An immutable arena of [AstParentKind] paths that share their common prefixes.
AstPathTrieBuilder
Accumulates [AstParentKind] paths, sharing their common prefixes.