1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-21 12:03:55 -08:00

Performance improvement of 'python-shell-get-process'

'python-shell-get-process' is frequently called from
'python-eldoc--get-doc-at-point' and etc., invoking
'project-current' unless there is a buffer-specific Inferior
Python process.  When the buffer is a remote buffer not
belonging to any project and has significant latency,
'project-current' may take a long time.  To avoid this,
implement a process cache in 'python-shell-get-process'.

* lisp/progmodes/python.el (python-shell--process-cache)
(python-shell--process-cache-valid): New variables.
(python-shell--invalidate-process-cache): New function.
(python-shell-make-comint): Add a call to the above function.
(python-shell-get-process): Add process cache.  (Bug#80045)
This commit is contained in:
kobarity 2026-01-04 22:50:47 +09:00 committed by Eli Zaretskii
parent 83f4e48106
commit 83b4f1ba26

View file

@ -3816,6 +3816,16 @@ variable.
(compilation-shell-minor-mode 1)
(python-pdbtrack-setup-tracking))
(defvar-local python-shell--process-cache)
(defvar-local python-shell--process-cache-valid)
(defun python-shell--invalidate-process-cache ()
"Invalidate process cache."
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(setq python-shell--process-cache nil
python-shell--process-cache-valid nil))))
(defun python-shell-make-comint (cmd proc-name &optional show internal)
"Create a Python shell comint buffer.
CMD is the Python command to be executed and PROC-NAME is the
@ -3832,6 +3842,7 @@ killed."
(let* ((proc-buffer-name
(format (if (not internal) "*%s*" " *%s*") proc-name)))
(when (not (comint-check-proc proc-buffer-name))
(python-shell--invalidate-process-cache)
(let* ((cmdlist (split-string-and-unquote cmd))
(interpreter (car cmdlist))
(args (cdr cmdlist))
@ -3955,7 +3966,15 @@ If current buffer is in `inferior-python-mode', return it."
(defun python-shell-get-process ()
"Return inferior Python process for current buffer."
(get-buffer-process (python-shell-get-buffer)))
(unless (and python-shell--process-cache-valid
(or (not python-shell--process-cache)
(and (process-live-p python-shell--process-cache)
(buffer-live-p
(process-buffer python-shell--process-cache)))))
(setq python-shell--process-cache
(get-buffer-process (python-shell-get-buffer))
python-shell--process-cache-valid t))
python-shell--process-cache)
(defun python-shell-get-process-or-error (&optional interactivep)
"Return inferior Python process for current buffer or signal error.