diff --git a/src/treesit.c b/src/treesit.c index 917db582676..69f54976509 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -2722,6 +2722,7 @@ DEFUN ("treesit-query-capture", bottleneck (98.4% of the running time spent on nconc). */ Lisp_Object result = Qnil; Lisp_Object prev_result = result; + Lisp_Object predicates_for_0 = NULL; while (ts_query_cursor_next_match (cursor, &match)) { /* Record the checkpoint that we may roll back to. */ @@ -2750,9 +2751,18 @@ DEFUN ("treesit-query-capture", result = Fcons (cap, result); } /* Get predicates. */ - Lisp_Object predicates - = treesit_predicates_for_pattern (treesit_query, - match.pattern_index); + Lisp_Object predicates; + if (match.pattern_index == 0) + { + if (predicates_for_0 == NULL) + predicates_for_0 = treesit_predicates_for_pattern (treesit_query, 0); + + predicates = predicates_for_0; + } + else + { + predicates = treesit_predicates_for_pattern (treesit_query, match.pattern_index); + } /* captures_lisp = Fnreverse (captures_lisp); */ struct capture_range captures_range = { result, prev_result };