1

Emacs on my Mac got upgraded when I upgraded some other package in Homebrew and now working with Markdown files is very slow. I'm now running the railwaycat/emacsmacport/emacs-mac package version with Emacs version 27.1. My markdown-mode is 20201211.329. Disabling markdown-mode makes the problem go away. I have run the profiler (in cpu mode) and gotten the following report:

+ redisplay_internal (C function)                                7782  75%
+ command-execute                                                1850  18%
+ ...                                                             443   4%
+ jit-lock--antiblink-post-command                                162   1%
+ timer-event-handler                                              21   0%
+ #<compiled 0x421b5d07>                                            9   0%
+ gui-set-selection                                                 4   0%
+ clear-transient-map                                               1   0%

Digging into redisplay_internal reveals the following:

- redisplay_internal (C function)                                7782  75%
 - jit-lock-function                                             7687  74%
  - jit-lock-fontify-now                                         7687  74%
   - jit-lock--run-functions                                     7681  74%
    - run-hook-wrapped                                           7681  74%
     - #<compiled 0x1feee74d43f1>                                7681  74%
      - font-lock-fontify-region                                 7671  74%
       - font-lock-default-fontify-region                        7671  74%
        - font-lock-fontify-keywords-region                      7663  74%
         - markdown-match-italic                                 7418  72%
          - markdown-match-italic                                7295  71%
           - markdown-match-italic                               7282  70%
            - markdown-match-italic                              7267  70%
             - markdown-match-italic                             7132  69%
              - markdown-match-italic                            6999  68%
               - markdown-match-italic                           6884  67%
                - markdown-match-italic                          6764  65%
                 - markdown-match-italic                         6656  64%
                  - markdown-match-italic                        6556  63%
                   - markdown-match-italic                       6454  62%
                    - markdown-match-italic                      6446  62%
                     - markdown-inline-code-at-pos-p               5758  56%
                      - markdown-inline-code-at-pos               5757  56%
                       - markdown-match-code                     5753  56%
                        - markdown-search-until-condition               5753  56%
                           apply                                 5752  55%
                         markdown-beginning-of-text-block                  3   0%
                         markdown-end-of-text-block                  1   0%

In short, Emacs appears to be spending looking for the following regular expression in order to identify inline code blocks:

"\\(?:\\`\\|[^\\]\\)\\(?1:\\(?2:`+\\)\\(?3:\\(?:.\\|\n[^\n]\\)*?[^`]\\)\\(?4:\\2\\)\\)\\(?:[^`]\\|\\'\\)"

I know much more than when I posted v1 of this question but I'm still not sure--how do I fix this?

2
  • Can we take a peek at what expanding the + sign reveals for the redisplay_internal section? Maybe it's helpful, maybe it's not .... – lawlist Dec 19 '20 at 4:51
  • Thanks @lawlist. I was missing expanding the + due to lack of familiarity with the profiler. – kuzzooroo Dec 19 '20 at 5:47
1

It is caused due to common * syntax of lists and italics. For deeply nested lists, parsing gets expensive due to recursive checks for italics. If you are willing to abandon *italics* syntax in favor of _italics_ then the computational overhead can be avoided.

(defconst markdown-regex-italic
    "\\(?:^\\|[^\\]\\)\\(?1:\\(?2:[_]\\)\\(?3:[^ \n\t\\]\\|[^ \n\t]\\(?:.\\|\n[^\n]\\)[^\\ ]\\)\\(?4:\\2\\)\\)")
;; and/or
(defconst markdown-regex-gfm-italic
    "\\(?:^\\|[^\\]\\)\\(?1:\\(?2:[_]\\)\\(?3:[^ \\]\\2\\|[^ ]\\(?:.\\|\n[^\n]\\)\\)\\(?4:\\2\\)\\)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.