treesit-auto icon indicating copy to clipboard operation
treesit-auto copied to clipboard

Slows down loading, even of text files

Open egh opened this issue 7 months ago • 4 comments

I was noticing that my file loading in emacs had become quite slow. It seems that treesit-auto mode may be part of the issue.

The following is my benchmarking result:

(global-treesit-auto-mode 1)
(benchmark-run 1 (find-file "x.txt"))
=> (1.84767 0 0.0)

versus:

(global-treesit-auto-mode -1)
(benchmark-run 1 (find-file "x.txt"))
=> (0.047460999999999996 0 0.0)

It would be nice if this were faster. Is there a workaround, at least, where we can only enable this for prog-mode buffers?

Thank you!

egh avatar Jul 24 '25 17:07 egh

Thanks for making this issue, I have been waiting through multiple seconds of latency to jump between wikilinked markdown notes, and I had no idea which package was at fault until I saw this and tried disabling it. I'll be sure to manually configure my treesitter modes from now on and avoid this package.

leaferiksen avatar Sep 03 '25 23:09 leaferiksen

Since macOS Sequoia, I notice every file I open takes about 2s and the CPU profiler shows that most of the time is spend in treesit-auto--set-major-remap in after-find-file:

        2481  97% - command-execute
        2248  88%  - funcall-interactively
        2248  88%   - find-file
        2248  88%    - find-file-noselect
        2247  88%     - find-file-noselect-1
        2246  88%      - after-find-file
        2245  88%       - normal-mode
        2235  87%        - set-auto-mode
        1480  58%         - set-auto-mode-0
        1480  58%          - apply
        1480  58%           - treesit-auto--set-major-remap
        1480  58%            - treesit-auto--build-major-mode-remap-alist
        1480  58%             - treesit-auto--ready-p
        1480  58%              - treesit-ready-p
        1480  58%                 treesit-language-available-p
         755  29%         - set-auto-mode--apply-alist
         755  29%          - set-auto-mode-0
         755  29%           - apply
         737  28%            - treesit-auto--set-major-remap
         737  28%             - treesit-auto--build-major-mode-remap-alist
         737  28%              - treesit-auto--ready-p
         737  28%               - treesit-ready-p
         737  28%                  treesit-language-available-p

Jeremy Friesen suggests to cache the lookup of the expensive treesit-language-available-p call: https://github.com/jeremyf/dotemacs/blob/75410e2f56273b2be4abf10d0d72627ec4ad6a85/emacs.d/init.el#L5176-L5197

That works extremely well for me.

DivineDominion avatar Sep 20 '25 05:09 DivineDominion

So as a suggestion, treesit-auto--set-major-remap could also employ a cache maybe

DivineDominion avatar Sep 20 '25 05:09 DivineDominion

I see the same. I mainly notice it when using consult-ripgrep and navigating through candidates. (global-treesit-auto-mode -1) makes it immediately fast, again.

       14380  96% - ...
       14334  95%  - completing-read
       14334  95%   - completing-read-default
       14334  95%    - apply
       14334  95%     - vertico--advice
       14334  95%      - apply
       14334  95%       - #<interpreted-function E69>
       14334  95%        - progn
       14334  95%         - unwind-protect
       14334  95%          - let
       14334  95%           - let
       14334  95%            - let
       14328  95%             - unwind-protect
       14328  95%              - progn
       14047  93%               - apply
       14047  93%                - #<byte-code-function 9D3>
       14047  93%                 - read-from-minibuffer
       13327  89%                  - vertico--exhibit
       13327  89%                   - vertico--protect
       13327  89%                    - condition-case
       13327  89%                     - progn
       13327  89%                      - handler-bind-1
       13315  88%                       - #<interpreted-function 248>
       13315  88%                        - funcall
       13315  88%                         - #<interpreted-function 3D0>
       13315  88%                          - let
       13315  88%                           - vertico--display-candidates
       13298  88%                            - apply
       13294  88%                             - #<byte-code-function F36>
       13294  88%                              - apply
       13294  88%                               - #<interpreted-function 243>
       13294  88%                                - progn
       13294  88%                                 - let
       13293  88%                                  - vertico-posframe--show
       13293  88%                                   - let
       13293  88%                                    - save-current-buffer
       13293  88%                                     - unwind-protect
       13293  88%                                      - progn
       13284  88%                                       - let*
       13284  88%                                        - if
       13284  88%                                         - progn
       13284  88%                                          - if
       13284  88%                                           - if
       13284  88%                                            - funcall
       13284  88%                                             - #<interpreted-function 8A3>
       13284  88%                                              - funcall
       13282  88%                                               - consult--grep-position
       13282  88%                                                - if
       13282  88%                                                 - progn
       13282  88%                                                  - let*
       13281  88%                                                   - let*
       13281  88%                                                    - and
       13281  88%                                                     - consult--marker-from-line-column
       13281  88%                                                      - funcall
       13281  88%                                                       - #<interpreted-function 662>
       13281  88%                                                        - if
       13281  88%                                                         - let
       13260  88%                                                          - or
       13260  88%                                                           - let*
       13254  88%                                                            - and
       13254  88%                                                             - consult--find-file-temporarily
       13254  88%                                                              - let
       13254  88%                                                               - condition-case
       13254  88%                                                                - unwind-protect
       13254  88%                                                                 - progn
       13254  88%                                                                  - consult--find-file-temporarily-1
       13254  88%                                                                   - let*
       13253  88%                                                                    - if
       13253  88%                                                                     - let*
       13250  88%                                                                      - if
       13249  88%                                                                       - find-file-noselect
       13229  88%                                                                        - find-file-noselect-1
       13225  88%                                                                         - after-find-file
       13224  88%                                                                          - normal-mode
       13224  88%                                                                           - set-auto-mode
       13203  88%                                                                            - set-auto-mode--apply-alist
       13202  88%                                                                             - set-auto-mode-0
       13202  88%                                                                              - apply
       13152  87%                                                                               - treesit-auto--set-major-remap
       13152  87%                                                                                - set
       13152  87%                                                                                 - treesit-auto--build-major-mode-remap-alist
       13152  87%                                                                                  - append
       13152  87%                                                                                   - let
       13152  87%                                                                                    - let*
       13152  87%                                                                                     - while
       13152  87%                                                                                      - if
       13152  87%                                                                                       - treesit-auto--ready-p
       13152  87%                                                                                        - let*
       13135  87%                                                                                         - if
       13135  87%                                                                                          - and
       13135  87%                                                                                           - treesit-ready-p
       13032  87%                                                                                              treesit-language-available-p
          90   0%                                                                                              format
           1   0%                                                                                              mapconcat
          17   0%                                                                                         + and
          50   0%                                                                               + #<byte-code-function FCD>
           1   0%                                                                               file-name-case-insensitive-p
          21   0%                                                                            + set-auto-mode-0
           1   0%                                                                          + auto-save-mode
           3   0%                                                                           insert-file-contents
           1   0%                                                                         + abbreviate-file-name
          10   0%                                                                        + find-buffer-visiting
           7   0%                                                                        + create-file-buffer
           2   0%                                                                        + file-truename
           1   0%                                                                          file-directory-p
           1   0%                                                                       + let*
           3   0%                                                                      + unwind-protect
           1   0%                                                                    + and
           6   0%                                                            + if
          21   0%                                                          + setq
           2   0%                                               + #<interpreted-function 3EE>
           9   0%                                       + apply
           1   0%                                  + vertico-posframe--handle-minibuffer-window
           4   0%                             + #<interpreted-function CDB>
          17   0%                            + vertico--arrange-candidates
           6   0%                       + #<interpreted-function 80E>
           3   0%                       + #<interpreted-function 605>
           1   0%                       + #<interpreted-function 90F>
           1   0%                       + #<interpreted-function A21>
           1   0%                       + #<interpreted-function 395>
         304   2%                  + redisplay_internal (C function)
         203   1%                  + timer-event-handler
           3   0%                  + minibuffer-inactive-mode
           3   0%                  + #:consult--preview-post-command-hook
           2   0%                  + evil-esc
           2   0%                  + minibuffer-mode
           1   0%                  + command-execute
           1   0%                  + vertico--prepare
           1   0%                    savehist-minibuffer-hook
           1   0%                  + vertico-posframe--minibuffer-exit-hook
         281   1%               + prescient--add-sort-info
           6   0%             + list
          46   0%  + #<interpreted-function 8EA>
         372   2%   Automatic GC
         155   1% + timer-event-handler
          51   0% + redisplay_internal (C function)
          13   0% + #<byte-code-function 4D9>
           1   0% + mode-local-post-major-mode-change
           1   0% + command-execute

mpereira avatar Nov 19 '25 18:11 mpereira