nit: reformat early-init

This commit is contained in:
Henrik Lissner
2024-10-22 16:23:48 -04:00
parent 9ae7aa1122
commit c23b6fa5db

View File

@ -26,118 +26,117 @@
;; ;;
;;; Code: ;;; Code:
;; PERF: Garbage collection is a big contributor to startup times. This fends it (let (file-name-handler-alist)
;; off, but will be reset later by `gcmh-mode' (or in doom-cli.el, if in a ;; PERF: Garbage collection is a big contributor to startup times. This fends
;; noninteractive session). Not resetting it later causes stuttering/freezes. ;; it off, but will be reset later by `gcmh-mode'. Not resetting it later
(setq gc-cons-threshold most-positive-fixnum) ;; causes stuttering/freezes.
(if noninteractive
;; PERF: Deferring the GC in non-interactive sessions isn't as important,
;; but still yields a notable benefit. Still, avoid setting it to high
;; here, as runaway memory usage is a real risk in longer sessions.
(setq gc-cons-threshold 134217728 ; 128mb
;; Backported from 29 (see emacs-mirror/emacs@73a384a98698)
gc-cons-percentage 1.0)
(setq gc-cons-threshold most-positive-fixnum))
;; PERF: Don't use precious startup time checking mtime on elisp bytecode. ;; PERF: Don't use precious startup time checking mtime on elisp bytecode.
;; Ensuring correctness is 'doom sync's job, not the interactive session's. ;; Ensuring correctness is 'doom sync's job, not the interactive session's.
;; Still, stale byte-code will cause *heavy* losses in startup efficiency, but ;; Still, stale byte-code will cause *heavy* losses in startup efficiency,
;; performance is unimportant when Emacs is in an error state. ;; but performance is unimportant when Emacs is in an error state.
(setq load-prefer-newer noninteractive) (setq load-prefer-newer noninteractive)
;; UX: Respect DEBUG envvar as an alternative to --debug-init, and to make ;; UX: Respect DEBUG envvar as an alternative to --debug-init, and to make
;; startup sufficiently verbose from this point on. ;; startup sufficiently verbose from this point on.
(when (getenv-internal "DEBUG") (when (getenv-internal "DEBUG")
(setq init-file-debug t (setq init-file-debug t
debug-on-error t)) debug-on-error t))
(let (;; FIX: Unset `command-line-args' in noninteractive sessions, to
;; ensure upstream switches aren't misinterpreted.
(command-line-args (unless noninteractive command-line-args))
;; I avoid using `command-switch-alist' to process --profile (and
;; --init-directory) because it is processed too late to change
;; `user-emacs-directory' in time.
(profile (or (cadr (member "--profile" command-line-args))
(getenv-internal "DOOMPROFILE"))))
(if (null profile)
;; REVIEW: Backported from Emacs 29. Remove when 28 support is dropped.
(let ((init-dir (or (cadr (member "--init-directory" command-line-args))
(getenv-internal "EMACSDIR"))))
(if (null init-dir)
;; FIX: If we've been loaded directly (via 'emacs -batch -l
;; early-init.el') or by a doomscript (like bin/doom), and Doom
;; is in a non-standard location (and/or Chemacs is used), then
;; `user-emacs-directory' will be wrong.
(when noninteractive
(setq user-emacs-directory
(file-name-directory (file-truename load-file-name))))
;; FIX: To prevent "invalid option" errors later.
(push (cons "--init-directory" (lambda (_) (pop argv))) command-switch-alist)
(setq user-emacs-directory (expand-file-name init-dir))))
;; FIX: Discard the switch to prevent "invalid option" errors later.
(push (cons "--profile" (lambda (_) (pop argv))) command-switch-alist)
;; Running 'doom sync' or 'doom profile sync' (re)generates a light
;; profile loader in $EMACSDIR/profiles/load.el (or $DOOMPROFILELOADFILE),
;; after reading `doom-profile-load-path'. This loader requires
;; `$DOOMPROFILE' be set to function.
(setenv "DOOMPROFILE" profile)
(or (load (let ((windows? (memq system-type '(ms-dos windows-nt cygwin))))
(expand-file-name
(format (or (getenv-internal "DOOMPROFILELOADFILE")
(file-name-concat (if windows? "doomemacs/data" "doom")
"profiles.%d.el"))
emacs-major-version)
(or (if windows? (getenv-internal "LOCALAPPDATA"))
(getenv-internal "XDG_DATA_HOME")
"~/.local/share"))
'noerror (not init-file-debug) 'nosuffix))
(user-error "Profiles not initialized yet; run 'doom sync' first"))))
;; ;; PERF: When `load'ing or `require'ing files, each permutation of
;;; Bootstrap ;; `load-suffixes' and `load-file-rep-suffixes' (then `load-suffixes' +
;; `load-file-rep-suffixes') is used to locate the file. Each permutation
(or ;; amounts to at least one file op, which is normally very fast, but can add
;; PERF: `file-name-handler-alist' is consulted often. Unsetting it offers a ;; up over the hundreds/thousands of files Emacs loads.
;; notable saving in startup time. This is just a stopgap though; this ;;
;; optimization is continued more comprehensively in lisp/doom.el. ;; To reduce that burden -- and since Doom doesn't load any dynamic modules
(let (file-name-handler-alist) ;; this early -- I remove `.so' from `load-suffixes' and pass the
(let (;; FIX: Unset `command-line-args' in noninteractive sessions, to ;; `must-suffix' arg to `load'. See the docs of `load' for details.
;; ensure upstream switches aren't misinterpreted. (if (let ((load-suffixes '(".elc" ".el"))
(command-line-args (unless noninteractive command-line-args)) (doom (expand-file-name "lisp/doom" user-emacs-directory)))
;; I avoid using `command-switch-alist' to process --profile (and ;; I avoid `load's NOERROR argument because it suppresses other,
;; --init-directory) because it is processed too late to change ;; legitimate errors (like permission or IO errors), which gets
;; `user-emacs-directory' in time. ;; incorrectly interpreted as "this is not a Doom config".
(profile (or (cadr (member "--profile" command-line-args)) (if (file-exists-p (concat doom ".el"))
(getenv-internal "DOOMPROFILE")))) ;; Load the heart of Doom Emacs.
(if (null profile) (load doom nil (not init-file-debug) nil 'must-suffix)
;; REVIEW: Backported from Emacs 29. Remove when 28 support is dropped. ;; Failing that, assume we're loading a non-Doom config...
(let ((init-dir (or (cadr (member "--init-directory" command-line-args)) ;; HACK: `startup--load-user-init-file' resolves $EMACSDIR from a
(getenv-internal "EMACSDIR")))) ;; lexical (and so, not-trivially-modifiable)
(if (null init-dir) ;; `startup-init-directory', so Emacs will fail to locate the
;; FIX: If we've been loaded directly (via 'emacs -batch -l ;; correct $EMACSDIR/init.el without help.
;; early-init.el') or by a doomscript (like bin/doom), and Doom (define-advice startup--load-user-init-file (:filter-args (args) reroute-to-profile)
;; is in a non-standard location (and/or Chemacs is used), then (list (lambda () (expand-file-name "init.el" user-emacs-directory))
;; `user-emacs-directory' will be wrong. nil (nth 2 args)))
(when noninteractive ;; (Re)set `user-init-file' for the `load' call further below, and do
(setq user-emacs-directory ;; so here while our `file-name-handler-alist' optimization is still
(file-name-directory (file-truename load-file-name)))) ;; effective (benefits `expand-file-name'). BTW: Emacs resets
;; FIX: To prevent "invalid option" errors later. ;; `user-init-file' and `early-init-file' after this file is loaded.
(push (cons "--init-directory" (lambda (_) (pop argv))) command-switch-alist) (setq user-init-file (expand-file-name "early-init" user-emacs-directory))
(setq user-emacs-directory (expand-file-name init-dir)))) ;; COMPAT: I make no assumptions about the config we're going to load,
;; FIX: Discard the switch to prevent "invalid option" errors later. ;; so undo this file's global side-effects.
(push (cons "--profile" (lambda (_) (pop argv))) command-switch-alist) (setq load-prefer-newer t)
;; Running 'doom sync' or 'doom profile sync' (re)generates a light ;; PERF: But make an exception for `gc-cons-threshold', which I think
;; profile loader in $XDG_DATA_HOME/doom/profiles.X.el (or ;; all Emacs users and configs will benefit from. Still, setting it
;; $DOOMPROFILELOADFILE), after reading `doom-profile-load-path'. This ;; to `most-positive-fixnum' is dangerous if downstream does not
;; loader requires `$DOOMPROFILE' be set to function. ;; reset it later to something reasonable, so I use 16mb as a best
(setenv "DOOMPROFILE" profile) ;; fit guess. It's better than Emacs' 80kb default.
(or (load (let ((windows? (memq system-type '(ms-dos windows-nt cygwin)))) (setq gc-cons-threshold (* 16 1024 1024))
(expand-file-name nil))
(format (or (getenv-internal "DOOMPROFILELOADFILE")
(file-name-concat (if windows? "doomemacs/data" "doom")
"profiles.%d.el"))
emacs-major-version)
(or (if windows? (getenv-internal "LOCALAPPDATA"))
(getenv-internal "XDG_DATA_HOME")
"~/.local/share"))
'noerror (not init-file-debug) 'nosuffix))
(user-error "Profiles not initialized yet; run 'doom sync' first"))))
;; PERF: When `load'ing or `require'ing files, each permutation of
;; `load-suffixes' and `load-file-rep-suffixes' (then `load-suffixes' +
;; `load-file-rep-suffixes') is used to locate the file. Each permutation
;; amounts to at least one file op, which is normally very fast, but can
;; add up over the hundreds/thousands of files Emacs loads.
;;
;; To reduce that burden -- and since Doom doesn't load any dynamic modules
;; this early -- I remove `.so' from `load-suffixes' and pass the
;; `must-suffix' arg to `load'. See the docs of `load' for details.
(if (let ((load-suffixes '(".elc" ".el"))
(doom-file (expand-file-name "lisp/doom" user-emacs-directory)))
;; I avoid `load's NOERROR argument because it suppresses other,
;; legitimate errors (like permission or IO errors), which gets
;; incorrectly interpreted as "this is not a Doom config".
(if (file-exists-p (concat doom-file ".el"))
;; Load the heart of Doom Emacs.
(load doom-file nil (not init-file-debug) nil 'must-suffix)
;; Failing that, assume we're loading a non-Doom config...
;; HACK: `startup--load-user-init-file' resolves $EMACSDIR from a
;; lexical (and so, not-trivially-modifiable)
;; `startup-init-directory', so Emacs will fail to locate the
;; correct $EMACSDIR/init.el without help.
(define-advice startup--load-user-init-file (:filter-args (args) reroute-to-profile)
(list (lambda () (expand-file-name "init.el" user-emacs-directory))
nil (nth 2 args)))
;; (Re)set `user-init-file' for the `load' call further below, and do
;; so here while our `file-name-handler-alist' optimization is still
;; effective (benefits `expand-file-name'). BTW: Emacs resets
;; `user-init-file' and `early-init-file' after this file is loaded.
(setq user-init-file (expand-file-name "early-init" user-emacs-directory))
;; COMPAT: I make no assumptions about the config we're going to
;; load, so undo this file's global side-effects.
(setq load-prefer-newer t)
;; PERF: But make an exception for `gc-cons-threshold', which I think
;; all Emacs users and configs will benefit from. Still, setting it
;; to `most-positive-fixnum' is dangerous if downstream does not
;; reset it later to something reasonable, so I use 16mb as a best
;; fit guess. It's better than Emacs' 80kb default.
(setq gc-cons-threshold (* 16 1024 1024))
nil))
;; ...Otherwise, we're loading a Doom config, so continue as normal. ;; ...Otherwise, we're loading a Doom config, so continue as normal.
(doom-require (if noninteractive 'doom-cli 'doom-start)))) (doom-require (if noninteractive 'doom-cli 'doom-start))
;; If we're here, the user wants to load another config/profile (that may or
;; Then continue on to the config/profile we want to load. ;; may not be a Doom config).
(load user-init-file 'noerror (not init-file-debug) nil 'must-suffix)) (load user-init-file 'noerror (not init-file-debug) nil 'must-suffix)))
;;; early-init.el ends here ;;; early-init.el ends here