jrblevin / markdown-mode

Emacs Markdown Mode
http://jblevins.org/projects/markdown-mode/
GNU General Public License v3.0
875 stars 160 forks source link

High CPU Usage When Editing Files With Lots of Links #691

Open quanticle opened 2 years ago

quanticle commented 2 years ago

I recently noticed that my emacs was lagging and having high CPU usage when editing markdown files with lots of links (Example file). I reloaded emacs with emacs -Q --load <path to markdown-mode.el> and I was able to isolate the problem to markdown-mode. I ran the profiler and I got the following trace:

- redisplay_internal (C function)                                 935  60%
 - jit-lock-function                                              934  60%
  - jit-lock-fontify-now                                          934  60%
   - jit-lock--run-functions                                      929  59%
    - run-hook-wrapped                                            929  59%
     - #<compiled 0x15736ac3b751>                                 929  59%
      - font-lock-fontify-region                                  929  59%
       - font-lock-default-fontify-region                         929  59%
        - font-lock-fontify-keywords-region                       901  58%
         - markdown-match-italic                                  413  26%
          - let*                                                  413  26%
           - if                                                   412  26%
            - progn                                               376  24%
             - let                                                376  24%
              - if                                                376  24%
               - or                                               372  23%
                - markdown-inline-code-at-pos-p                   367  23%
                 - let                                            366  23%
                  - unwind-protect                                366  23%
                   - progn                                        366  23%
                    - markdown-inline-code-at-pos                 366  23%
                     - save-excursion                             366  23%
                      - let                                       366  23%
                       - while                                    324  20%
                        - and                                     324  20%
                         - markdown-match-code                    324  20%
                          - if                                    324  20%
                           - markdown-search-until-condition                324  20%
                            - let                                 324  20%
                             - while                              324  20%
                              - and                               324  20%
                                 apply                            324  20%
                       + markdown-beginning-of-text-block                 35   2%
                       + progn                                      6   0%
                + markdown-range-property-any                       5   0%
               + progn                                              4   0%
            + and                                                  36   2%
         + markdown-fontify-inline-links                          145   9%
         + markdown-fontify-plain-uris                            112   7%
         + markdown-fontify-list-items                             52   3%
         + markdown-fontify-sub-superscripts                       36   2%
         + markdown-match-bold                                     28   1%
         + markdown-match-code                                     27   1%
         + markdown-match-inline-attributes                         5   0%
         + markdown-fontify-tables                                  3   0%
         + markdown-match-html-tag                                  3   0%
         + markdown-match-leanpub-sections                          2   0%
           #<compiled 0x1fcd58f4f709>                               2   0%
           markdown-match-wiki-link                                 1   0%
         + markdown-fontify-headings                                1   0%
         + markdown-match-gfm-open-code-blocks                      1   0%
         + markdown-fontify-gfm-code-blocks                         1   0%
         + markdown-fontify-blockquotes                             1   0%
         + markdown-match-pandoc-metadata                           1   0%
         + markdown-fontify-reference-links                         1   0%
         + markdown-fontify-hrs                                     1   0%
         + markdown-match-includes                                  1   0%
         + markdown-match-fenced-end-code-block                     1   0%
          font-lock-fontify-syntactically-region                   10   0%
        + font-lock-unfontify-region                                2   0%
   mode-line-default-help-echo                                      1   0%
+ ...                                                             521  33%
+ jit-lock--antiblink-post-command                                 56   3%
+ command-execute                                                  38   2%
+ timer-event-handler                                               2   0%
+ undo-auto--add-boundary                                           1   0%

I'm not quite sure what's going on. It seems like it's spending an inordinate amount of time in markdown-match-italic, which would explain the high CPU usage, but it's not clear to me why it's spending so much time in that function. Initially, I thought it was because I was using * to demarcate list items, which might have led to markdown-mode getting confused about unmatched italics markers, but shifting the list marker to - did not solve the problem.

I'm running emacs 27.1 on Ubuntu 21.10. I'm running markdown-mode-20220212.728, which is the latest that's on MELPA.

If I can provide any additional information to help debug, please let me know and I'll be glad to try additional steps.

c0001 commented 1 year ago

Either here! Markup.

Seems the traditional fontification method is laggy, looking for treesit variant now.

jman-schief commented 1 year ago

Hello, also experiencing extreme slow text edits in a markdown file with many links (file is just ~23K). Profiling Emacs (actually: Doom with Emacs 29.0.90) leads to the the same results as the issue reporter.

I think I've enabled tree-sitter on the markdown-mode but I'm unsure on how to check if it's enabled and - if yes - how to check and compare the results. Should and additional package be installed to interface the markdown-mode to the tree-sitter? How does that actually look like? cc @c0001 for possibly some insights :) thanks!

Knusper commented 12 months ago

Uff... and now I also get bitten by this. This and the bug where markdown mode becomes slow in moderately large tables render it currently completely unusable for my projects. I am switching to org-mode as my main markup language right now.

c0001 commented 8 months ago

Hello, also experiencing extreme slow text edits in a markdown file with many links (file is just ~23K). Profiling Emacs (actually: Doom with Emacs 29.0.90) leads to the the same results as the issue reporter.

I think I've enabled tree-sitter on the markdown-mode but I'm unsure on how to check if it's enabled and - if yes - how to check and compare the results. Should and additional package be installed to interface the markdown-mode to the tree-sitter? How does that actually look like? cc @c0001 for possibly some insights :) thanks!

Sorry for almost half a year lately to reply since I've not got any notification about this cc until I review my inbox.

BTW, I've not found any markdown treesit relavance for emacs til now, so just a waiting again ...

jman-schief commented 8 months ago

After a deep dive into this problem I came to the conclusion that the culprit is the underlying font-lock-mode. I'm not sure what markdown-mode can do about this. Setting font-lock-mode to nil makes the issue disappear, along with the syntax highlighting (unsurprisingly).

Happy to receive feedback though. I'd like to gain a better context about the issue to open an ticket/discussion on the emacs-devel mailing list.

dgutov commented 3 months ago

Why would it be font-lock's problem if the profiler says most of the time is spent inside markdown-match-italic and markdown-inline-code-at-pos-p?

        8362  64%          - markdown-match-italic
        4495  34%           + markdown-inline-code-at-pos-p
        3747  28%           + markdown-match-italic

Same here, BTW, when editing a Markdown file with lots of links in one place (table of contents).