emacs jit-lock-function 消耗大量CPU导致卡住,求解决


#1

用profiler得到的结果如下:

- redisplay_internal (C function)                                2728  90%
 - jit-lock-function                                             2724  89%
  - jit-lock-fontify-now                                         2724  89%
   - jit-lock--run-functions                                     2724  89%
    - run-hook-wrapped                                           2724  89%
     - #<compiled 0x40f2001>                                     2724  89%
      - font-lock-fontify-region                                 2724  89%
       - c-font-lock-fontify-region                              2724  89%
        - font-lock-default-fontify-region                       2716  89%
         - font-lock-fontify-keywords-region                     2712  89%
          - c-font-lock-declarations                             1932  63%
           - c-find-decl-spots                                   1912  63%
            - #<compiled 0x22af545>                              1148  37%
             - c-get-fontification-context                        732  24%
              - c-looking-at-or-maybe-in-bracelist                504  16%
               + c-backward-token-2                               472  15%
              - c-back-over-member-initializers                   108   3%
               + c-back-over-compound-identifier                   56   1%
               + c-parse-state                                     32   1%
                 c-backward-sws                                     8   0%
              + c-back-over-compound-identifier                    68   2%
              + c-backward-token-2                                 16   0%
                c-backward-sws                                      4   0%
             + c-forward-decl-or-cast-1                           240   7%
             + c-forward-label                                     60   1%
             + c-backward-token-2                                  16   0%
             + c-backward-sws                                      12   0%
             + c-font-lock-single-decl                              8   0%
               c-syntactic-re-search-forward                        4   0%
            + c-bs-at-toplevel-p                                  672  22%
            + c-beginning-of-macro                                 28   0%
              c-forward-sws                                         8   0%
              c-backward-token-2                                    4   0%
            c-font-lock-<>-arglists                               304  10%
          + #<compiled 0x22b6359>                                  92   3%
            #<compiled 0x22b6381>                                  68   2%
            #<compiled 0x22b5fbd>                                  32   1%
            #<compiled 0x22b62b9>                                  32   1%
            #<compiled 0x22b625d>                                  28   0%
            #<compiled 0x22b62fd>                                  28   0%
            #<compiled 0x22b631d>                                  28   0%
            #<compiled 0x22b63d5>                                   8   0%
          + c-font-lock-enclosing-decls                             4   0%
            c-font-lock-enum-body                                   4   0%
            #<compiled 0x22b6669>                                   4   0%
           font-lock-fontify-syntactically-region                   4   0%
        + c-before-context-fl-expand-region                         8   0%

是不是哪里配置不正确?经常这样


#2

这种问题你可以提交一个bug报告

M-x report-emacs-bug

Emacs会自动生成报告模板,你填上细节就可以了。如果你不用Emacs发邮件,那就把内容复制粘贴到你的邮件软件里发到bug-gnu-emacs@gnu.org


#3

我执行了report-emacs-bug,然后emacs崩溃了……


#4

已经解决,是emacs本身的性能问题,需要降低C/C++模式下的字体渲染,用以下代码:

(setq font-lock-maximum-decoration '((c++-mode . 2) (c-mode . 2) (t . t)))

现在出现另一个问题是,每天大约一到两次,突然卡住,CPU占100%,持续30秒到1分钟,出现无预兆,所以也来不及开profiler。不过这个不是太影响日常使用,暂时不管了。


#5

砸场子的吧?哈哈


#6

:joy: 不敢不敢


#7

不至于吧?你用的难道是树莓派么?我每天上班用C++模式,从来没有遇到过字体渲染卡住的。


#8

不是,i7 4790的CPU,16G内存,虽然是四年前的电脑,但应该也不差。设置了C++下font-lock-maximum-decoration后,就没什么问题了。


#9

最近有个针对滚动慢的改进刚刚进入master哦

ec16dd1a1e0cc4c315fb57f8720083aabfd434e5
Author:     Alan Mackenzie <acm@muc.de>
AuthorDate: Sat Nov 2 13:03:50 2019 +0000
Commit:     Alan Mackenzie <acm@muc.de>
CommitDate: Sat Nov 2 13:03:50 2019 +0000

Parent:     6911ef3da69 Correct merge blunder in last icomplete commit
Merged:     emacs-24
Contained:  master
Follows:    emacs-26.1 (8168)

CC Mode.  Fix slow scrolling by adding a search limit.

This fixes bug #10149.

* lisp/progmodes/cc-fonts.el (c-font-lock-single-decl) Limit the search by
c-go-up-list-backwards to 500 non-literal characters.

1 file changed, 1 insertion(+), 1 deletion(-)
lisp/progmodes/cc-fonts.el | 2 +-

modified   lisp/progmodes/cc-fonts.el
@@ -1244,7 +1244,7 @@ c-font-lock-single-decl
   (if (save-excursion
 	(and
 	 (car (cddr decl-or-cast))	; maybe-expression flag.
-	 (c-go-up-list-backward)
+	 (c-go-up-list-backward nil (c-determine-limit 500))
 	 (eq (char-after) ?\()
 	 (progn (c-backward-syntactic-ws)
 		(c-simple-skip-symbol-backward))