From: Sean Whitton Date: Fri, 2 May 2025 04:04:26 +0000 (+0800) Subject: New user option native-comp-async-on-battery-power X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=e15fc92ab239f9249eaa0769878707a47cbba104;p=emacs.git New user option native-comp-async-on-battery-power * lisp/emacs-lisp/comp-run.el (native-comp-async-on-battery-power): New option. (battery-status-function): Declare. (native--compile-skip-on-battery-p): New function. (comp--run-async-workers): Call it. * etc/NEWS: Announce the new option. (cherry picked from commit 6b11687555cdd1d6896bad5eb09af217b28d6076) --- diff --git a/lisp/emacs-lisp/comp-run.el b/lisp/emacs-lisp/comp-run.el index 3dbf24a8be7..89fd8cf68a9 100644 --- a/lisp/emacs-lisp/comp-run.el +++ b/lisp/emacs-lisp/comp-run.el @@ -54,6 +54,23 @@ or one if there's just one execution unit." :risky t :version "28.1") +;; If we could start compilations that were skipped if and when AC power +;; is subsequently reconnected, we could consider changing the default +;; to nil. --spwhitton +(defcustom native-comp-async-on-battery-power t + "Whether to start asynchronous native compilation while on battery power. +Customize this to nil to disable starting async compilations when AC +power is not connected. Async compilations that are already running +when AC power is disconnected are not affected. Compilations skipped +because AC power was disconnected are not started again if AC power is +subsequently reconnected; in most cases, those compilations will not be +tried again until after you next restart Emacs. + +Customizing this to nil has no effect unless `battery-status-function' +can correctly detect the absence of connected AC power on your platform." + :type 'boolean + :version "31.1") + (defcustom native-comp-async-report-warnings-errors t "Whether to report warnings and errors from asynchronous native compilation. @@ -158,6 +175,16 @@ LOAD and SELECTOR work as described in `native--compile-async'." (string-match-p re file)) native-comp-jit-compilation-deny-list)))) +(defvar battery-status-function) + +(defun native--compile-skip-on-battery-p () + "Should we skip JIT compilation because we're running on battery power?" + (and-let* (((not native-comp-async-on-battery-power)) + ((require 'battery)) + battery-status-function + (status (assq ?L (funcall battery-status-function))) + ((not (string= (cdr status) "on-line")))))) + (defvar comp-files-queue () "List of Emacs Lisp files to be compiled.") @@ -221,7 +248,8 @@ display a message." (cl-assert (null comp-no-spawn)) (if (or comp-files-queue (> (comp--async-runnings) 0)) - (unless (>= (comp--async-runnings) (comp--effective-async-max-jobs)) + (unless (or (>= (comp--async-runnings) (comp--effective-async-max-jobs)) + (native--compile-skip-on-battery-p)) (cl-loop for (source-file . load) = (pop comp-files-queue) while source-file