perf(cli): doom: reduce init time

Even `emacs --batch -f kill-emacs` can take a non-trivial amount of time
to start up (0.661s on my system). This makes the two `emacs` calls in
bin/doom's shebang amount to >1s of startup time for Doom scripts (~1.5s
on my system). This change removes the second call (or at least defers
it until the after-script; at least, if $TMPDIR or $TEMP aren't set),
bringing down that time to a more bearable 0.9s.

This sacrifices the more descriptive "Can't find Emacs in your $PATH"
error message should the user set their own $EMACS, but the alternative
bash error is good enough:

  $ EMACS=foo doom version
  /home/$USER/.config/emacs/bin/doom: line 12: foo: command not found

I'm not doing more sophisticated checks on $EMACS, because it could be a
command (like `flatpak run org.gnu.emacs`), rather than an executable or
path, and the boilerplate to handle that, within what small space we get
in bin/doom's shebang, would be too much maintenance headache just for a
slightly better error message.
This commit is contained in:
Henrik Lissner 2024-09-09 23:40:34 -04:00
parent 98e28d801e
commit 498966179f
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -1,16 +1,16 @@
#!/usr/bin/env sh
:; # -*- mode: emacs-lisp; lexical-binding: t -*-
:; case "$EMACS" in *term*) EMACS=emacs ;; *) EMACS="${EMACS:-emacs}" ;; esac
:; [ "$EMACS" = emacs ] && { type emacs >/dev/null 2>&1 || err=1; }
:; [ -n "$err" ] && { echo "Error: failed to run Emacs with command '$EMACS'"; echo; echo "Are you sure Emacs is installed and in your \$PATH?"; exit 1; } >&2
:; emacs="$EMACS ${DEBUG:+--debug-init} -q --no-site-file --batch"
:; tmpdir=`$emacs -Q --eval '(princ (temporary-file-directory))' 2>/dev/null`
:; [ -z "$tmpdir" ] && { >&2 echo "Error: failed to run Emacs with command '$EMACS'"; >&2 echo; >&2 echo "Are you sure Emacs is installed and in your \$PATH?"; exit 1; }
:; export __DOOMPID="${__DOOMPID:-$$}"
:; export __DOOMSTEP="${__DOOMSTEP:-0}"
:; export __DOOMGEOM="${__DOOMGEOM:-`tput cols 2>/dev/null`x`tput lines 2>/dev/null`}"
:; export __DOOMGPIPE=${__DOOMGPIPE:-$__DOOMPIPE}
:; export __DOOMPIPE=; [ -t 0 ] || __DOOMPIPE="${__DOOMPIPE}0"; [ -t 1 ] || __DOOMPIPE="${__DOOMPIPE}1"
:; $emacs --load "$0" -- "$@" || exit=$?
:; [ "${exit:-0}" -eq 254 ] && { sh "${tmpdir}/doom.${__DOOMPID}.${__DOOMSTEP}.sh" "$0" "$@" && true; exit="$?"; }
:; [ "${exit:-0}" -eq 254 ] && { export TMPDIR="${TMPDIR:-${TEMP:-`$emacs -Q --eval '(princ (temporary-file-directory))' 2>/dev/null`}}"; sh "${TMPDIR}/doom.${__DOOMPID}.${__DOOMSTEP}.sh" "$0" "$@" && true; exit="$?"; }
:; exit $exit
;; This magical mess of a shebang is necessary for any script that relies on