Subject: bug#78697: `treesit-major-mode-setup` causing substantial pause when finding a file

From: Eli Zaretskii <[email-masked]>
Date: Thu, 05 Jun 2025 17:03:10 +0300

> Date: Thu, 05 Jun 2025 13:55:05 +0000
> From: alexis purslane <[email-masked]>
> Cc: [email-masked]
> 
> I don't have the original profile anymore, but it was easy enough to reproduce. Here's that reproduced version:
> 
>          481  80% - command-execute
>          481  80%  - funcall-interactively
>          481  80%   - god-mode-self-insert
>          262  44%    - funcall-interactively
>          261  43%     - project-find-file
>          261  43%      - project-find-file-in
>          205  34%       - find-file
>          205  34%        - find-file-noselect
>          202  34%         - find-file-noselect-1
>          200  33%          - after-find-file
>          195  32%           - normal-mode
>          189  31%            - set-auto-mode
>          188  31%             - set-auto-mode--apply-alist
>          188  31%              - set-auto-mode-0
>          188  31%               - clojure-ts-clojurescript-mode
>          164  27%                - clojure-ts-mode
>          114  19%                 - treesit-major-mode-setup
>          114  19%                  - treesit-validate-font-lock-rules
>           91  15%                     treesit-query-compile
>           23   3%                   clojure-ts--ensure-grammars
>           10   1%                 + treesit-ready-p
>            1   0%                 + clojure-ts-mode-variables
>           24   4%                + run-mode-hooks
>            1   0%             + hack-dir-local--get-variables
>            6   1%            + font-lock-mode
>            5   0%           + run-hooks
>            2   0%            insert-file-contents
>            1   0%           abbreviate-file-name
>            1   0%         + file-truename
>            1   0%         + find-buffer-visiting
>           52   8%       + project--read-file-name
>            4   0%       + project-files
>            1   0%     + execute-extended-command
>          122  20%    - god-mode-lookup-key-sequence
>           85  14%     - god-mode-lookup-command
>           85  14%      - apply
>           85  14%       - which-key--god-mode-lookup-command-advice
>           85  14%        + #<native-comp-function god-mode-lookup-command>
>           37   6%     + god-key-string-after-consuming-key
>           97  16%    + byte-code
>           91  15%   redisplay_internal (C function)
>           11   1% + ...
>           10   1% + timer-event-handler
>            1   0% + which-key--hide-popup

Thanks, this seem to say that a large portion of time is spent inside
treesit-query-compile.

> Also, I didn't original report this to the clojurescript-ts-mode folks because (1) since this branched into built-in functions it seemed like an emacs problem, and compoundingly (2) in one of the earlier iterations of my testing I was seeing this performance issue across all files that would load tree-sitter modes; however, I just did another couple tests and profiles with rust, bash, and TSX and didn't see it anymore, so I'll go poke them about it now because it does seem like more of their problem.

It is quite possible that clojure-ts-clojurescript-mode defines some
mode-specific data that makes treesit-query-compile do some heavy
lifting.  So I think you should report this to the mode developers.