Refactor comp-effective-async-max-jobs advice

We only need this magic in CLI sessions. It's better to only use half
the CPUs in interactive sessions (if the user has enabled
comp-deferred-compilation for some reason).

Fixes #5042
This commit is contained in:
Henrik Lissner 2021-05-12 14:50:59 -04:00
parent ce11a98b70
commit 6d2c6b44fa
3 changed files with 31 additions and 31 deletions

View file

@ -77,30 +77,31 @@
'face '(:height 1) 'face '(:height 1)
'display '(raise 0))) 'display '(raise 0)))
(defvar doom--system-cpus nil)
;;;###autoload ;;;###autoload
(defun doom-system-cpus () (defun doom-system-cpus ()
"Return the max number of processing units on this system. "Return the max number of processing units on this system.
Tries to be portable. Returns 1 if cannot be determined." Tries to be portable. Returns 1 if cannot be determined."
(or doom--system-cpus (or (get 'doom-system-cpus 'cached-value)
(setq doom--system-cpus (put 'doom-system-cpus 'cached-value
(let ((cpus (let ((cpus
(cond ((getenv "NUMBER_OF_PROCESSORS")) (cond ((eq 'windows-nt system-type)
((executable-find "nproc") (w32-get-nproc))
(doom-call-process "nproc")) ((getenv "NUMBER_OF_PROCESSORS"))
((executable-find "sysctl") ((executable-find "nproc")
(doom-call-process "sysctl" "-n" "hw.ncpu"))))) (doom-call-process "nproc"))
(max ((executable-find "sysctl")
1 (or (cl-typecase cpus (doom-call-process "sysctl" "-n" "hw.ncpu")))))
(string (max
(condition-case _ 1 (or (cl-typecase cpus
(string-to-number cpus) (string
(wrong-type-argument (condition-case _
(user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S" (string-to-number cpus)
cpus)))) (wrong-type-argument
(consp (user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S"
(if (zerop (car cpus)) cpus))))
(string-to-number (cdr cpus)) (consp
(user-error "Failed to look up number of processors, because:\n\n%s" (if (zerop (car cpus))
(cdr cpus))))) (string-to-number (cdr cpus))
1)))))) (user-error "Failed to look up number of processors, because:\n\n%s"
(cdr cpus)))))
1))))))

View file

@ -70,6 +70,12 @@ purpose.")
;; Stop user configuration from interfering with package management ;; Stop user configuration from interfering with package management
(setq enable-dir-local-variables nil) (setq enable-dir-local-variables nil)
;; Default to using all cores, rather than half of them, since we compile things
;; ahead-of-time in a non-interactive session.
(defadvice! doom--comp-use-all-cores-a (&rest _)
:before #'comp-effective-async-max-jobs
(setq comp-num-cpus (doom-system-cpus)))
;; ;;
;;; Entry point ;;; Entry point

View file

@ -189,14 +189,7 @@ users).")
(concat local-dir-re ".*/evil-collection-vterm\\.el\\'") (concat local-dir-re ".*/evil-collection-vterm\\.el\\'")
(concat local-dir-re ".*/with-editor\\.el\\'") (concat local-dir-re ".*/with-editor\\.el\\'")
;; https://github.com/nnicandro/emacs-jupyter/issues/297 ;; https://github.com/nnicandro/emacs-jupyter/issues/297
(concat local-dir-re ".*/jupyter-channel\\.el\\'")))) (concat local-dir-re ".*/jupyter-channel\\.el\\'")))))
;; Default to using all cores, rather than half of them, since we compile
;; things ahead-of-time in a non-interactive session.
(defun doom--comp-use-all-cores-a ()
(if (zerop native-comp-async-jobs-number)
(setq comp-num-cpus (doom-system-cpus))
native-comp-async-jobs-number))
(advice-add #'comp-effective-async-max-jobs :override #'doom--comp-use-all-cores-a))
;; ;;