Refactor startup optimizations

+ Add doom-gc-cons-upper-limit variable
+ Don't use most-positive-fixnum, in case Emacs somehow wants to
  allocate that much!
+ Don't use startup optimizations in noninteractive sessions
+ Restore gc-cons-threshold on idle timer, instead of hook (to defer the
  possible GC pause and so deferred packages can take advantage of these
  optimizations).
This commit is contained in:
Henrik Lissner 2018-09-03 01:18:52 +02:00
parent 81ee563c4c
commit eaa10946f1
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 33 additions and 23 deletions

View file

@ -300,7 +300,7 @@ original value of `symbol-file'."
;; Don't garbage collect to speed up minibuffer commands
(defun doom|defer-garbage-collection ()
(setq gc-cons-threshold most-positive-fixnum))
(setq gc-cons-threshold doom-gc-cons-upper-limit))
(defun doom|restore-garbage-collection ()
(setq gc-cons-threshold doom-gc-cons-threshold))
(add-hook 'minibuffer-setup-hook #'doom|defer-garbage-collection)
@ -318,12 +318,13 @@ easier to tell where the came from.
Meant to be used with `run-hook-wrapped'."
(when doom-debug-mode
(message "Running doom hook: %s" hook))
(condition-case e
(funcall hook)
((debug error)
(signal 'doom-hook-error (list hook e))))
;; return nil so `run-hook-wrapped' won't short circuit
nil)
(let ((gc-cons-threshold doom-gc-cons-upper-limit))
(condition-case e
(funcall hook)
((debug error)
(signal 'doom-hook-error (list hook e))))
;; return nil so `run-hook-wrapped' won't short circuit
nil))
(defun doom-ensure-same-emacs-version-p ()
"Check if the running version of Emacs has changed and set

41
init.el
View file

@ -27,30 +27,38 @@
;;
;;; License: MIT
(defvar doom-gc-cons-threshold (* 8 1024 1024)
"The default value to use for `gc-cons-threshold'.")
(defvar doom-gc-cons-threshold 16777216 ; 16mb
"The default value to use for `gc-cons-threshold'. If you experience freezing,
decrease this. If you experience stuttering, increase this.")
(defvar doom-gc-cons-upper-limit 268435456 ; 256mb
"The temporary value for `gc-cons-threshold' to defer it.")
(defvar doom--file-name-handler-alist file-name-handler-alist)
(unless after-init-time
(defun doom|restore-startup-optimizations ()
"Resets garbage collection settings to reasonable defaults (a large
`gc-cons-threshold' can cause random freezes otherwise) and resets
`file-name-handler-alist'."
(setq file-name-handler-alist doom--file-name-handler-alist
gc-cons-threshold doom-gc-cons-threshold))
(if (or after-init-time noninteractive)
(setq gc-cons-threshold doom-gc-cons-threshold)
;; A big contributor to long startup times is the garbage collector, so we up
;; its memory threshold, temporarily and reset it later in
;; `doom|disable-startup-optimizations'.
(setq gc-cons-threshold most-positive-fixnum)
(setq gc-cons-threshold doom-gc-cons-upper-limit) ; 256mb
;; This is consulted on every `require', `load' and various file reading
;; functions. You get a minor speed up by nooping this.
(setq file-name-handler-alist nil))
(defun doom|disable-startup-optimizations ()
"Resets garbage collection settings to reasonable defaults (if you don't do
this, you'll get stuttering and random freezes) and resets
`file-name-handler-alist'."
(setq file-name-handler-alist doom--file-name-handler-alist
gc-cons-threshold doom-gc-cons-threshold)
(makunbound 'doom--file-name-handler-alist))
(add-hook 'emacs-startup-hook #'doom|disable-startup-optimizations)
(add-hook 'doom-reload-hook #'doom|disable-startup-optimizations)
(setq file-name-handler-alist nil)
;; Restore these to their defaults later. Do it on an idle timer to defer the
;; possible GC pause, and to give deferred packages the ability to take
;; advantage of these optimizations.
(run-with-idle-timer 5 nil #'doom|restore-startup-optimizations)
(add-hook 'doom-reload-hook #'doom|restore-startup-optimizations))
;; Ensure Doom is always running out of this file's directory
@ -60,5 +68,6 @@ this, you'll get stuttering and random freezes) and resets
;; load errors, it may help to set this to nil.
(setq load-prefer-newer noninteractive)
;; Let 'er rip!
(require 'core (concat user-emacs-directory "core/core"))