Advanced Techniques for Reducing Emacs Startup Time

Six techniques to reduce Emacs startup time by the author of the Emacs Start Up Profiler.

tl;dr: Do these steps:

Profile with Esup. Adjust the garbage collection threshold. Autoload everything with use-package. Avoid helper functions which cause eager loads. See my Emacs config for an example.

From .emacs.d Bankruptcy to Now

I recently declared my third .emacs.d bankruptcy and finished the fourth iteration of my Emacs configuration. The evolution was:

Copy and paste elisp snippets into ~/.emacs and hope it works. Adopt a more structured approach with el-get to manage dependencies. Give up and outsource to Spacemacs. Get tired of Spacemacs intricacies and rewrite with use-package .

This article is a collection of tips collected during the 3 rewrites and from creating the Emacs Start Up Profiler. Many thanks to the teams behind Spacemacs, use-package and general. Without these dedicated voluteers, this task would be vastly more difficult.

But What About Daemon Mode

Before we get started, let me acknowledge the common retort when optimizing Emacs: “Emacs is meant to run as a daemon so you’ll only start it once.” That’s all well and good except:

Fast things feel nicer.

When customizing Emacs, you sometimes get into weird states that can be hard to recover from without restarting. For example, if you add a slow lambda function to your post-command-hook , it’s tough to remove it.

function to your , it’s tough to remove it. Restarting Emacs helps verify that customization will persist between sessions.

1. Establish the Current and Best Possible Start Up Time

The first step is to measure the current start up time. The easy way is to display the information at startup which will show progress through the next steps.

(add-hook 'emacs-startup-hook ( lambda () (message "Emacs ready in %s with %d garbage collections." (format "%.2f seconds" (float-time (time-subtract after-init-time before-init-time))) gcs-done)))

Second, measure the best possible startup speed so you know what’s possible. Mine is 0.3 seconds.

emacs -q --eval= '(message "%s" (emacs-init-time))' ;; For macOS users: open -n /Applications/Emacs.app --args -q --eval= '(message "%s" (emacs-init-time))'

2. Profile Emacs Startup for Easy Wins

The Emacs StartUp Profiler (ESUP) will give you detailed metrics for top-level expressions.

Figure 1: Emacs Start Up Profiler Screenshot

WARNING: Spacemacs users, ESUP currently chokes on the Spacemacs init.el file. Follow https://github.com/jschaf/esup/issues/48 for updates.

3. Set the Garbage Collection Threshold Higher during Startup

This saves about 0.3 seconds on my configuration.

The default value for Emacs is 760kB which is extremely conservative on a modern machine. The real trick is to lower it back to something reasonable after initialization. This saves about 0.3 seconds on my init files.

( setq gc-cons-threshold (* 50 1000 1000)) ( setq gc-cons-threshold (* 2 1000 1000))

4. Never require anything; autoload with use-package instead

The best way to make Emacs faster is to do less. Running require eagerly loads the underlying source file. It’s rare the you’ll need functionality immediately at startup time.

With use-package , you declare which features you need from a package and use-package does the right thing. Here’s what it looks like:

(use-package evil-lisp-state :defer t :init ( setq evil-lisp-state-global nil) :config (abn/define-leader-keys "k" evil-lisp-state-map))

To see what packages Emacs currently has loaded, examine the features variable. For nice output see lpkg explorer or my variant in abn-funcs-benchmark.el. The output looks like:

479 features currently loaded - abn-funcs-benchmark: /Users/jschaf/.dotfiles/emacs/funcs/abn-funcs-benchmark.el - evil-surround: /Users/jschaf/.emacs.d/elpa/evil-surround-20170910.1952/evil-surround.elc - misearch: /Applications/Emacs.app/Contents/Resources/lisp/misearch.elc - multi-isearch: nil - <many more>

5. Avoid Helper Functions to Set Up Modes

Often, Emacs packages will suggest running a helper function to set up keybindings. Here’s a few examples:

(evil-escape-mode)

(windmove-default-keybindings) ; Sets up keybindings.

(yas-global-mode 1) ; Complex snippet setup.

Rewrite these with use-package to improve startup speed. These helper functions are really just sneaky ways to trick you into eagerly loading packages before you need them.

As an example, here’s how to autoload evil-escape-mode .

( define-minor-mode evil-escape-mode ( if evil-escape-mode (add-hook 'pre-command-hook 'evil-escape-pre-command-hook) (remove-hook 'pre-command-hook 'evil-escape-pre-command-hook))) (evil-escape-mode) (use-package evil-escape :defer t :commands (evil-escape-pre-command-hook) :init (add-hook 'pre-command-hook 'evil-escape-pre-command-hook))

For a much trickier example, consider org-babel . The common recipe is:

(org-babel-do-load-languages 'org-babel-load-languages '((shell . t) (emacs-lisp . nil)))

This is bad because org-babel-do-load-languages is defined in org.el , which is over 24k lines of code and takes about 0.2 seconds to load. After examining the source code, org-babel-do-load-languages is simply requiring the ob-<lang> package like so:

( require (intern (concat "ob-" lang)))

In the ob-<lang>.el , there’s only two methods we care about, org-babel-execute:<lang> and org-babel-expand-body:<lang> . We can autoload the org-babel functionality instead of org-babel-do-load-languages like so:

(use-package ob-python :defer t :ensure org-plus-contrib :commands (org-babel-execute:python)) (use-package ob-shell :defer t :ensure org-plus-contrib :commands (org-babel-execute:sh org-babel-expand-body:sh org-babel-execute:bash org-babel-expand-body:bash))

6. Defer Packages you don’t need Immediately with Idle Timers

This saves about 0.4 seconds for the 9 packages I defer.

Some packages are useful and you want them available soon, but are not essential for immediate editing. These modes include:

recentf : Saves recent files.

: Saves recent files. saveplace : Saves point of visited files.

: Saves point of visited files. server : Starts Emacs daemon.

: Starts Emacs daemon. autorevert : Automatically reloads files that changed on disk.

: Automatically reloads files that changed on disk. paren : Highlight matching parenthesis.

: Highlight matching parenthesis. projectile : Project management tools.

: Project management tools. whitespace : Highlight trailing whitespace.

Instead of requiring these modes, load them after N seconds of idle time. I use 1 second for the more important packages and 2 seconds for everything else.

(use-package recentf :defer 1) (use-package uniquify :defer 2)

Optimizations that aren’t Worth It

Don’t bother byte-compiling your personal Emacs files. It saved about 0.05 seconds. Byte compiling causes difficult to debug errors when the source file gets out of sync with compiled file.