GNU bug report logs - #78697
`treesit-major-mode-setup` causing substantial pause when finding a file

Previous Next

Package: emacs;

Reported by: alexis purslane <alexispurslane <at> pm.me>

Date: Thu, 5 Jun 2025 01:29:02 UTC

Severity: normal

To reply to this bug, email your comments to 78697 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#78697; Package emacs. (Thu, 05 Jun 2025 01:29:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to alexis purslane <alexispurslane <at> pm.me>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 05 Jun 2025 01:29:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: alexis purslane <alexispurslane <at> pm.me>
To: "bug-gnu-emacs <at> gnu.org" <bug-gnu-emacs <at> gnu.org>
Subject: `treesit-major-mode-setup` causing substantial pause when finding a
 file
Date: Thu, 05 Jun 2025 01:27:58 +0000
[Message part 1 (text/plain, inline)]
# Problem

As the title says: `treesit-major-mode-setup` appears to be causing a substantial pause when finding a file that involves tree sitter modes (that I have installed). I'm unclear s to why.

# Detailed Information

## System

Apple Mac Studio M1 Max running macOS 15.5 Sequoia (`aarch64-apple-darwin24.5.0`) (temporarily using it instead of my Linux machine for work reasons)

## Emacs

I've tried a few variations of Emacs to root out the problem:

### emacs-plus <at> 31 from Homebrew

System Configuration Options: `--disable-dependency-tracking --disable-silent-rules --enable-locallisppath=/opt/homebrew/share/emacs/site-lisp --infodir=/opt/homebrew/Cellar/emacs-plus <at> 31/31.0.50/share/info/emacs --prefix=/opt/homebrew/Cellar/emacs-plus <at> 31/31.0.50 --with-native-compilation=aot --with-xml2 --with-gnutls --without-compress-install --without-dbus --without-imagemagick --with-modules --with-rsvg --with-webp --with-ns --disable-ns-self-contained 'CFLAGS=-O2 -DFD_SETSIZE=10000 -DDARWIN_UNLIMITED_SELECT'`

System Configuration Features: `ACL GIF GLIB GMP GNUTLS JPEG LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE NS PDUMPER PNG RSVG SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM ZLIB`

#### Issues:

- Random freezes all the time
- Substantial freezes when opening a file

### `feature/igc` emacs 31.0.50 from HEAD

System Configuration Options: `--prefix=/usr/local --with-tree-sitter --with-xwidgets --with-ns --with-native-compilation=aot --with-imagemagick --without-compress-install --with-small-ja-dic --without-selinux --without-gsettings CC=clang 'CFLAGS=-O3     -march=native`

System Configuration Features: `ACL GLIB GNUTLS IMAGEMAGICK LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE NS PDUMPER PNG RSVG SQLITE3 THREADS TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM XWIDGETS ZLIB`

#### Issues

- Thanks to the experimental MPS garbage collector, the random intermediate pauses have gone away (although it's still odd they were there – even without MPS my Emacs configuration doesn't pause randomly on Linux)
- However, while shorter, the pauses upon opening a new file are the same.

### same as above but with `treesit-auto` disabled

Profiling the previous two versions seemed to indicate that a function in the `treesit-auto` package was to blame, so I disabled `treesit-auto-mode` to see if that made a difference; it did not. However, it narrowed down the issue further:

#### Issues

- There is no undue pause when opening any other kind of file; only tree-sitter enabled files are so slow.

- Most time opening a file (38%) is spent within `treesit-major-mode-setup`:

```
    112,106,431  95% - command-execute
...
     84,918,927  72%     - find-file
     84,916,421  72%      - find-file-noselect
     84,902,072  72%       - find-file-noselect-1
     84,852,273  72%        - after-find-file
     84,735,710  72%         - normal-mode
     80,077,055  68%          - set-auto-mode
     79,933,703  68%           - set-auto-mode--apply-alist
     79,923,463  68%            - set-auto-mode-0
     79,923,463  68%             - clojure-ts-clojurescript-mode
     45,382,937  38%              - clojure-ts-mode
     45,305,561  38%               + treesit-major-mode-setup
         25,386   0%               + treesit-ready-p
         20,512   0%               + clojure-ts-mode-variables
         17,202   0%                 clojure-ts--ensure-grammars
          8,288   0%               + clojure-ts--compute-semantic-indentation-rules-cache
     34,540,526  29%              + run-mode-hooks
 ...
```

The function `clojure-ts-clojurescript-mode` seems to add significant overhead as well, even though it appears to be a simple derived mode that doesn't even have any body forms. It didn't have any problem on Linux.

I'm rather at a loss as to what's going on. 
[publickey - alexispurslane@pm.me - 0x41E61568.asc (application/pgp-keys, attachment)]
[signature.asc (application/pgp-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78697; Package emacs. (Thu, 05 Jun 2025 09:10:04 GMT) Full text and rfc822 format available.

Message #8 received at 78697 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: alexis purslane <alexispurslane <at> pm.me>
Cc: 78697 <at> debbugs.gnu.org
Subject: Re: bug#78697: `treesit-major-mode-setup` causing substantial pause
 when finding a file
Date: Thu, 05 Jun 2025 12:09:00 +0300
> Date: Thu, 05 Jun 2025 01:27:58 +0000
> From:  alexis purslane via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
> 
> - Most time opening a file (38%) is spent within `treesit-major-mode-setup`:
> 
> ```
>     112,106,431  95% - command-execute
> ...
>      84,918,927  72%     - find-file
>      84,916,421  72%      - find-file-noselect
>      84,902,072  72%       - find-file-noselect-1
>      84,852,273  72%        - after-find-file
>      84,735,710  72%         - normal-mode
>      80,077,055  68%          - set-auto-mode
>      79,933,703  68%           - set-auto-mode--apply-alist
>      79,923,463  68%            - set-auto-mode-0
>      79,923,463  68%             - clojure-ts-clojurescript-mode
>      45,382,937  38%              - clojure-ts-mode
>      45,305,561  38%               + treesit-major-mode-setup
>          25,386   0%               + treesit-ready-p
>          20,512   0%               + clojure-ts-mode-variables
>          17,202   0%                 clojure-ts--ensure-grammars
>           8,288   0%               + clojure-ts--compute-semantic-indentation-rules-cache
>      34,540,526  29%              + run-mode-hooks
>  ...
> ```
> 
> The function `clojure-ts-clojurescript-mode` seems to add significant overhead as well, even though it appears to be a simple derived mode that doesn't even have any body forms. It didn't have any problem on Linux.

Please show the fully expanded profile, with all the levels marked
with "+" expanded.  As posted, the profile stops showing details at
the most important function: treesit-major-mode-setup, so it is hard
to come up with ideas what could be the problem.

Also, since clojure-ts-mode is not part of Emacs, did you report this
to the mode's developers?

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78697; Package emacs. (Thu, 05 Jun 2025 13:56:01 GMT) Full text and rfc822 format available.

Message #11 received at 78697 <at> debbugs.gnu.org (full text, mbox):

From: alexis purslane <alexispurslane <at> pm.me>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 78697 <at> debbugs.gnu.org
Subject: Re: bug#78697: `treesit-major-mode-setup` causing substantial pause
 when finding a file
Date: Thu, 05 Jun 2025 13:55:05 +0000
[Message part 1 (text/plain, inline)]
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

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.

On Thursday, June 5th, 2025 at 9:09 AM, Eli Zaretskii <eliz <at> gnu.org> wrote:

> > Date: Thu, 05 Jun 2025 01:27:58 +0000
> > From: alexis purslane via "Bug reports for GNU Emacs,
> > the Swiss army knife of text editors" bug-gnu-emacs <at> gnu.org
> > 

> > - Most time opening a file (38%) is spent within `treesit-major-mode-setup`:
> > 

> > `112,106,431 95% - command-execute ... 84,918,927 72% - find-file 84,916,421 72% - find-file-noselect 84,902,072 72% - find-file-noselect-1 84,852,273 72% - after-find-file 84,735,710 72% - normal-mode 80,077,055 68% - set-auto-mode 79,933,703 68% - set-auto-mode--apply-alist 79,923,463 68% - set-auto-mode-0 79,923,463 68% - clojure-ts-clojurescript-mode 45,382,937 38% - clojure-ts-mode 45,305,561 38% + treesit-major-mode-setup 25,386 0% + treesit-ready-p 20,512 0% + clojure-ts-mode-variables 17,202 0% clojure-ts--ensure-grammars 8,288 0% + clojure-ts--compute-semantic-indentation-rules-cache 34,540,526 29% + run-mode-hooks ...`
> > 

> > The function `clojure-ts-clojurescript-mode` seems to add significant overhead as well, even though it appears to be a simple derived mode that doesn't even have any body forms. It didn't have any problem on Linux.
> 

> 

> Please show the fully expanded profile, with all the levels marked
> with "+" expanded. As posted, the profile stops showing details at
> the most important function: treesit-major-mode-setup, so it is hard
> to come up with ideas what could be the problem.
> 

> Also, since clojure-ts-mode is not part of Emacs, did you report this
> to the mode's developers?
> 

> Thanks.
[publickey - alexispurslane@pm.me - 0x41E61568.asc (application/pgp-keys, attachment)]
[signature.asc (application/pgp-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78697; Package emacs. (Thu, 05 Jun 2025 14:04:02 GMT) Full text and rfc822 format available.

Message #14 received at 78697 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: alexis purslane <alexispurslane <at> pm.me>
Cc: 78697 <at> debbugs.gnu.org
Subject: Re: bug#78697: `treesit-major-mode-setup` causing substantial pause
 when finding a file
Date: Thu, 05 Jun 2025 17:03:10 +0300
> Date: Thu, 05 Jun 2025 13:55:05 +0000
> From: alexis purslane <alexispurslane <at> pm.me>
> Cc: 78697 <at> debbugs.gnu.org
> 
> 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.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78697; Package emacs. (Thu, 05 Jun 2025 14:06:02 GMT) Full text and rfc822 format available.

Message #17 received at 78697 <at> debbugs.gnu.org (full text, mbox):

From: alexis purslane <alexispurslane <at> pm.me>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 78697 <at> debbugs.gnu.org
Subject: Re: bug#78697: `treesit-major-mode-setup` causing substantial pause
 when finding a file
Date: Thu, 05 Jun 2025 14:04:56 +0000
[Message part 1 (text/plain, inline)]
Yeah, looking at the new profiles made me realize that too, so I submitted it before responding. In case you want to follow the issue: https://github.com/clojure-emacs/clojure-ts-mode/issues/109

On Thursday, June 5th, 2025 at 2:03 PM, Eli Zaretskii <eliz <at> gnu.org> wrote:

> > Date: Thu, 05 Jun 2025 13:55:05 +0000
> > From: alexis purslane alexispurslane <at> pm.me
> > Cc: 78697 <at> debbugs.gnu.org
> > 

> > 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.
[publickey - alexispurslane@pm.me - 0x41E61568.asc (application/pgp-keys, attachment)]
[signature.asc (application/pgp-signature, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78697; Package emacs. (Thu, 05 Jun 2025 15:03:06 GMT) Full text and rfc822 format available.

Message #20 received at 78697 <at> debbugs.gnu.org (full text, mbox):

From: alexis purslane <alexispurslane <at> pm.me>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 78697 <at> debbugs.gnu.org
Subject: Re: bug#78697: `treesit-major-mode-setup` causing substantial pause
 when finding a file
Date: Thu, 05 Jun 2025 15:01:48 +0000
[Message part 1 (text/plain, inline)]
Oh, I did want to mention that while on Linux I didn't notice much of an improvement using MPS, on Mac OS, going from regular Emacs to the feature/igc branch eliminated a huge amount of very long and constant random pauses and made it much, much faster. And I haven't run into any bugs or issues with it still.

On Thursday, June 5th, 2025 at 2:04 PM, alexis purslane <alexispurslane <at> pm.me> wrote:

> Yeah, looking at the new profiles made me realize that too, so I submitted it before responding. In case you want to follow the issue: https://github.com/clojure-emacs/clojure-ts-mode/issues/109
> 

> On Thursday, June 5th, 2025 at 2:03 PM, Eli Zaretskii eliz <at> gnu.org wrote:
> 

> > > Date: Thu, 05 Jun 2025 13:55:05 +0000
> > > From: alexis purslane alexispurslane <at> pm.me
> > > Cc: 78697 <at> debbugs.gnu.org
> > > 

> > > 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.
[publickey - alexispurslane@pm.me - 0x41E61568.asc (application/pgp-keys, attachment)]
[signature.asc (application/pgp-signature, attachment)]

This bug report was last modified 1 day ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.