Subject: bug#79021: 31.0.50; Unnecessary GC in after-load-functions

From: Helmut Eller <[email-masked]>
Date: Fri, 12 Sep 2025 09:26:02 +0200

On Tue, Jul 15 2025, Stefan Monnier wrote:

>>> In loadup.el there is this:
>>> 
>>>   (add-hook 'after-load-functions (lambda (_) (garbage-collect)))
>>> 
>>> What's this for?  It seems unnecessary.
>>> 
>>> It would be nice to remove this, because without it the time to execute
>>> loadup.el goes down from 13 seconds to 6.
>>
>> I presume this is because we don't want to dump any garbage.
>> Stefan?
>
> AFAIK it was added to keep the dumped Emacs' heap "dense": if we run the
> GC only "in the normal way", the heap objects created during `loadup.el`
> end up interspersed with more free spaces.
>
> It's possible/likely that the portable dumper has made that unnecessary.

Should I commit this?

diff --git a/lisp/loadup.el b/lisp/loadup.el
index 18f09878f98..05bd468b713 100644
--- a/lisp/loadup.el
+++ b/lisp/loadup.el
@@ -127,7 +127,11 @@
 
 ;; Do it after subr, since both after-load-functions and add-hook are
 ;; implemented in subr.el.
-(add-hook 'after-load-functions (lambda (_) (garbage-collect)))
+;;
+;; This was probably done to reduce fragmentation with unexec.  With the
+;; pdumper it makes little sense.  See bug#79021.
+(if (not (fboundp 'dump-emacs-portable))
+    (add-hook 'after-load-functions (lambda (_) (garbage-collect))))
 
 (load "version")