Major optimization refactor, across the board

+ enable lexical-scope everywhere (lexical-binding = t): ~5-10% faster
  startup; ~5-20% general boost
+ reduce consing, function calls & garbage collection by preferring
  cl-loop & dolist over lambda closures (for mapc[ar], add-hook, and
  various cl-lib filter/map/reduce functions) -- where possible
+ prefer functions with dedicated opcodes, like assq (see byte-defop's
  in bytecomp.el for more)
+ prefer pcase & cond (faster) over cl-case
+ general refactor for code readability
+ ensure naming & style conventions are adhered to
+ appease byte-compiler by marking unused variables with underscore
+ defer minor mode activation to after-init, emacs-startup or
  window-setup hooks; a customization opportunity for users + ensures
  custom functionality won't interfere with startup.
This commit is contained in:
Henrik Lissner 2017-06-08 11:47:56 +02:00
parent 64a142b3fc
commit c7254e7bdc
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
154 changed files with 1101 additions and 1118 deletions

View file

@ -1,4 +1,4 @@
;;; core-packages.el
;;; core-packages.el --- package management system -*- lexical-binding: t; -*-
;; Emacs package management is opinionated. Unfortunately, so am I. I've bound
;; together `use-package', `quelpa' and package.el to create my own,
@ -76,7 +76,7 @@ base by `doom!' and for calculating how many packages exist.")
(defvar doom--refresh-p nil)
(setq load-prefer-newer noninteractive
(setq load-prefer-newer (or noninteractive doom-debug-mode)
package--init-file-ensured t
package-user-dir (expand-file-name "elpa" doom-packages-dir)
package-enable-at-startup nil
@ -96,7 +96,7 @@ base by `doom!' and for calculating how many packages exist.")
use-package-always-defer t
use-package-always-ensure nil
use-package-expand-minimally (eval-when-compile (not doom-debug-mode))
use-package-expand-minimally (not doom-debug-mode)
use-package-debug nil
use-package-verbose doom-debug-mode
use-package-minimum-reported-time (if doom-debug-mode 0 0.1)
@ -144,7 +144,7 @@ to speed up startup."
;; Also, in some edge cases involving package initialization during a
;; non-interactive session, `package-initialize' fails to fill `load-path'.
;; If we want something done right, do it ourselves!
(setq doom--package-load-path (directory-files package-user-dir t "^\\w" t)
(setq doom--package-load-path (directory-files package-user-dir t "^[^.]" t)
load-path (append load-path doom--package-load-path))
;; Ensure core packages are installed
@ -164,7 +164,7 @@ to speed up startup."
(setq doom-init-p t)))
(defun doom-initialize-autoloads (&optional inhibit-reload-p)
(defun doom-initialize-autoloads ()
"Ensures that `doom-autoload-file' exists and is loaded. Otherwise run
`doom/reload-autoloads' to generate it."
(unless (file-exists-p doom-autoload-file)
@ -189,15 +189,16 @@ This aggressively reloads core autoload files."
(funcall load-fn (expand-file-name "init.el" doom-emacs-dir))
(funcall load-fn (doom-module-path :private user-login-name "init.el"))
(when load-p
(mapc (lambda (file) (funcall load-fn file t))
(append (nreverse (file-expand-wildcards (concat doom-core-dir "core*.el")))
(file-expand-wildcards (concat doom-core-dir "autoload/*.el"))
(doom--module-paths "config.el")))))
(cl-loop for file
in (append (nreverse (file-expand-wildcards (expand-file-name "core*.el" doom-core-dir)))
(file-expand-wildcards (expand-file-name "autoload/*.el" doom-core-dir))
(doom--module-paths "config.el"))
do (funcall load-fn file t))))
(when (or force-p (not doom-packages))
(setq doom-packages nil)
(funcall load-fn (expand-file-name "packages.el" doom-core-dir))
(mapc (lambda (file) (funcall load-fn file t))
(doom--module-paths "packages.el")))))
(dolist (file (doom--module-paths "packages.el"))
(funcall load-fn file t)))))
(defun doom-initialize-modules (modules)
"Adds MODULES to `doom-modules'. MODULES must be in mplist format.
@ -213,15 +214,12 @@ This aggressively reloads core autoload files."
(error "No namespace specified on `doom!' for %s" m))
((eq m '*)
(doom-initialize-modules
(cons mode
(mapcar
(lambda (dir) (intern (file-name-nondirectory dir)))
(cl-remove-if-not
#'file-directory-p
(directory-files (expand-file-name
(substring (symbol-name mode) 1)
doom-modules-dir)
t "^\\w"))))))
(cl-loop with modpath = (expand-file-name (substring (symbol-name mode) 1) doom-modules-dir)
for path in (directory-files modpath t "^\\w")
if (file-directory-p path)
collect (intern (file-name-nondirectory path)) into paths
finally return (cons mode paths)
finally do (message "== %s %s" mode paths))))
(t
(doom--enable-module mode m))))))
@ -243,11 +241,9 @@ This aggressively reloads core autoload files."
(defun doom--module-pairs ()
"Returns `doom-modules' as a list of (MODULE . SUBMODULE) cons cells. The list
is sorted by order of insertion."
(let (pairs)
(when (hash-table-p doom-modules)
(maphash (lambda (key value) (push (cons (car key) (cdr key)) pairs))
doom-modules)
(nreverse pairs))))
(when (hash-table-p doom-modules)
(cl-loop for key being the hash-keys of doom-modules
collect (cons (car key) (cdr key)))))
(defun doom--module-paths (&optional append-file)
"Returns a list of absolute file paths to modules, with APPEND-FILE added, if
@ -291,19 +287,17 @@ byte-compilation."
(unless noninteractive
(load ,(doom-module-path :private user-login-name "init") t t)
,@(let (forms)
(dolist (module (doom--module-pairs))
(push `(require! ,(car module) ,(cdr module) t) forms))
(unless (doom-module-loaded-p :private (intern user-login-name))
(push `(require! :private ,user-login-name t) forms))
(nreverse forms))
,@(cl-loop for (module . submodule) in (doom--module-pairs)
collect `(require! ,module ,submodule t))
,(unless (doom-module-loaded-p :private (intern user-login-name))
`(require! :private ,user-login-name t))
(when (display-graphic-p)
(require 'server)
(unless (server-running-p)
(server-start)))
(add-hook 'after-init-hook #'doom--display-benchmark t))))
(add-hook 'emacs-startup-hook #'doom--display-benchmark t))))
(defmacro def-package! (name &rest plist)
"A thin wrapper around `use-package'."
@ -425,7 +419,7 @@ SUBMODULE is a symbol."
;; Commands
;;
(defun doom/reload (&optional ignorable-p)
(defun doom/reload ()
"Reload `load-path' and recompile files (if necessary). Useful if you
modify/update packages outside of emacs. Automatically called (through the
server, if necessary) by `doom/packages-install', `doom/packages-update' and
@ -434,7 +428,7 @@ server, if necessary) by `doom/packages-install', `doom/packages-update' and
(cond (noninteractive
(message "Reloading...")
(require 'server)
(unless (ignore-errors (server-eval-at "server" '(doom/reload t)))
(unless (ignore-errors (server-eval-at "server" '(doom/reload)))
(message "Recompiling")
(doom/recompile)))
(t
@ -454,35 +448,34 @@ In modules, checks modules/*/autoload.el and modules/*/autoload/*.el.
Rerun this whenever init.el is modified. You can also use `make autoloads` from
the commandline."
(interactive)
;; This function must not use `cl-lib', autoloaded functions or external
;; dependencies. It must assume nothing is set up!
;; This function must not use autoloaded functions or external dependencies.
;; It must assume nothing is set up!
(doom-initialize-packages (not noninteractive))
(let ((generated-autoload-file doom-autoload-file)
(autoload-files
(let ((evil-p (featurep! :feature evil))
(targets
(file-expand-wildcards
(expand-file-name "autoload/*.el" doom-core-dir))))
(dolist (path (doom--module-paths))
(let ((auto-dir (expand-file-name "autoload" path))
(auto-file (expand-file-name "autoload.el" path)))
(when (file-exists-p auto-file)
(push auto-file autoload-files))
(push auto-file targets))
(when (file-directory-p auto-dir)
(mapc (lambda (file)
;; Make evil*.el autoload files a special case; don't load
;; them unless evil is enabled.
(unless (and (string-prefix-p "evil" (file-name-nondirectory file))
(not (featurep! :feature evil)))
(push file autoload-files)))
(file-expand-wildcards (expand-file-name "*.el" auto-dir) t)))))
(when (file-exists-p generated-autoload-file)
(delete-file generated-autoload-file)
(dolist (file (file-expand-wildcards (expand-file-name "*.el" auto-dir) t))
;; Make evil*.el autoload files a special case; don't load
;; them unless evil is enabled.
(unless (and (string-prefix-p "evil" (file-name-nondirectory file))
(not evil-p))
(push file targets))))))
(when (file-exists-p doom-autoload-file)
(delete-file doom-autoload-file)
(message "Deleted old autoloads.el"))
(dolist (file (nreverse autoload-files))
(dolist (file (nreverse targets))
(let ((inhibit-message (not doom-debug-mode)))
(update-file-autoloads file))
(update-file-autoloads file nil doom-autoload-file))
(message "Scanned %s" (file-relative-name file doom-emacs-dir)))
(condition-case ex
(let ((buf (get-file-buffer generated-autoload-file)))
(let ((buf (get-file-buffer doom-autoload-file)))
(unwind-protect
(with-current-buffer buf
(save-buffer)
@ -490,7 +483,7 @@ the commandline."
(message "Finished generating autoloads.el!"))
(kill-buffer buf)))
('error
(delete-file generated-autoload-file)
(delete-file doom-autoload-file)
(error "Couldn't evaluate autoloads.el: %s" (cadr ex))))))
(defun doom/compile (&optional lite-p only-recompile-p)
@ -506,46 +499,50 @@ If ONLY-RECOMPILE-P is non-nil, only recompile out-of-date files."
;; Ensure all relevant config files are loaded and up-to-date. This way we
;; don't need eval-when-compile and require blocks scattered all over.
(doom-initialize-packages t t)
(let ((targets (append (list "init.el" doom-core-dir)
(unless lite-p (doom--module-paths))))
(let ((targets
(cond ((equal (car command-line-args-left) "--")
(cl-loop for file in command-line-args-left
if (file-exists-p file)
collect (expand-file-name file)
finally do (setq command-line-args-list nil)) )
(t
(append (list (expand-file-name "init.el" doom-emacs-dir)
doom-core-dir)
(unless lite-p (doom--module-paths))))))
(total-success 0)
(total-fail 0)
(total-nocomp 0)
el-files)
(mapc (lambda (file)
(when (or (not only-recompile-p)
(let ((elc-file (byte-compile-dest-file file)))
(and (file-exists-p elc-file)
(file-newer-than-file-p file elc-file))))
(let ((result (byte-compile-file file))
(short-name (file-relative-name file doom-emacs-dir)))
(cl-incf
(cond ((eq result 'no-byte-compile)
(message! (dark (white "Ignored %s" short-name)))
total-nocomp)
((null result)
(message! (red "Failed to compile %s" short-name))
total-fail)
(t
(message! (green "Compiled %s" short-name))
total-success))))))
(dolist (path targets (reverse el-files))
(let ((path (expand-file-name path doom-emacs-dir)))
(cond ((file-directory-p path)
(setq el-files (append (directory-files-recursively path "\\.el$") el-files)))
((file-exists-p path)
(push path el-files))
(t
(error "Invalid path: %s" path))))))
(message!
(bold
(color (if (zerop total-fail) 'green 'red)
"%s %s file(s) %s"
(if only-recompile-p "Recompiled" "Compiled")
(format (if el-files "%d/%d" "%d")
total-success
(- (length el-files) total-nocomp))
(format "(%s not compiled)" total-nocomp))))))
(total-nocomp 0))
(let ((el-files (cl-loop for path in targets
if (file-directory-p path)
nconc (nreverse (directory-files-recursively path "\\.el$"))
else if (file-exists-p path)
collect path)))
(dolist (file el-files)
(when (or (not only-recompile-p)
(let ((elc-file (byte-compile-dest-file file)))
(and (file-exists-p elc-file)
(file-newer-than-file-p file elc-file))))
(let ((result (byte-compile-file file))
(short-name (file-relative-name file doom-emacs-dir)))
(cl-incf
(cond ((eq result 'no-byte-compile)
(message! (dark (white "Ignored %s" short-name)))
total-nocomp)
((null result)
(message! (red "Failed to compile %s" short-name))
total-fail)
(t
(message! (green "Compiled %s" short-name))
total-success))))))
(message!
(bold
(color (if (= total-fail 0) 'green 'red)
"%s %s file(s) %s"
(if only-recompile-p "Recompiled" "Compiled")
(format (if el-files "%d/%d" "%d")
total-success
(- (length el-files) total-nocomp))
(format "(%s ignored)" total-nocomp)))))))
(defun doom/recompile ()
"Recompile any compiled *.el files in your Emacs configuration."
@ -554,12 +551,6 @@ If ONLY-RECOMPILE-P is non-nil, only recompile out-of-date files."
;; In case `load-path' has changed (e.g. after an update)
(byte-recompile-file (expand-file-name "core.el" doom-core-dir) t))
(defun doom/compile-lite ()
"A light-weight version of `doom/compile' which only compiles core files in
your emacs configuration (init.el and core/**/*.el)."
(interactive)
(doom/compile t))
(defun doom/clean-cache ()
"Clear local cache (`doom-cache-dir'). You may need to restart Emacs for some
components to feel its effects."
@ -571,15 +562,14 @@ components to feel its effects."
"Delete all compiled elc files in DOOM emacs, excluding compiled ELPA/QUELPA
package files."
(interactive)
(let ((elc-files (append
(let ((init-elc (expand-file-name "init.elc" doom-emacs-dir)))
(if (file-exists-p init-elc) (list init-elc)))
(directory-files-recursively doom-core-dir "\\.elc$")
(directory-files-recursively doom-modules-dir "\\.elc$"))))
(if elc-files
(dolist (file elc-files)
(delete-file file)
(message "Deleting %s" (file-relative-name file doom-emacs-dir)))
(let ((targets (append (list (expand-file-name "init.elc" doom-emacs-dir))
(directory-files-recursively doom-core-dir "\\.elc$")
(directory-files-recursively doom-modules-dir "\\.elc$"))))
(unless (cl-loop for path in targets
if (file-exists-p path)
collect path
and do (delete-file path)
and do (message "Deleted %s" (file-relative-name path)))
(message "Everything is clean"))))