renzmann / treesit-auto

Automatic installation, usage, and fallback for tree-sitter major modes in Emacs 29
GNU General Public License v3.0
353 stars 27 forks source link

Global mode noticeably slows opening files #84

Open noctuid opened 5 months ago

noctuid commented 5 months ago

Opening a python file takes 0.4s more than normal (according to (benchmark-run 1 (find-file...) with the global mode enabled. This is for every file open (grammar already installed).

treesit-auto--build-major-mode-remap-alist is expensive, 0.15-0.25 seconds on my machine. I had an older version (603de37) on another machine, which didn't add overhead to opening a file. I don't fully understand the comments in the function or the method is treesit-auto is using, so I'm not sure what the best way to improve this is. Regarding the first comment, it makes sense that a toggleable mode shouldn't alter major-mode-remap-alist globally, but is there any reason that I wouldn't want to just add to major-mode-remap-alist once globally other than that I would have to myself remove the remappings if I decided I wanted to stop using a *-ts-mode without changing treesit-auto-langs and restarting Emacs? If there isn't some other downside I am missing, can you provide treesit-auto--build-major-mode-remap-alist as a non-internal function and mention it in the readme? It would be nice if it was faster, but running it only once instead for every file load would be an improvement.

Given the current method, is there a reason you couldn't only consider the mode passed to set-auto-mode-0 instead of building the remap alist for all modes every time? As for caching, wouldn't it be relatively safe to cache the remapping for a mode that already met treesit-auto--ready-p? I mean technically you could uninstall a grammar, but I don't see that being a real issue. treesit-auto-langs could have a defcustom setter to handle the case where a cached mode is removed. If either of those are concerns, caching could be opt-in.

Edit: I think treesit-auto--ready-p is probably the slow part, so you could just cache that when it finally returns t and wouldn't need to worry about changes to treesit-auto-langs.

I think it would be nice if the global mode used caching and only did the minimum amount of calculation, so I could keep using it as-is without it slowing things down. And if not, it would be nice if there was an option to not advise set-auto-mode-0.

noctuid commented 4 months ago

Played with this more, and got the time down from 0.15 to 0.001 seconds. Don't think caching is necessary then. The build function is much slower on one of my computers (maybe because treesit-auto hasn't been native compiled yet). In any case, it looks like setq-local is not necessary. I'll make a PR.

dlawisch commented 1 month ago

Experienced huge slowdowns in org files with citar references. Profiler pointed to treesit-auto--ready-p which led me here.

Tested the proposed branch with straight + use-package:

  (use-package treesit-auto
    :straight (treesit-auto
               :type git
               :host github
               :repo "renzmann/treesit-auto"
               :fork ( :host github
                       :repo "noctuid/treesit-auto"
                       :branch "bind-around-set-auto-mode-0")))

Much better, no issues.