docs(Emacs basic conf): better garbage collection
deploy / build (push) Successful in 2m22s Details

This commit is contained in:
Lucien Cartier-Tilet 2024-02-07 06:35:42 +01:00
parent 2ef0f4fc89
commit 610046ce96
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
1 changed files with 46 additions and 1 deletions

View File

@ -25,9 +25,54 @@ loaded, speeding Emacs up a bit.
(set-fringe-mode 10) ; give some breathing room
(menu-bar-mode -1) ; disable menubar
(blink-cursor-mode 0) ; disable blinking cursor
(setq gc-cons-threshold (* 1024 1024 1024))
#+end_src
*** Better garbage collection
Emacs sometimes freezes up a bit when doing some garbage collection,
which is not super. So, in order to fix that, I do two things:
1. Set up a really high threshold (I have a lot of RAM to spare, so I
dont really care),
2. Hook garbage collection thirty seconds after Emacs loses focus and
every thirty seconds after that,
3. Cancel garbage collection if Emacs gains focus back within this
thirty seconds window.
With a garbage collection threshold, Emacs should never garbage
collect on its own, and Emacs is free to freeze up for a few seconds
when it loses focus since Im looking away.
#+begin_src emacs-lisp
(setq garbage-collection-messages t ;; tell me when garbage collecting
gc-cons-threshold (* 16 1024 1024 1024)) ;; 16GiB of RAM
(defmacro my/time (&rest body)
`(let ((time (current-time)))
,@body
(float-time (time-since time))))
(defun my/garbage-collect ()
"Garbage collect and tell the user how much time it took."
(message "Garbage collector ran for %.06fs"
(my/time (garbage-collect))))
(defvar my/gc-timer nil
"Timer for garbage collection. See
`my/garbage-collect-on-focus-lost'.")
(defun my/garbage-collect-on-focus-lost ()
"Garbage collect when Emacs loses focus.
Garbage collection is only triggered thirty seconds after losing
focus, and only once."
(if (frame-focus-state
(cancel-timer my/gc-timer))
(setq my/gc-timer (run-with-idle-timer 30 nil #'my/garbage-collect))))
(add-function :after after-focus-change-function #'my/garbage-collect-on-focus-lost)
#+end_src
To write this, I mostly based myself on [[https://news.ycombinator.com/item?id=39190110][this HackerNews thread]] and [[https://akrl.sdf.org/#orgc15a10d][its
related article]].
** Emacs Behavior
*** Editing Text in Emacs
I *never* want to keep trailing spaces in my files, which is why Im