Enhance garbage collection

- Enhance early-init garbage collection optimization
  (Add support for: gc-cons-percentage)
- Make minimal-emacs-load-user-init display messages when
  init-file-debug is non-nil
- Update README.md
This commit is contained in:
James Cherti
2025-08-27 23:29:35 -04:00
parent 8f5b2030fd
commit e69ed598a6
2 changed files with 26 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ The **minimal-emacs.d** project is a lightweight and optimized Emacs base (`init
Building the *minimal-emacs.d* `init.el` and `early-init.el` was the result of **extensive research and testing** to fine-tune the best parameters and optimizations for an Emacs configuration. *(More information about the *minimal-emacs.d* features can be found here: [Features](#features).)*
If this enhances your workflow, please show your support by **⭐ starring minimal-emacs.d GitHub** to help more users discover its benefits.
If this enhances your workflow, please show your support by **⭐ starring minimal-emacs.d GitHub** to help more Emacs users discover its benefits.
<p align="center">
<img src="https://jamescherti.com/misc/minimal-emacs.d.png" width="50%" />

View File

@@ -24,10 +24,14 @@
;;; Internal variables
(defvar minimal-emacs--backup-gc-cons-threshold gc-cons-threshold
"Backup of the original value of `gc-cons-threshold' before startup.")
;; Backup of `gc-cons-threshold' and `gc-cons-percentage' before startup.
(defvar minimal-emacs--backup-gc-cons-threshold gc-cons-threshold)
(defvar minimal-emacs--backup-gc-cons-percentage gc-cons-percentage)
(setq gc-cons-threshold most-positive-fixnum)
;; Temporarily raise the garbage collection threshold to its maximum value.
;; It will be restored later to controlled values.
(setq gc-cons-threshold (- most-positive-fixnum 1))
(setq gc-cons-percentage 1.0)
;;; Variables
@@ -55,6 +59,10 @@ stored in `minimal-emacs-gc-cons-threshold'.")
"Value to which `gc-cons-threshold' is set after Emacs startup.
Ignored if `minimal-emacs-optimize-startup-gc' is nil.")
(defvar minimal-emacs-gc-cons-percentage gc-cons-percentage
"Value to which `gc-cons-percentage' is set after Emacs startup.
Ignored if `minimal-emacs-optimize-startup-gc' is nil.")
(defvar minimal-emacs-gc-cons-threshold-restore-delay nil
"Number of seconds to wait before restoring `gc-cons-threshold'.")
@@ -157,23 +165,26 @@ pre-early-init.el, and post-early-init.el.")
(setq garbage-collection-messages minimal-emacs-debug)
(defun minimal-emacs--restore-gc-cons-threshold ()
"Restore `gc-cons-threshold' to `minimal-emacs-gc-cons-threshold'."
(defun minimal-emacs--restore-gc-values ()
"Restore garbage collection values to minimal-emacs.d values."
(setq gc-cons-threshold minimal-emacs-gc-cons-threshold)
(setq gc-cons-percentage minimal-emacs-gc-cons-percentage))
(defun minimal-emacs--restore-gc ()
"Restore garbage collection settings."
(if (bound-and-true-p minimal-emacs-gc-cons-threshold-restore-delay)
;; Defer garbage collection during initialization to avoid 2 collections.
(run-at-time
minimal-emacs-gc-cons-threshold-restore-delay nil
(lambda () (setq gc-cons-threshold minimal-emacs-gc-cons-threshold)))
(setq gc-cons-threshold minimal-emacs-gc-cons-threshold)))
(run-at-time minimal-emacs-gc-cons-threshold-restore-delay nil
#'minimal-emacs--restore-gc-values)
(minimal-emacs--restore-gc-values)))
(if minimal-emacs-optimize-startup-gc
;; `gc-cons-threshold' is managed by minimal-emacs.d
(add-hook 'emacs-startup-hook #'minimal-emacs--restore-gc-cons-threshold 105)
(add-hook 'emacs-startup-hook #'minimal-emacs--restore-gc 105)
;; gc-cons-threshold is not managed by minimal-emacs.d.
;; If it is equal to `most-positive-fixnum', this indicates that the user has
;; not overridden the value in their `pre-early-init.el' configuration.
(when (= gc-cons-threshold most-positive-fixnum)
(setq gc-cons-threshold minimal-emacs--backup-gc-cons-threshold)))
(when (= gc-cons-threshold (- most-positive-fixnum 1))
(setq gc-cons-threshold minimal-emacs--backup-gc-cons-threshold)
(setq gc-cons-percentage minimal-emacs--backup-gc-cons-percentage)))
;;; Native compilation and Byte compilation