From e69ed598a66f4b400c809c56e6f6b07155ae643d Mon Sep 17 00:00:00 2001 From: James Cherti <60946298+jamescherti@users.noreply.github.com> Date: Wed, 27 Aug 2025 23:29:35 -0400 Subject: [PATCH] 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 --- README.md | 2 +- early-init.el | 39 +++++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b26b995..a4b4ea2 100644 --- a/README.md +++ b/README.md @@ -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.
diff --git a/early-init.el b/early-init.el
index c120546..86a9960 100644
--- a/early-init.el
+++ b/early-init.el
@@ -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