Subject: bug#51420: 29.0.50; [PATCH] Be more efficient when checking for a matching client or frame in server.el

From: Jim Porter <[email-masked]>
Date: Tue, 26 Oct 2021 13:22:16 -0700

On 10/26/2021 1:18 PM, Jim Porter wrote:
> Attached is a patch to eliminate unnecessary checks for matching 
> clients/frames in `server-handle-delete-frame' and 
> `server-kill-emacs-query-function'.

Oops, forgot to commit a typo fix before exporting the patch. Attached 
is the fixed version that actually byte-compiles. :)
From faa09da0429fe521155ab011271cc92aa84600ef Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Tue, 26 Oct 2021 13:16:51 -0700
Subject: [PATCH] Be more efficient when checking for a matching client or
 frame in server.el

lisp/server.el (server-handle-delete-frame): Use 'seq-some' to
determine if another frame for the current client exists.
(server-kill-emacs-query-function): Use 'seq-some' to determine if
another live client exists.
---
 lisp/server.el | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/lisp/server.el b/lisp/server.el
index 5306a54776..d998656237 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -485,11 +485,11 @@ server-handle-delete-frame
     (when (and (frame-live-p frame)
 	       proc
 	       ;; See if this is the last frame for this client.
-	       (>= 1 (let ((frame-num 0))
-		       (dolist (f (frame-list))
-			 (when (eq proc (frame-parameter f 'client))
-			   (setq frame-num (1+ frame-num))))
-		       frame-num)))
+               (not (seq-some
+                     (lambda (f)
+                       (and (not (eq frame f))
+                            (eq proc (frame-parameter f 'client))))
+                     (frame-list))))
       (server-log (format "server-handle-delete-frame, frame %s" frame) proc)
       (server-delete-client proc 'noframe)))) ; Let delete-frame delete the frame later.
 
@@ -1580,13 +1580,13 @@ server-done
     (server-buffer-done (current-buffer))))
 
 (defun server-kill-emacs-query-function ()
-  "Ask before exiting Emacs if it has live clients."
-  (or (not (let (live-client)
-             (dolist (proc server-clients)
-               (when (memq t (mapcar #'buffer-live-p
-                                     (process-get proc 'buffers)))
-                 (setq live-client t)))
-             live-client))
+  "Ask before exiting Emacs if it has live clients.
+A \"live client\" is a client with at least one live buffer
+associated with it."
+  (or (not (seq-some (lambda (proc)
+                       (seq-some #'buffer-live-p
+                                 (process-get proc 'buffers)))
+                     server-clients))
       (yes-or-no-p "This Emacs session has clients; exit anyway? ")))
 
 (defun server-kill-buffer ()
-- 
2.25.1