提高emacs启动速度至少10%的技巧

来自reddit,亲测有效,需要Emacs 27,

亮点是gc设置放在 early-init.el 中,之前我都放在 init.el 。截图对比了两者的效果。

优化前,

优化后,

5 个赞

最初在lsp-mode的 文档 看到这个设置, gc-cons-thresholdread-process-output-max,以为这个只是对提高lsp性能需要,原来还可提高emacs启动速度

可以看看懒猫这篇文章的前半部分 https://manateelazycat.github.io/emacs/2019/05/05/lazy-load.html

只在启动的时候最大化GC,运行时使用 gcmh 这个包进行 GC 管理

doom emacs 的这个 issue 解释的比较详细

1 个赞

通过 GC 参数优化Emacs启动速度的方案, 我之前专门搜索过.

我的优化方案包括不仅限于调整 cons 值.

具体见 init-speedup.el : 加速启动配置, 主要是 gc

GC 优化主要几个思路我列举一下 :

  1. 调整 cons 值加快启动速度, 启动之后调整到正常值
  2. 利用 idle time 垃圾回收 (gcmh package)
  3. 鼠标焦点 不在 emacs的时候执行垃圾回收
  4. minibuffer 出现的时候禁止垃圾回收

最后, 我要祭出 Emacs启动速度优化终极大杀器 : dump 大法. 直接将启动速度降到 1s 以下.

gc的技巧我很早就用了。reddit文章创新的地方是把改gc的代码移到了 early-init.el . 不是很确定 read-process-output-max 的效果。emacs的瓶颈是lisp转换文本到数据结构的效率太低,和读入文本的大小是否有关系我得测一下。

更新,在init.el最后加入以下代码,

(let* ((gc-cons-threshold most-positive-fixnum)
       (read-process-output-max (* 1024 1024))
       )
  (message "default-directory=%s benchmark=%S"
           default-directory
           (benchmark-run-compiled 1
             (my-ensure 'find-file-in-project)
             (let* ((cnt (ffip-project-search "t" nil)))
               (message "cnt=%s" (length cnt))))))

删除设置read-process-output-max 一行,启动emacs并关闭,恢复删除的代码,启动emacs并关闭。

以下是输出,

$ emacs -Q -nw --debug-init --batch --eval "(setq my-disable-idle-timer t)" -l init.el
Loading /home/cb/.emacs.d/.session...
Loading /home/cb/.custom.el (source)...
Color theme [doom-henna] loaded.
*** Emacs loaded in 0.00 seconds with 0 garbage collections.
Source file ‘/home/cb/.emacs.d/elpa/find-file-in-project-6.0.7/find-file-in-project.el’ newer than byte-compiled file; using older file
cnt=13577
default-directory=~/.emacs.d/ benchmark=(0.999218989 0 0.0)
cb@sydneypc:~/.emacs.d on master
$ fg
TERM=xterm-256color emacs -nw "$@"	(wd: ~)

[1]+  Stopped                 TERM=xterm-256color emacs -nw "$@"  (wd: ~)
(wd now: ~/.emacs.d)
cb@sydneypc:~/.emacs.d on master
$ emacs -Q -nw --debug-init --batch --eval "(setq my-disable-idle-timer t)" -l init.el
Loading /home/cb/.emacs.d/.session...
Loading /home/cb/.custom.el (source)...
Color theme [doom-gruvbox] loaded.
*** Emacs loaded in 0.00 seconds with 0 garbage collections.
Source file ‘/home/cb/.emacs.d/elpa/find-file-in-project-6.0.7/find-file-in-project.el’ newer than byte-compiled file; using older file
cnt=13577
default-directory=~/.emacs.d/ benchmark=(1.005885627 0 0.0)

没什么区别,测试的命令提取了命令行程序Find的13577行的输出转换成一个list。