Skip to content

Collect first builder after - #1749

Draft
Janther wants to merge 9 commits into
mainfrom
collect-first-builder-after
Draft

Janther wants to merge 9 commits into
mainfrom
collect-first-builder-after

Conversation

@Janther

@Janther Janther commented Oct 2, 2026

Copy link
Copy Markdown
Member

Every call into Slang's wasm that returns an object has to build a new JavaScript object from scratch. Each node's constructor called children() on its own Slang node, creating an array and a new object for every child. Instead, we now walk the tree once and collect all the metadata (locations and comments) before calling the constructors; SlangNode's constructor is now just a lookup.

There is a 10% improvement in the parsing speed after these changes and the constructor for each node is much cleaner.

Important changes:

  • Instead of calling node.children() at each constructor, we walk the whole tree once using a Cursor.

    // This:
    for (const { node } of cst.children()) {
      ... 
    }
    // becomes:
    do {
      ...
    } while (cursor.goToNextSibling())

    This way we avoid creating an array of new JavaScript objects for each parent node.

  • Instead of calling node.textLength on every NonterminalNode, and since we are already recursively walking the tree, we just use the pointer avoiding another call to the wasm.

    // This:
    for (const { node } of cst.children()) {
      const textLength = node.textLength.utf16;
      ...
    }
    // becomes:
    visitNonterminal(cursor, id, collected, offset);
      do {
        const node = cursor.node;
        if (node.isNonterminalNode()) {
          offset = visitNonterminal(cursor, node.id, collected, offset);
          ...
          continue;
        }
        const textLength = node.textLength.utf16;
        const end = offset + textLength;
        ...
        offset = end;
      } while (cursor.goToNextSibling())
      ...
      return offset;
    }

@Janther
Janther requested a review from fvictorio October 2, 2026 18:56
@Janther Janther added the performance Results in smaller size, better memory usage, or faster execution label Oct 2, 2026
@Janther Janther mentioned this pull request Oct 6, 2026
@Janther

Janther commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

parking until #1756 is merged

@Janther
Janther marked this pull request as draft October 6, 2026 17:43
@Janther
Janther force-pushed the collect-first-builder-after branch from 5c9d4d9 to f53558e Compare October 13, 2026 15:58
@Janther
Janther force-pushed the collect-first-builder-after branch from f53558e to 0e4a7af Compare October 19, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Results in smaller size, better memory usage, or faster execution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant