-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(treesitter): bound has-ancestor?
for highlighter
#27783
Conversation
I'm not a fan of arbitrary magic constants (we had similar PRs killed by that). Can't we expose the number instead by, e.g., having |
I don't think we should do this as it breaks correctness. Ideally the grammars should be rewritten so they don't create nested structures for if-else statements. |
Filed an issue there. tree-sitter/tree-sitter-c#198. note: other languages (e.g., rust) have similar issue. |
#24965 (comment)
I think distance limit is good enough even if it can't be totally correct since the worst thing that could happen is just some highlights going slightly off. Also, this would be very rare because the most uses of |
9227229
to
f7c8e69
Compare
I still think a global limit is not acceptable (even though I mentioned that initially; the analogy with What's the argument against making this explicit in the directive? |
I'm not against per-directive limits, and I'm happy to implement that. |
Problem: `has-ancestor?` predicate can take long when the match is deep in the tree, e.g., long `else if` chain in C. This may slow down highlighting and thus redrawing significantly. Solution: Add option for limiting `has-ancestor?`'s search length, and use it for highlighter. The limit `15` is arbitrary, but seems to be good enough: * Existing use of `has-ancestor?` in highlight queries are quite local. For example in cpp, it searches for a call expression above a quantified identifier to highlight the function name. * Scrolling is reponsive enough in the long `else if` chain example.
f7c8e69
to
8195c1e
Compare
That's a different approach than the one I suggested (which would allow per-language and per-pattern configuration, and also consolidate |
Oh, sorry for confusion. I haven't worked on it yet, and the force push was just for resolving the conflicts. But I think we haven't reached consensus on whether there should be any kind of arbitrary limits, be it global or the per-pattern. @lewis6991 What do you think about per-pattern limits like By the way, there is an ongoing work on changing the grammar itself: tree-sitter/tree-sitter-c#199 |
This might not be needed with tree-sitter/tree-sitter#3214 (review) which should provide even more optimization opportunities. |
I thought that we removed has-ancestor from the C family nesting limited copy&paste. But I haven't been a around for a long time and it is now actively used again. Ideally, tree-sitter should answer whether this is a common usecase that should be expressible by the queries or whether this is something that users should avoid. With tree-sitter/tree-sitter#3214 (review) tree-sitter could send the message that requesting parent is something you can do. |
Closing, since (1) the other solution is much nicer, (2) the linear factor on the depth of the node is not avoidable, and (3) this factor can be bounded by |
Problem:
has-ancestor?
predicate can take long when the match is deep in the tree, e.g., longelse if
chain in C. This may slow down highlighting and thus redrawing significantly.See #24965.
Solution:
Add option for limiting
has-ancestor?
's search length, and use it for highlighter.The limit
15
is arbitrary, but seems to be good enough:has-ancestor?
in highlight queries are quite local. For example in cpp, it searches for a call expression above a quantified identifier to highlight the function name.else if
chain example.See #24965 for other approaches.