Skip to content

feat: replace node pointer in place for much cheaper AST traversal#48

Open
jkumz wants to merge 1 commit into
tree-sitter:masterfrom
jkumz:master
Open

feat: replace node pointer in place for much cheaper AST traversal#48
jkumz wants to merge 1 commit into
tree-sitter:masterfrom
jkumz:master

Conversation

@jkumz

@jkumz jkumz commented Dec 28, 2025

Copy link
Copy Markdown

This is a small addition that can make a significant dent in Go GC pressure under certain conditions. In something I'm building I was able to reduce my *Node allocations by 96%.

The standard pattern of calling cursor.Node() at each position allocates a new *Node wrapper. On certain batch jobs on very large codebases this can accumulate a lot of allocations.

It's additive so no compatibility issues with pre-existing implementations.

// Basic DFS traversal with node reuse
node := rootNode 
cursor := node.Walk()
defer cursor.Close()
current := cursor.Node() // allocate once
for {
    // Process current node
    doSomething(current)
    
    if cursor.GotoFirstChild() {
        cursor.UpdateNode(current) // update in place, no allocation
        continue
    }
    for !cursor.GotoNextSibling() {
        if !cursor.GotoParent() {
            return
        }
    }
    cursor.UpdateNode(current)
}```

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.

1 participant