unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#51420: 29.0.50; [PATCH] Be more efficient when checking for a matching client or frame in server.el
@ 2021-10-26 20:18 Jim Porter
  2021-10-26 20:22 ` Jim Porter
  0 siblings, 1 reply; 3+ messages in thread
From: Jim Porter @ 2021-10-26 20:18 UTC (permalink / raw)
  To: 51420

[-- Attachment #1: Type: text/plain, Size: 884 bytes --]

Attached is a patch to eliminate unnecessary checks for matching 
clients/frames in `server-handle-delete-frame' and 
`server-kill-emacs-query-function'.

Previously, `server-handle-delete-frame' would iterate over all frames 
to check if there were any other frames associated with the current 
client. Now, it uses `seq-some' to stop iterating once it finds the 
first such frame.

Similarly, `server-kill-emacs-query-function' used to iterate over all 
clients, and then all buffers for *each* client to check if there were 
any live buffers associated with any clients. Now, it uses a pair of 
calls to `seq-some' to stop iterating once it finds a live buffer from 
any client.

I haven't benchmarked these changes since I hope the strategy of 
stopping iteration as soon as we can is uncontroversial. However, if you 
think it's important, I can try to generate some benchmarks.

[-- Attachment #2: 0001-Be-more-efficient-when-checking-for-a-matching-clien.patch --]
[-- Type: text/plain, Size: 2307 bytes --]

From fdba88af916d8e47725da5aea52bb2ef45da09ef Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Tue, 26 Oct 2021 13:04:08 -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..478e59577e 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#51420: 29.0.50; [PATCH] Be more efficient when checking for a matching client or frame in server.el
  2021-10-26 20:18 bug#51420: 29.0.50; [PATCH] Be more efficient when checking for a matching client or frame in server.el Jim Porter
@ 2021-10-26 20:22 ` Jim Porter
  2021-10-27 13:55   ` Lars Ingebrigtsen
  0 siblings, 1 reply; 3+ messages in thread
From: Jim Porter @ 2021-10-26 20:22 UTC (permalink / raw)
  To: 51420

[-- Attachment #1: Type: text/plain, Size: 327 bytes --]

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. :)

[-- Attachment #2: 0001-Be-more-efficient-when-checking-for-a-matching-clien.patch --]
[-- Type: text/plain, Size: 2308 bytes --]

From faa09da0429fe521155ab011271cc92aa84600ef Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#51420: 29.0.50; [PATCH] Be more efficient when checking for a matching client or frame in server.el
  2021-10-26 20:22 ` Jim Porter
@ 2021-10-27 13:55   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2021-10-27 13:55 UTC (permalink / raw)
  To: Jim Porter; +Cc: 51420

Jim Porter <jporterbugs@gmail.com> writes:

> 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.

Thanks; applied to Emacs 29.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-10-27 13:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-26 20:18 bug#51420: 29.0.50; [PATCH] Be more efficient when checking for a matching client or frame in server.el Jim Porter
2021-10-26 20:22 ` Jim Porter
2021-10-27 13:55   ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).