Giter Site home page Giter Site logo

Comments (13)

seagle0128 avatar seagle0128 commented on May 22, 2024 1

经过测试,这种方法确实有效,大概能缩短~20%的启动时间。测试环境:

macOS Majove, GNU Emacs 26.1
Ubuntu 18.04,GNU Emacs 27.0.50.

究其原因,应该是package 包初始化之后会加载所有的安装包路径,而lisp目录在相对靠后的位置,这样每次加载配置文件会花时间找路径。这也让我想到了另外一种解决方案,就是把lisp的路径放到最前面,理论上也会缩短加载的时间,而且改动更小。我在 mscOS 和 Linux 上测试和chenbin 的方法差不多,但不太清楚 Windows 上的情况。

你能帮忙在 Windows 上对比下两种方法吗?把上述代码换成下面即可,调用的地方仍然用require

;; Load path
(add-to-list 'load-path (expand-file-name "lisp" user-emacs-directory))
(add-to-list 'load-path (expand-file-name "site-lisp" user-emacs-directory))

(defadvice package-initialize (after my-init-load-path activate)
  "Reset `load-path'."
  (cl-pushnew (expand-file-name "lisp" user-emacs-directory) load-path)
  (cl-pushnew (expand-file-name "site-lisp" user-emacs-directory) load-path))

我先commit这种方法。如果 Windows 上相差很大,我再 reopen。我相信这种方法也能极大改善启动时间。

BTW: lispsite-lisp 还是需要加入load-path,对于查找文件和跳转等还是有用的。比如放到init-const上直接C-x C-f是可以直接打开~/.emacs.d/lisp/init-const.el的。如果没有加入到load-path,这种操作是无效的。所以如果使用第一种方法也必须启动结束之后再更新。这样实现就不优雅了。

from .emacs.d.

seagle0128 avatar seagle0128 commented on May 22, 2024

谢谢你的建议!
因为我大多在 Linux 和 macOS 上开发,启动时间已经控制在2s 以内,没有注意到会有这么明显的差别。Windows 确实启动非常慢,所以我一般都打开不会关闭。
这个方案我需要时间测试评估下。Thanks again!

from .emacs.d.

cireu avatar cireu commented on May 22, 2024

wow, 似乎你的方案比宏还要快!

测试环境: Windows 10 1803 Emacs 26.1

直接添加load-path 新优化方案
emacs-init-time 8.8s 12.9s 8.1s
benchmark-init 23241ms 26161ms 20793ms

这可以说是相当惊喜了, 我猜测宏版本比较慢的原因是我没有进行字节码编译 (一般我也不会直接编译 init.el , 不然实在是太不方便了. ), 反复调用了file-truename这个函数吃了亏. 我这个宏还是直接从doom哪里提取出来的而不是直接抄的陈斌的, 陈斌的那个就算字节码编译完也不会从这里有优化, 他的comma点错了地方.

无论如何, 谢谢你所做的工作! 另外佩服你的机智 😄

from .emacs.d.

seagle0128 avatar seagle0128 commented on May 22, 2024

Cool!!! Windows 版本 Emacs 读取文件效率确实不好,毕竟是 msys 编译的。我刚才也简单测试了下,我的Windows环境里大概能缩短~30%的时间。 看起来这是个有效且简洁的solution。也非常感谢你提出的建议,才能想到这样的解决方案。 Thanks! 😄

from .emacs.d.

seagle0128 avatar seagle0128 commented on May 22, 2024

重构了下,有时间请帮忙Windows 上测试下。注意,这里只能使用push而不是cl-pushnew.

(defun update-load-path (&rest _)
  "Update `load-path'."
  (push (expand-file-name "site-lisp" user-emacs-directory) load-path)
  (push (expand-file-name "lisp" user-emacs-directory) load-path))

;; Optimize: Force "lisp"" and "site-lisp" at the head to reduce the startup time.
(advice-add #'package-initialize :after #'update-load-path)

(update-load-path)

from .emacs.d.

redguardtoo avatar redguardtoo commented on May 22, 2024

Why advice? could I just call update-load-path before package-initialize?

from .emacs.d.

cireu avatar cireu commented on May 22, 2024

谢谢, 两者基本上没有太大的区别. 不用cl-pushnew的原因是push的代码会比较简单一些吗?

我之前从未注意到file-truename的性能问题, 被你提醒后发现它是一个elisp函数, 而expand-file-name是C函数. 看来宏的问题的确是出在file-truename这里了.

另外, 我发现如果用use-pacakge指定:load-path关键字, use-package会自作聪明的也把那个path自己pushload-path的顶端. 可能用宏比较适合我这种用其他包比较多的人, Thanks!

from .emacs.d.

cireu avatar cireu commented on May 22, 2024

Why advice? could I just call update-load-path before package-initialize?

update-load-path needs to be called after package-initialize. However, package-initialize was fired up in init-package.el in his config. So he need Emacs can require init-package.el first.

from .emacs.d.

seagle0128 avatar seagle0128 commented on May 22, 2024

不用 cl-pushnew 的原因是 push 的代码会比较简单一些吗?

刚启动时cl-pushnew还没有定义,而且也要复杂一些。 为保持统一改用了push。

from .emacs.d.

seagle0128 avatar seagle0128 commented on May 22, 2024

@redguardtoo @cireu has explained above. I need to load some config files in ~/.emacs.d/lisp/ first, and keep lisp directory at the head of load-path to speedup after package-iniitialize.

from .emacs.d.

redguardtoo avatar redguardtoo commented on May 22, 2024

place (package-initalize) at the begining of init.el, update-load-path is placed at next line, then you don't need advice.

from .emacs.d.

seagle0128 avatar seagle0128 commented on May 22, 2024

@redguardtoo Right! But in 27, package-initalize call is unnecessary, I am not very sure the new behaviors. advice is the safer way, but I will do more testing on 27. Thanks.

from .emacs.d.

redguardtoo avatar redguardtoo commented on May 22, 2024

Good point.

from .emacs.d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.