Emacs gets extremely slow when one code block in an org-mode file reaches some size — say > 2000 lines (Emacs 26.1 & org-mode 9.1.9).
Pointed out in this thread of 2015, the problem comes from syntax highlighting, activated by font-lock-mode.
Here's the profiling report
M-x profiler-start
Type a character in the code block (inside, before or after)
M-x profiler-report
- redisplay_internal (C function) 39467 84%
- jit-lock-function 39454 84%
- jit-lock-fontify-now 39454 84%
- jit-lock--run-functions 39418 84%
- run-hook-wrapped 39418 84%
- #<compiled 0x11fd819> 39418 84%
- font-lock-fontify-region 39418 84%
- font-lock-default-fontify-region 39418 84%
- font-lock-fontify-keywords-region 39334 84%
- org-fontify-meta-lines-and-blocks 38967 83%
- org-fontify-meta-lines-and-blocks-1 38963 83%
- org-src-font-lock-fontify-block 38911 83%
- org-font-lock-ensure 37567 80%
- #<compiled 0x52bfbd> 37567 80%
- font-lock-default-fontify-buffer 37567 80%
- font-lock-fontify-region 37559 80%
- c-font-lock-fontify-region 37559 80%
- font-lock-default-fontify-region 37559 80%
- font-lock-fontify-keywords-region 37435 80%
- c-font-lock-declarations 35256 75%
+ c-find-decl-spots 34937 75%
From the same thread, quoting Adam M. :
According to the Emacs documentation: 'In Emacs 21 and later, turning on font-lock-mode automatically activates the new Just-In-Time fontification provided by jit-lock-mode.' It would appear, therefore, that just-in-time fontification is not taking place in the code block with the whole code block being fontified at once - and hence the delay. Any help to confirm this hypothesis and how to enable just-in-time fontification in the code block would be appreciated.
Does anybody know how to fix this ?
font-lock-fontify-region
callsc-font-lock-fontify-region
.