jrblevin / markdown-mode

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

Markdown mode leads to heavy input lag #799

Open RalfJung opened 1 year ago

RalfJung commented 1 year ago

Since a recent emacs update, markdown mode leads to heavy input lag when editing inside a list. This makes it basically unusable, I've uninstalled it for now.

Expected Behavior

Everything I type should appear on the screen immediately.

Actual Behavior

There is noticeable input lag, making editing quite painful.

Steps to Reproduce

Software Versions

tenpast commented 1 year ago

I can confirm this issue. Running emacs profiler I get the following results:

     42,604,780  81% + redisplay_internal (C function)
      8,102,036  15% + command-execute
      1,833,645   3% + jit-lock--antiblink-post-command
         43,728   0% + timer-event-handler
          4,144   0%   internal-echo-keystrokes-prefix
             24   0% + eldoc-schedule-timer
              0   0%   ...
       24573  94% + redisplay_internal (C function)
        1122   4% + command-execute
         115   0% + ...
          37   0% + jit-lock--antiblink-post-command
          22   0% + timer-event-handler
jman-schief commented 1 year ago

I suspect you're running into #691

Can you try expanding the profile reports and see if you end up isolating the font-lock-* functions as culprit?

tenpast commented 1 year ago

I suspect you're running into #691

Can you try expanding the profile reports and see if you end up isolating the font-lock-* functions as culprit?

I uninstalled markdown-mode after I posted the comment above as it was getting in the way of whatever it was that I was in the middle of. I've reinstalled it now and tried to look at different Markdown files on my system, but was unable to find the culprit.

I will keep Markdown-mode installed and run the report if I come across the issue again.

Thanks for your response.

I should have thought of expanding the profile reports at the time, my apologies.

tenpast commented 1 year ago

I've downloaded the file suggested by the OP. There is some lag when first loading the file if repeatedly using scroll-up-command. Once the bottom of the file is reached there is no more lag navigating anywhere in the document. I assume this is to be expected as the file is being fontified. Running profiler points to jit-lock-function as the cuplrit, and particularly markdown-fontify-* functions.

There was no lag when going somewhere inside the first list of changes and typing.

Based on the profiler report it doesn't seem I was able to replicate whatever happened to me on October 6th.

umanwizard commented 10 months ago

I can reproduce in any buffer with a lot of _ characters enclosed in backticks. For example, generate a buffer as follows:

(dotimes (n 1000)
  (insert "`my_test_string`\n"))

then observe that running (font-lock-debug-fontify) takes a long time (12 seconds for me, and also appears to be quadratic in the number of lines).

The heavy time-takers seem to be markdown-match-italic and, in particular, its call to markdown-inline-code-at-pos-p.

umanwizard commented 10 months ago

For some reason I can't explain, the loss-of-interactivity issue can be worked around by turning on jit-lock-debug-mode in the buffer.

jdtsmith commented 5 months ago

Same issue here: latest markdown-mode crawls to a near halt on a fast machine when editing inside of lists (e.g. this file). And I can confirm, turning on jit-lock-debug-mode solves it entirely. Without it, if I hold down a key (with my fast repeat), I get <1 cps. With it, >50cps.

I profiled. Much of the time is spent in GC, but markdown-match-bold also seems to be a hot-spot:

        5647  65% - ...
        5647  65%    Automatic GC
        2459  28% - redisplay_internal (C function)
        2402  27%  - jit-lock-function
        2398  27%   - jit-lock-fontify-now
        2332  26%    - jit-lock--run-functions
        2332  26%     - run-hook-wrapped
        2330  26%      - #<compiled -0x1576faa4ebbd38c3>
        2313  26%       - font-lock-fontify-region
        2309  26%        - font-lock-default-fontify-region
        2223  25%         - font-lock-fontify-keywords-region
        1180  13%          + markdown-match-bold
         220   2%          + markdown-match-italic
         133   1%          + markdown-fontify-plain-uris
          94   1%          + markdown-match-code
          79   0%          + markdown-fontify-inline-links
          61   0%          + markdown-fontify-sub-superscripts
          50   0%          + markdown-match-html-tag
          43   0%          + #<compiled 0x1caac574dd64597e>
          28   0%          + markdown-fontify-list-items
          26   0%            markdown-match-declarative-metadata
          23   0%            markdown-match-pandoc-metadata
           9   0%            markdown-match-inline-attributes
           7   0%          + markdown-fontify-angle-uris
           7   0%          + markdown-fontify-blockquotes
           6   0%            markdown-match-yaml-metadata-begin
           5   0%            markdown-match-gfm-close-code-blocks
           5   0%            markdown-match-includes
           5   0%          + markdown-fontify-gfm-code-blocks
           4   0%            markdown-match-escape
           4   0%          + markdown-fontify-reference-links
           4   0%            markdown-match-yaml-metadata-key
           3   0%            markdown-fontify-headings
           3   0%          + markdown-fontify-hrs
           3   0%            markdown-match-fenced-start-code-block
           3   0%            markdown-match-yaml-metadata-end
           3   0%            markdown-match-fenced-end-code-block
           2   0%            markdown-match-gfm-open-code-blocks
           2   0%          + eval
           2   0%            markdown-match-pre-blocks
           1   0%          + markdown-fontify-fenced-code-blocks
           1   0%            #<compiled 0xda56985e5828f>
          27   0%         + font-lock-unfontify-region
          19   0%         + font-lock-fontify-syntactically-region
           4   0%         + font-lock-extend-region-wholelines
           2   0%           font-lock-extend-region-multiline
          58   0%    + run-with-timer
           1   0%      #<compiled 0x1982939eea8f>
          56   0%  + eval
         470   5% + command-execute
          73   0% + timer-event-handler
           3   0% + jit-lock--antiblink-post-command
           1   0%   help-command-error-confusable-suggestions
ackerleytng commented 5 months ago

I can also confirm, turning on jit-lock-debug-mode solves it. Here's my profiler report before turning off jit-lock-debug-mode:

     Samples    %   Function                                                                                                   
        1359  65% - redisplay_internal (C function)                                                                            
        1359  65%  - jit-lock-function                                                                                         
        1359  65%   - jit-lock-fontify-now                                                                                     
        1359  65%    - jit-lock--run-functions                                                                                 
        1359  65%     - #<compiled -0x156ee42e70414b83>                                                                        
        1359  65%      - font-lock-fontify-region                                                                              
        1355  65%       - font-lock-default-fontify-region                                                                     
        1323  64%        - font-lock-fontify-keywords-region                                                                   
        1111  53%         - markdown-match-italic                                                                              
        1099  53%          - markdown-match-italic                                                                             
        1091  52%           - markdown-match-italic                                                                            
        1091  52%            - markdown-match-italic                                                                           
        1083  52%             - markdown-match-italic                                                                          
        1083  52%              - markdown-match-italic                                                                         
        1083  52%               - markdown-match-italic                                                                        
        1071  51%                - markdown-match-italic                                                                       
         860  41%                 - markdown-match-italic                                                                      
         844  40%                  - markdown-match-italic                                                                     
         840  40%                   - markdown-match-italic                                                                    
         836  40%                    - markdown-match-italic                                                                   
         836  40%                     - markdown-match-italic                                                                  
         516  25%                      - markdown-inline-code-at-pos-p                                                         
         516  25%                       - markdown-inline-code-at-pos                                                          
         512  24%                        - markdown-match-code                                                                 
         504  24%                         - markdown-search-until-condition                                                    
         124   6%                          - #<compiled 0x1f00e4862f9e715a>                                                    
          72   3%                           + markdown-code-block-at-pos                                                       
         320  15%                      + markdown-match-italic                                                                 
           4   0%                   + markdown-inline-code-at-pos-p                                                            
          16   0%                  + markdown-inline-code-at-pos-p                                                             
         211  10%                 + markdown-inline-code-at-pos-p                                                              
           4   0%                + markdown-inline-code-at-pos-p                                                               
           8   0%             + markdown-inline-code-at-pos-p                                                                  
           8   0%           + markdown-inline-code-at-pos-p                                                                    
           8   0%          + markdown-inline-code-at-pos-p                                                                     
           4   0%            markdown-match-inline-generic                                                                     
          40   1%         + markdown-fontify-list-items                                                                        
          28   1%           markdown-match-declarative-metadata                                                                
          24   1%           markdown-match-pandoc-metadata                                                                     
          24   1%         + markdown-match-math-double                                                                         
          20   0%         + markdown-match-code                                                                                
          20   0%         + markdown-fontify-sub-superscripts                                                                  
          20   0%         + markdown-fontify-plain-uris                                                                        
          12   0%           markdown-match-bold                                                                                
           8   0%           font-lock-prepend-text-property                                                                    
           8   0%         + markdown-match-math-single                                                                         
           4   0%         + markdown-fontify-angle-uris                                                                        
           4   0%           markdown-match-inline-attributes                                                                   
           4   0%          font-lock-fontify-syntactically-region 
ackerleytng commented 5 months ago

It seems like jit-lock-debug-mode isn't actually a workaround. It effectively turns off highlighting, which improves performance at the cost of functionality.

umanwizard commented 5 months ago

True, I hadn't realized that.

Oh well, it is still useful to know for when I need to edit markdown files.

ackerleytng commented 4 months ago

I have to get to something else so I can't bisect to find the right setting, but one of these four defcustoms fixed it for me:

(use-package markdown-mode
  :ensure t
  :mode ("README\\.md\\'" . gfm-mode)
  :custom
  (markdown-italic-underscore nil)
  (markdown-enable-html nil)
  (markdown-enable-math nil)
  (markdown-gfm-use-electric-backquote nil))