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

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

Open
quanticle opened this issue Feb 28, 2022 · 6 comments
Open

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

quanticle opened this issue Feb 28, 2022 · 6 comments

Comments

@quanticle
Copy link

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
Copy link

c0001 commented Apr 15, 2023

Either here! Markup.

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

@jman-schief
Copy link

jman-schief commented Apr 25, 2023

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
Copy link

Knusper commented Jul 10, 2023

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
Copy link

c0001 commented Oct 20, 2023

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
Copy link

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
Copy link

dgutov commented Mar 22, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants