perf: add additional startup optimizations

Also switches `expand-file-name` calls with the much faster
`file-name-concat` where possible.
This commit is contained in:
Henrik Lissner 2022-09-13 14:06:56 +02:00
parent 42d88421ba
commit 57a91235bd
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 53 additions and 19 deletions

View file

@ -89,6 +89,7 @@
;; Remember these variables' initial values, so we can safely reset them at a
;; later time, or consult them without fear of contamination.
(dolist (var '(exec-path load-path process-environment
load-suffixes load-file-rep-suffixes
file-name-handler-alist))
(unless (get var 'initial-value)
(put var 'initial-value (default-toplevel-value var))))
@ -191,6 +192,31 @@
(define-advice startup--load-user-init-file (:before (&rest _) undo-silence)
(advice-remove #'load-file #'load-file@silence))
;; PERF: `load-suffixes' and `load-file-rep-suffixes' are consulted on each
;; `require' and `load'. Doom won't load any dmodules this early, so omit
;; .so for a small startup boost. This is later restored in doom-start.
(set-default-toplevel-value 'load-suffixes '(".elc" ".el"))
(set-default-toplevel-value 'load-file-rep-suffixes '(""))
;; PERF: The mode-line procs a couple dozen times during startup. This is
;; normally quite fast, but disabling the default mode-line and reducing the
;; update delay timer seems to stave off ~30-50ms.
(setq-default mode-line-format nil)
(dolist (buf (buffer-list))
(with-current-buffer buf (setq mode-line-format nil)))
;; PERF,UX: Premature redisplays can substantially affect startup times and
;; produce ugly flashes of unstyled Emacs.
(setq-default inhibit-redisplay t
inhibit-message t)
;; COMPAT: Then reset it with advice, because `startup--load-user-init-file'
;; will never be interrupted by errors. And if these settings are left
;; set, Emacs could appear frozen or garbled.
(define-advice startup--load-user-init-file (:after (&rest _) undo-inhibit-vars)
(setq-default inhibit-redisplay nil
inhibit-message nil)
(unless (default-toplevel-value 'mode-line-format)
(setq-default mode-line-format (get 'mode-line-format 'initial-value))))
;; PERF: Unset a non-trivial list of command line options that aren't
;; relevant to our current OS, but `command-line-1' still processes.
(unless IS-MAC
@ -291,14 +317,16 @@
"The root directory for Doom's modules. Must end with a slash.")
(defconst doom-user-dir
(if-let (doomdir (getenv-internal "DOOMDIR"))
(expand-file-name (file-name-as-directory doomdir))
(or (let ((xdgdir
(expand-file-name "doom/"
(or (getenv-internal "XDG_CONFIG_HOME")
"~/.config"))))
(if (file-directory-p xdgdir) xdgdir))
"~/.doom.d/"))
(expand-file-name
(if-let (doomdir (getenv-internal "DOOMDIR"))
(file-name-as-directory doomdir)
(or (let ((xdgdir
(file-name-concat
(or (getenv-internal "XDG_CONFIG_HOME")
"~/.config")
"doom/")))
(if (file-directory-p xdgdir) xdgdir))
"~/.doom.d/")))
"Where your private configuration is placed.
Defaults to ~/.config/doom, ~/.doom.d or the value of the DOOMDIR envvar;
@ -391,9 +419,10 @@ This file is responsible for informing Emacs where to find all of Doom's
autoloaded core functions (in lisp/lib/*.el).")
(defconst doom-env-file
(if doom-profile
(expand-file-name "env" doom-profile-dir)
(concat doom-local-dir "env"))
(file-name-concat (if doom-profile
doom-profile-dir
doom-local-dir)
"env")
"The location of your envvar file, generated by `doom env`.
This file contains environment variables scraped from your shell environment,
@ -443,22 +472,22 @@ users).")
;; ...However, this may surprise packages (and users) that read
;; `user-emacs-directory' expecting to find the location of your Emacs config,
;; such as server.el!
(setq server-auth-dir (expand-file-name "server/" doom-emacs-dir))
(setq server-auth-dir (file-name-concat doom-emacs-dir "server/"))
;; Packages with file/dir settings that don't use `user-emacs-directory' or
;; `locate-user-emacs-file' to initialize will need to set explicitly, to stop
;; them from littering in ~/.emacs.d/.
(setq desktop-dirname (expand-file-name "desktop" doom-cache-dir)
pcache-directory (expand-file-name "pcache/" doom-cache-dir))
(setq desktop-dirname (file-name-concat doom-cache-dir "desktop")
pcache-directory (file-name-concat doom-cache-dir "pcache/"))
;; Allow the user to store custom.el-saved settings and themes in their Doom
;; config (e.g. ~/.doom.d/).
(setq custom-file (expand-file-name "custom.el" doom-user-dir))
(setq custom-file (file-name-concat doom-user-dir "custom.el"))
;; By default, Emacs stores `authinfo' in $HOME and in plain-text. Let's not do
;; that, mkay? This file stores usernames, passwords, and other treasures for
;; the aspiring malicious third party. You'll need a GPG setup though.
(setq auth-sources (list (concat doom-data-dir "authinfo.gpg")
(setq auth-sources (list (file-name-concat doom-data-dir "authinfo.gpg")
"~/.authinfo.gpg"))
(define-advice en/disable-command (:around (fn &rest args) write-to-data-dir)