Skip to content

Commit

Permalink
Add show notes and link for Emacs From Scratch Ep 12
Browse files Browse the repository at this point in the history
  • Loading branch information
daviwil committed Feb 15, 2021
1 parent 492f951 commit 65a994b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Here is a list of all the episode videos with links to the configuration we buil
9. [[https://youtu.be/wa_wZIuT9Vw][Learn to Love the Terminal Modes]] ([[https://github.com/daviwil/emacs-from-scratch/tree/bbfbc77b3afab0c14149e07d0ab08d275d4ba575][Code]], [[file:show-notes/Emacs-09.org][Notes]])
10. [[https://youtu.be/PMWwM8QJAtU][Effortless File Management with Dired]] ([[https://github.com/daviwil/emacs-from-scratch/blob/8c302a79bf5700f6ef0279a3daeeb4123ae8bd59/Emacs.org#dired][Code]], [[file:show-notes/Emacs-10.org][Notes]])
11. [[https://youtu.be/dtjq68F2dXw][Keeping Your Emacs Packages Up to Date]] ([[https://github.com/daviwil/emacs-from-scratch/blob/4e921ccbe603d5fdd9c7f16c2418ac7322c8ab71/Emacs.org#automatic-package-updates][Code]], [[file:show-notes/Emacs-Scratch-11.org][Notes]])
12. [[https://youtu.be/9i_9hse_Y08][How to Cut Emacs Start Up Time in Half!]] (Code, [[file:show-notes/Emacs-Scratch-12.org][Notes]])

** [[https://www.youtube.com/watch?v=RQK_DaaX34Q&list=PLEoMzSkcN8oPQtn7FQEF3D7sroZbXuPZ7][Learning Emacs Lisp]]

Expand Down
95 changes: 95 additions & 0 deletions show-notes/Emacs-Scratch-12.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#+title: Making Emacs Start Faster

* Why is Emacs so slow to start up?

Is it actually slow with no config? Run =emacs -Q= to find out!

So why is it so much slower with our configuration?

* Let's find out how long it's taking!

Add a function to =emacs-startup-hook= to print out the duration of Emacs startup:

#+begin_src emacs-lisp

(defun efs/display-startup-time ()
(message "Emacs loaded in %s with %d garbage collections."
(format "%.2f seconds"
(float-time
(time-subtract after-init-time before-init-time)))
gcs-done))

(add-hook 'emacs-startup-hook #'efs/display-startup-time)

#+end_src

All startup behavior is happening in the =normal-top-level= function!

A helpful manual page is [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html][Summary: Sequence of Actions at Startup]].

* The most important tip: don't load any packages!

=use-package= gives you a few different ways to defer package loading:

- =:hook= - Package will be loaded the first time one of the hooks is invoked
- =:bind= - Package will be loaded the first time one of the key bindings is used
- =:commands= - Package will be loaded when one of the commands are used
- =:mode= - Package will be loaded the first time a file with a particular extension is opened
- =:after= - Load this package after other specific packages are loaded
- =:defer= - If you don't use any of the other options, this one will defer loading until after startup

There are a [[https://github.com/jwiegley/use-package#getting-started][few other options]] =use-package= provides, but these are all the most likely ones you would use.

The strategy is to look at all of your =use-package= expressions and decide whether it *really* needs to be loaded immediately at startup!

If you want to make sure a package gets loaded at startup despite the use of any of the options above, use =:demand t=.

Let's try it!

#+begin_src emacs-lisp

"fde" '(lambda () (interactive) (find-file (expand-file-name "~/.emacs.d/Emacs.org")))))

#+end_src

* with-eval-after-load can be useful!

=with-eval-after-load= is a macro that causes a section of code to be executed only after a particular package gets loaded.

#+begin_src emacs-lisp

(with-eval-after-load 'org
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(python . t)))

(push '("conf-unix" . conf-unix) org-src-lang-modes))

#+end_src

If you need to split up your configuration into multiple sections, this is one way to ensure you don't accidentally cause a package to load too early!

* Tweaking the garbage collector

One other common performance trick is to reduce the number of times the garbage collector will run during the startup process.

Set the =gc-cons-threshold= high at the beginning of your =init.el=:

#+begin_src emacs-lisp

;; The default is 800 kilobytes. Measured in bytes.
(setq gc-cons-threshold (* 50 1000 1000))

#+end_src

Then bring it back down at the end of your =init.el=:

#+begin_src emacs-lisp

;; Make gc pauses faster by decreasing the threshold.
(setq gc-cons-threshold (* 2 1000 1000))

#+end_src

Another option is to use this package: https://gitlab.com/koral/gcmh/

0 comments on commit 65a994b

Please sign in to comment.