Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart: nvim_treesitter#indent() slow since latest dart updates #4945

Open
fpbouchard opened this issue Jun 12, 2023 · 25 comments
Open

Dart: nvim_treesitter#indent() slow since latest dart updates #4945

fpbouchard opened this issue Jun 12, 2023 · 25 comments
Labels
bug Something isn't working indent Issues or PRs about indentation queries or module upstream parser Solving this issue requires changes in the parser

Comments

@fpbouchard
Copy link

fpbouchard commented Jun 12, 2023

Describe the bug

Triggering indent (opening a file, pressing o or cc, etc) takes more than one second on any dart file.

To Reproduce

  1. Create an empty dart file
  2. Restart nvim
  3. Open the empty dart file
  4. Observe long opening time
  5. Enter insert mode with a key that triggers indent (o or cc)
  6. Observe delay before insert mode triggers

Expected behavior

File should open quickly, and insert mode should trigger instantly.

Output of :checkhealth nvim-treesitter

==============================================================================
nvim-treesitter: require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.20.8 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v18.12.1 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: Apple clang version 14.0.0 (clang-1400.0.29.202)
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "arm64",
  release = "21.6.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 21.6.0: Thu Mar  9 20:12:21 PST 2023; root:xnu-8020.240.18.700.8~1/RELEASE_ARM64_T6000"
} ~

Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - cpp                 ✓ ✓ ✓ ✓ ✓
  - css                 ✓ . ✓ ✓ ✓
  - dart                ✓ ✓ ✓ ✓ ✓
  - diff                ✓ . . . .
  - git_rebase          ✓ . . . ✓
  - gitattributes       ✓ . . . ✓
  - gitcommit           ✓ . . . ✓
  - gitignore           ✓ . . . .
  - go                  ✓ ✓ ✓ ✓ ✓
  - graphql             ✓ . . ✓ ✓
  - html                ✓ ✓ ✓ ✓ ✓
  - ini                 ✓ . ✓ . .
  - java                ✓ ✓ ✓ ✓ ✓
  - javascript          ✓ ✓ ✓ ✓ ✓
  - json                ✓ ✓ ✓ ✓ .
  - json5               ✓ . . . ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ ✓ ✓
  - prisma              ✓ . . . .
  - python              ✓ ✓ ✓ ✓ ✓
  - ruby                ✓ ✓ ✓ ✓ ✓
  - rust                ✓ ✓ ✓ ✓ ✓
  - sql                 ✓ . . ✓ ✓
  - terraform           ✓ . ✓ ✓ ✓
  - toml                ✓ ✓ ✓ ✓ ✓
  - tsx                 ✓ ✓ ✓ ✓ ✓
  - typescript          ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

VIM v0.10.0-dev-513+g758c418d1-Homebrew
Build type: Release
LuaJIT 2.1.0-beta3

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/HEAD-758c418/share/nvim"

Additional context

Profile info:

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    2   1.375896             nvim_treesitter#indent()
   74   0.012148             nvim_treesitter#foldexpr()
   14   0.009982             <SNR>19_try()
    2   0.004595             <SNR>1_LoadFTPlugin()
   18   0.002382   0.002187  <SNR>36_Highlight_Matching_Pair()
    2   0.001635             <SNR>52_SynSet()
    2   0.000672             <SNR>2_LoadIndent()
   18   0.000195             <SNR>36_Remove_Matches()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
    2              1.375896  nvim_treesitter#indent()
   74              0.012148  nvim_treesitter#foldexpr()
   14              0.009982  <SNR>19_try()
    2              0.004595  <SNR>1_LoadFTPlugin()
   18   0.002382   0.002187  <SNR>36_Highlight_Matching_Pair()
    2              0.001635  <SNR>52_SynSet()
    2              0.000672  <SNR>2_LoadIndent()
   18              0.000195  <SNR>36_Remove_Matches()
@fpbouchard fpbouchard added the bug Something isn't working label Jun 12, 2023
@alepop
Copy link

alepop commented Jun 12, 2023

Same. Also have an a delay while trying to navigate a file with h,j,k,l.

@lucario387
Copy link
Member

Time to rewrite that indent, or just remove it entirely ig ...

@lucario387 lucario387 self-assigned this Jun 12, 2023
@lucario387
Copy link
Member

lucario387 commented Jun 12, 2023

Can I get the file that shows the issue?

Edit: Apparently not, tests file inside nvim-treesitter suffices.

@lucario387
Copy link
Member

Initial opening of the file is also darn slow, which should not be related to indent ...

@clason
Copy link
Contributor

clason commented Jun 12, 2023

UserNobody14/tree-sitter-dart#46

@lucario387 lucario387 removed their assignment Jun 12, 2023
@lucario387 lucario387 added the upstream parser Solving this issue requires changes in the parser label Jun 12, 2023
@lucario387
Copy link
Member

There is definitely some issues with the indent module here ... 😮‍💨

@lucario387 lucario387 added the indent Issues or PRs about indentation queries or module label Jun 12, 2023
@mrmeowski
Copy link

I think commit 46ddea9 is the culprit. I do not observe the slowness before this commit. This comment demonstrates another way of reproducing the issue.

@CTZNpk

This comment was marked as duplicate.

@theHamsta
Copy link
Member

maybe relevant UserNobody14/tree-sitter-dart#47

@clason
Copy link
Contributor

clason commented Jun 14, 2023

That PR has been merged; please try the new version (update nvim-treesitter, then :TSUpdate).

@lucario387
Copy link
Member

lucario387 commented Jun 14, 2023

It's better but its speed is still worse than the version a few updates ago.

Testing with a 5 line dart file:

void main() {
  someMethod(() {
  }, (Exception ex, StackTrace stackTrace) {
  });
}
With (if_statement (block) Without it
image image

@lucario387

This comment was marked as outdated.

@lucario387
Copy link
Member

lucario387 commented Jun 14, 2023

The time to parse the tree is absurdly long, I don't see how parsing a 5 line file took 5.5 second

In nanoseconds:
Start at 0                                // At nvim_treesitter#indent() start                                                                                                                                                      
Finished find root at: 750587                                                                                                                                                                   
Finished parsing at:  5569491639          // Right after `get_indents(...)` in indent.lua`                                                                                                                                                      
Finished find start node at: 5569545360   // Right before `while node do` in indent.lua

@fpbouchard
Copy link
Author

fpbouchard commented Jun 15, 2023

I concur, the last update is faster, but still quite slower than a few updates ago.

By disabling vim.o.foldexpr = "nvim_treesitter#foldexpr()" on this version, opening a file is almost as fast as previous verisons (it seems that it does not trigger an indent now):

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    2   0.007609   0.005980  <SNR>1_LoadFTPlugin()
    6   0.002443             <SNR>21_try()
    1   0.001169   0.000003  <SNR>30_AutoInit()
    1   0.001166   0.000038  <SNR>30_Init()
    1   0.000954   0.000071  <SNR>30_DetectDeclared()
    1   0.000930             <SNR>12_lastplace()
    2   0.000719             <SNR>2_LoadIndent()
    2   0.000690             <SNR>52_SynSet()
    1   0.000611   0.000122  <SNR>30_DetectEditorConfig()
    1   0.000489   0.000423  <SNR>30_ReadEditorConfig()
    3   0.000286   0.000273  <SNR>37_Highlight_Matching_Pair()
    1   0.000210   0.000139  <SNR>30_ModelineOptions()
    1   0.000143             <SNR>30_Apply()
   10   0.000071             <SNR>30_ParseOptions()
    2   0.000067   0.000060  <SNR>30_FnmatchTranslate()
    1   0.000059             <SNR>30_EditorConfigToOptions()
    1   0.000031   0.000021  <SNR>30_DetectHeuristics()
    1   0.000030             <SNR>38_LocalBrowse()
    3   0.000013             <SNR>37_Remove_Matches()
    1   0.000007             <SNR>30_UserOptions()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
    2   0.007609   0.005980  <SNR>1_LoadFTPlugin()
    6              0.002443  <SNR>21_try()
    1              0.000930  <SNR>12_lastplace()
    2              0.000719  <SNR>2_LoadIndent()
    2              0.000690  <SNR>52_SynSet()
    1   0.000489   0.000423  <SNR>30_ReadEditorConfig()
    3   0.000286   0.000273  <SNR>37_Highlight_Matching_Pair()
    1              0.000143  <SNR>30_Apply()
    1   0.000210   0.000139  <SNR>30_ModelineOptions()
    1   0.000611   0.000122  <SNR>30_DetectEditorConfig()
   10              0.000071  <SNR>30_ParseOptions()
    1   0.000954   0.000071  <SNR>30_DetectDeclared()
    2   0.000067   0.000060  <SNR>30_FnmatchTranslate()
    1              0.000059  <SNR>30_EditorConfigToOptions()
    1   0.001166   0.000038  <SNR>30_Init()
    1              0.000030  <SNR>38_LocalBrowse()
    1   0.000031   0.000021  <SNR>30_DetectHeuristics()
    3              0.000013  <SNR>37_Remove_Matches()
    1              0.000007  <SNR>30_UserOptions()
    3              0.000007  <SNR>30_FnmatchReplace()

Entering insert mode with a key that triggers indent is almost as slow though:

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    1   1.314231             nvim_treesitter#indent()
    2   0.004706             <SNR>1_LoadFTPlugin()
    2   0.001642             <SNR>52_SynSet()
    7   0.001210             <SNR>5_try()
    2   0.000707             <SNR>2_LoadIndent()
    7   0.000326   0.000289  <SNR>37_Highlight_Matching_Pair()
    7   0.000037             <SNR>37_Remove_Matches()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
    1              1.314231  nvim_treesitter#indent()
    2              0.004706  <SNR>1_LoadFTPlugin()
    2              0.001642  <SNR>52_SynSet()
    7              0.001210  <SNR>5_try()
    2              0.000707  <SNR>2_LoadIndent()
    7   0.000326   0.000289  <SNR>37_Highlight_Matching_Pair()
    7              0.000037  <SNR>37_Remove_Matches()

You probably know by now but the commit that introduced this problem is 46ddea9 , so for folks that want to avoid the issue until it is fully resolved, you can lock your version at 33eb472.

alondahari added a commit to alondahari/dotnvim that referenced this issue Jun 18, 2023
@searleser97

This comment was marked as off-topic.

@lucario387
Copy link
Member

The maintainer of tree-sitter-dart is currently MIA, so the ETA is unknown
We can revert that 46ddea9 commit, cc-ing @amaanq to re-disable dart update :D

@clason
Copy link
Contributor

clason commented Jul 19, 2023

Before we do that, we need to check what that update did. If it fixed a crash or broken highlighting, this is more important than a performance regression for indents (which I remind are still experimental).

@searleser97

This comment was marked as off-topic.

@clason

This comment was marked as off-topic.

@ErikReider
Copy link

this is more important than a performance regression for indents

Not the only issue, opening new files is also extremely slow (1-2 seconds)

@aap01

This comment was marked as off-topic.

@robinsrk

This comment was marked as off-topic.

@okmanideep
Copy link

Just in case someone is doing a git bisect the following commits are good

  "nvim-treesitter": { "branch": "master", "commit": "f2778bd1a28b74adf5b1aa51aa57da85adfa3d16" },
  "nvim-treesitter-textobjects": { "branch": "master", "commit": "35a60f093fa15a303874975f963428a5cd24e4a0" },

Thanks to Lazy.nvim 's lock file

@lucario387
Copy link
Member

lucario387 commented Aug 25, 2023

It's so fun to try to debug this issue, even when removing dart/indents.scm, have the indentexpr to not be nvim-treesitter#indent(), and the parser is still snail slow

For solutions, one other way to deal with it is to use revisions, such as

local parser = require("nvim-treesitter.parsers").get_parser_configs()
parser.dart = {
  install_info = {
    url = "https://github.com/UserNobody14/tree-sitter-dart",
    files = { "src/parser.c", "src/scanner.c" },
    revision = "8aa8ab977647da2d4dcfb8c4726341bee26fbce4", -- The last commit before the snail speed
  },
}

This makes it uses the last commit that the parser is still okay

For queries, of course you'd need to use an older version of it. I recommend using the queries from before #4924, and put all of {highlights,indents,injections,fold,locals, textobjects}.scm into ~/.config/nvim/queries/dart. As it's most likely going to be the first in your rtp, those queries will be priotized over nvim-treesitter's, allowing for you to both use the latest tree-sitter parsers for other languages, and to have better experience with dart.

bluz71 added a commit to bluz71/dotfiles that referenced this issue Aug 30, 2023
Dart files have awful load performance due to this bug:
- nvim-treesitter/nvim-treesitter#4945

Until that issue is resolved use the last known good version of
nvim-treesitter.

Remove 'nvim-treesitter-textobjects' plugin; I basically never used
anything this plugin provided. It "may" be a source of performance
problems (may), so just get rid of it until I actually need it (which
may be never).
n0ks added a commit to n0ks/dotfiles that referenced this issue Sep 1, 2023
NiamhFerns pushed a commit to Nekovim/Nekovim that referenced this issue Sep 16, 2023
The issue fixed earlier was related to a treesitter issue. Currently locking treesitter commit 33eb472 till nvim-treesitter/nvim-treesitter#4945 is fixed.
@hinell
Copy link

hinell commented Oct 25, 2023

This is not just per dart. I'm having performance issues with nvim_treesitter#foldexpr() as well. foldexpr is called like crazy, slowing down everything.

  • NVIM v0.10.0-dev-ac1c23442f
  • nvim-treesitter: 9c4fc86
FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    6   0.040504   0.038771  <SNR>1_LoadFTPlugin()
  820   0.035560             nvim_treesitter#foldexpr()
   17   0.035431   0.035181  <SNR>62_SynSet()
    6   0.012263             <SNR>2_LoadIndent()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
    6   0.040504   0.038771  <SNR>1_LoadFTPlugin()
  820              0.035560  nvim_treesitter#foldexpr()
   17   0.035431   0.035181  <SNR>62_SynSet()
    6              0.012263  <SNR>2_LoadIndent()

koka831 added a commit to koka831/nvim that referenced this issue Nov 7, 2023
bardusco added a commit to bardusco/kickstart.nvim that referenced this issue Jan 29, 2024
…o delays

This commit updates the treesitter setup in `treesitter-setup.lua` to address
performance issues when editing Dart files. A significant delay was observed
when creating new lines in Dart files, as reported in multiple issues [1][2][3].
To mitigate this, the indent and textobjects configurations for Dart have been
disabled. This should prevent the editor from hanging and improve the overall
user experience when working with Dart code.

The commit also includes a small change where 'rust' was removed from the list
of languages in `ensure_installed` to match the project's current requirements.
The line was refactored for consistency.

References:
- [1] UserNobody14/tree-sitter-dart#48
- [2] UserNobody14/tree-sitter-dart#46
- [3] nvim-treesitter/nvim-treesitter#4945
divan added a commit to divan/astrocommunity that referenced this issue Feb 10, 2024
Currently there is an issue with treesitter for Dart, which makes it extremely slow on a basic editing tasks (like inserting new line takes 1 second on my M3 Pro).

Issue is tracked here: nvim-treesitter/nvim-treesitter#4945

One of the proposed solutions in the comments that worked for me is to use latest working version of treesitter:
nvim-treesitter/nvim-treesitter#4945 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working indent Issues or PRs about indentation queries or module upstream parser Solving this issue requires changes in the parser
Projects
None yet
Development

No branches or pull requests