Subject: 30.1.90; Calling setopt during init loads cus-start over and over
From: Aaron Zeng <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Thu, 29 May 2025 16:57:22 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello,

While trying to improve Emacs startup time at my site, I noticed that
cus-start was being loaded repeatedly, each time setopt was called.
It seems that, when called during site-start or loading the user's
init file, setopt (which eventually calls custom-load-symbol) requires
cus-start, ignoring errors---and loading cus-start at that moment does
indeed error.

As a result of this ignored error, cus-start is loaded from scratch
each time (require 'cus-start) is evaluated.  This adds something like
12ms of startup time to Emacs batch commands at my site, since we have
about 25 setopt calls in site-start, and each one takes an extra 500us
or so.

I performed a quick experiment, with:

    emacs -Q --batch --init-directory=example-init --user ""

where example-init/init.el contains just:

    (dotimes (_ 25)
      (setopt make-backup-files nil))

Running the above command under hyperfine shows that the runtime is
about 82ms on my machine.

Prepending this code to example-init/init.el:

    (ignore-errors
      (require 'cus-start))
    (push 'cus-start features)

drops the runtime of the command to 75ms.  (I believe this experiment
might underestimate the true impact of repeatedly loading cus-start
due to caching effects.)

I believe this can be fixed with the following patch.  On my machine,
this patch reduces the runtime of the original init file to 75ms and
causes the prepended code to make no difference to the runtime.

diff --git a/lisp/cus-start.el b/lisp/cus-start.el
index 91cc6e22152..e82e97e87ca 100644
--- a/lisp/cus-start.el
+++ b/lisp/cus-start.el
@@ -934,7 +934,7 @@ minibuffer-prompt-properties--setter
       ;; This is used by describe-variable.
       (if version (put symbol 'custom-version version))
       ;; Don't re-add to custom-delayed-init-variables post-startup.
-      (unless after-init-time
+      (when (listp custom-delayed-init-variables)
 	;; Note this is the _only_ initialize property we handle.
 	(if (eq (cadr (memq :initialize rest)) #'custom-initialize-delay)
 	    ;; These vars are defined early and should hence be initialized

In GNU Emacs 30.1.90 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.15.12, Xaw scroll bars) of 2025-05-29 built on
 igm-qws-u12685a
Repository revision: 3e57c35323abe0782ba6a70adeecf99c27497e48
Repository branch: HEAD
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Rocky Linux 8.10 (Green Obsidian)

Configured using:
 'configure --config-cache --with-x-toolkit=lucid --without-gpm
 --without-gconf --without-selinux --without-imagemagick --with-modules
 --with-gif=no --with-cairo --with-rsvg --without-compress-install
 --with-tree-sitter --with-native-compilation=aot --prefix='

Configured features:
CAIRO DBUS FREETYPE GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG LIBSYSTEMD
LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP
SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER X11 XDBE XIM
XINPUT2 XPM LUCID ZLIB

Important settings:
  value of $LANG: en_US.utf8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date subr-x mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
cl-loaddefs cl-lib sendmail rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mail-utils rmc iso-transl tooltip cconv eldoc paren electric
uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
term/x-win x-win term/common-win x-dnd touch-screen tool-bar dnd fontset
image regexp-opt fringe tabulated-list replace newcomment text-mode
lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow isearch
easymenu timer select scroll-bar mouse jit-lock font-lock syntax
font-core term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads dbusbind inotify
dynamic-setting system-font-setting font-render-setting cairo x-toolkit
xinput2 x multi-tty move-toolbar make-network-process native-compile
emacs)

Memory information:
((conses 16 50339 9279) (symbols 48 5372 0) (strings 32 15703 2215)
 (string-bytes 1 493685) (vectors 16 9036)
 (vector-slots 8 126107 9764) (floats 8 24 2) (intervals 56 274 0)
 (buffers 992 10))