Remove unnecessary allocator trampoline #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, every allocation from tree-sitter calls back to a Go shim for alloc/calloc/realloc/free operations. If
SetAllocator
is called with all nil values (the default), this has the effect of callingC.{alloc,calloc,realloc,free}
and keeps all memory operations on the C side. However, this also incurs a C -> Go -> C boundary crossing for every allocation and free, which can be massively expensive when parsing at scale.This PR changes the behavior of
SetAllocator
to remove the C -> Go -> C trampoline when there are no Go-side shims to execute. If any of the allocation parameters are non-nil, we'll fall back to the current behavior of installing shims for all four methods. I'm assuming common use of this is to replace all of these methods uniformly.On a representative parse over 30,940 files, I see a decrease from 22,615ms to 5,517ms with this simple change. And see the following disappear completely from the CPU profile: