Last active
August 20, 2023 12:30
-
-
Save jdtsmith/7fa6263a13559d587abb51827e6ae472 to your computer and use it in GitHub Desktop.
tree-sitter navigation speed test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let (cnt tm node res) | |
(goto-char (point-min)) | |
(while (< (point) (point-max)) | |
(setq tm | |
(benchmark-run 100 | |
(setq node (treesit-node-at (point))) | |
(while node | |
(setq node (treesit-node-parent node)))) | |
node (treesit-node-at (point))) | |
(setq cnt 0) | |
(while node (setq node (treesit-node-parent node)) (cl-incf cnt)) | |
(setq res (push (cons cnt tm) res)) | |
(beginning-of-line 2)) | |
(with-output-to-temp-buffer "*ts-test-output*" | |
(cl-loop for (cnt tau ngc tgc) in (nreverse res) | |
for i from 1 | |
do (princ (format "%d\t%d\t%f\t%d\t%f\n" | |
i cnt (* tau 10) ngc (* tgc 10)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
plt.clf() | |
d=np.loadtxt('test.dat') | |
sc=plt.scatter(d[:,0],d[:,2], c=d[:,1], alpha=0.5, s=60, cmap='rainbow', linewidths=0) | |
plt.xlabel('Line Number') | |
plt.ylabel('Root Navigation Time (ms)') | |
plt.title('Tree Sitter Navigation Time\nfrom start of each line in _axes.py') | |
plt.colorbar(sc, label='Node Depth') | |
plt.xscale('log') | |
plt.yscale('log') | |
plt.tight_layout() | |
first = d[d[:,0]<=10,:] | |
fac = np.mean(np.sqrt(first[:,0])/first[:,2]) | |
plt.plot([1,1e4],np.sqrt([1,1e4])/fac, label='$t\propto\sqrt{N}$') | |
plt.legend() | |
plt.savefig('ts.png', dpi=300) |
An updated patch along these lines, which trims the tree search using both the start and end coverage position of nodes:
This has similar performance and scaling as the simple node_parent
version above. Looks like the right solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have applied a patch developed by Dimitry Gutov, which uses
ts_node_parent
, vs. the originalts_tree_cursor_goto_parent
, on a freshly compiled NS build ofmain.
The results are striking:With
ts_tree_cursor_goto_parent
(current design ofnode-parent
):This looks quite similar to the emacs-29 results.
With
ts_node_parent
(Dimitry's patch):Note the reduced y range and much shallower scaling. This indicates that the scaling and long navigation time at the end of the file are related to the cursor walk, not the parent search from root per se. The patched version results also make more sense in terms of their similar logarithmic growth as node-at-point, since the method of search for a node at point and for its parent is quite similar.