SyntaxArena

A syntax arena owns the memory for all syntax nodes within it.

SyntaxArena.swift:46
class SyntaxArena

The following is only relevant if you are accessing the raw syntax tree using RawSyntax nodes. When working with syntax trees using SwiftSyntax’s API, the usage of a SyntaxArena is transparent.

Contrary to Swift’s usual memory model, syntax node’s are not individually reference-counted. Instead, they live in an arena. That arena allocates a chunk of memory at a time, which it can then use to store syntax nodes in. This way, only a single memory allocation needs to be performed for multiple syntax nodes and since memory allocations have a non-trivial cost, this significantly speeds up parsing.

As a consequence, syntax nodes cannot be freed individually but the memory will get freed once the owning SyntaxArena gets freed. Thus, it needs to be manually ensured that the SyntaxArena is not deallocated while any of its nodes are being accessed. The SyntaxData type ensures this as follows:

  • The root node has a strong reference to its SyntaxArena

  • Each node retains its parent SyntaxData, thus keeping it alive.

  • If any node is allocated within a different SyntaxArena, that arena is added to the root’s childRefs property and thus kept a live as long as the parent tree is alive.

As an added benefit of the SyntaxArena, RawSyntax nodes don’t need to be reference-counted, further improving the performance of SwiftSyntax when worked with at that level.