Subject: Re: bug#59935: 29.0.60; project-list-buffers is slow

From: Juri Linkov <[email-masked]>
Date: Sun, 11 Dec 2022 19:07:27 +0200

> Do you have the time to finish the fix yourself? It was your feature, after
> all.

Here is a complete patch tested for all possible cases:
- emacs 28/29
- with/without file arg
- with/without reverting

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index 38d4fdad5fc..64af86558e4 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1321,18 +1321,31 @@ project-list-buffers
 start with a space (which are for internal use).  With prefix argument
 ARG, show only buffers that are visiting files."
   (interactive "P")
-  (let ((pr (project-current t)))
+  (let* ((pr (project-current t))
+         (buffer-list-function
+          (lambda ()
+            (seq-filter
+             (lambda (buffer)
+               (let ((name (buffer-name buffer))
+                     (file (buffer-file-name buffer)))
+                 (and (or (not (string= (substring name 0 1) " "))
+                          file)
+                      (not (eq buffer (current-buffer)))
+                      (or file (not Buffer-menu-files-only)))))
+             (project-buffers pr)))))
     (display-buffer
      (if (version< emacs-version "29.0.50")
-         (let ((buf (list-buffers-noselect arg (project-buffers pr))))
+         (let ((buf (list-buffers-noselect
+                     arg (let ((Buffer-menu-files-only arg))
+                           (funcall buffer-list-function)))))
            (with-current-buffer buf
              (setq-local revert-buffer-function
                          (lambda (&rest _ignored)
-                           (list-buffers--refresh (project-buffers pr))
+                           (list-buffers--refresh
+                            (funcall buffer-list-function))
                            (tabulated-list-print t))))
            buf)
-       (list-buffers-noselect
-        arg nil (lambda (buf) (memq buf (project-buffers pr))))))))
+       (list-buffers-noselect arg buffer-list-function)))))
 
 (defcustom project-kill-buffer-conditions
   '(buffer-file-name    ; All file-visiting buffers are included.
diff --git a/lisp/buff-menu.el b/lisp/buff-menu.el
index 588fe599a46..cabf7cb8fc4 100644
--- a/lisp/buff-menu.el
+++ b/lisp/buff-menu.el
@@ -100,12 +100,8 @@ Buffer-menu-files-only
 This is set by the prefix argument to `buffer-menu' and related
 commands.")
 
-(defvar-local Buffer-menu-filter-predicate nil
-  "Function to filter out buffers in the buffer list.
-Buffers that don't satisfy the predicate will be skipped.
-The value should be a function of one argument; it will be
-called with the buffer.  If this function returns non-nil,
-then the buffer will be displayed in the buffer list.")
+(defvar-local Buffer-menu-buffer-list-function nil
+  "Function to return buffers for the buffer list.")
 
 (defvar-keymap Buffer-menu-mode-map
   :doc "Local keymap for `Buffer-menu-mode' buffers."
@@ -623,23 +619,23 @@ Buffer-menu-view-other-window
 ;;; Functions for populating the Buffer Menu.
 
 ;;;###autoload
-(defun list-buffers-noselect (&optional files-only buffer-list filter-predicate)
+(defun list-buffers-noselect (&optional files-only buffer-list)
   "Create and return a Buffer Menu buffer.
 This is called by `buffer-menu' and others as a subroutine.
 
 If FILES-ONLY is non-nil, show only file-visiting buffers.
-If BUFFER-LIST is non-nil, it should be a list of buffers; it
-means list those buffers and no others.
-If FILTER-PREDICATE is non-nil, it should be a function
-that filters out buffers from the list of buffers.
-See more at `Buffer-menu-filter-predicate'."
+If BUFFER-LIST is non-nil, it should be either a list of buffers
+or a function that returns a list of buffers; it means
+list those buffers and no others.
+See more at `Buffer-menu-buffer-list-function'."
   (let ((old-buffer (current-buffer))
 	(buffer (get-buffer-create "*Buffer List*")))
     (with-current-buffer buffer
       (Buffer-menu-mode)
       (setq Buffer-menu-files-only
 	    (and files-only (>= (prefix-numeric-value files-only) 0)))
-      (setq Buffer-menu-filter-predicate filter-predicate)
+      (when (functionp buffer-list)
+        (setq Buffer-menu-buffer-list-function buffer-list))
       (list-buffers--refresh buffer-list old-buffer)
       (tabulated-list-print))
     buffer))
@@ -661,13 +657,17 @@ list-buffers--refresh
         (marked-buffers (Buffer-menu-marked-buffers))
         (buffer-menu-buffer (current-buffer))
 	(show-non-file (not Buffer-menu-files-only))
-	(filter-predicate (and (functionp Buffer-menu-filter-predicate)
-			       Buffer-menu-filter-predicate))
 	entries name-width)
     ;; Collect info for each buffer we're interested in.
-    (dolist (buffer (or buffer-list
-			(buffer-list (if Buffer-menu-use-frame-buffer-list
-					 (selected-frame)))))
+    (dolist (buffer (cond
+                     ((functionp buffer-list)
+                      (funcall buffer-list))
+                     (buffer-list)
+                     (Buffer-menu-buffer-list-function
+                      (funcall Buffer-menu-buffer-list-function))
+                     (t (buffer-list
+                         (if Buffer-menu-use-frame-buffer-list
+                             (selected-frame))))))
       (with-current-buffer buffer
 	(let* ((name (buffer-name))
 	       (file buffer-file-name))
@@ -676,9 +676,7 @@ list-buffers--refresh
 			 (and (or (not (string= (substring name 0 1) " "))
                                   file)
 			      (not (eq buffer buffer-menu-buffer))
-			      (or file show-non-file)
-			      (or (not filter-predicate)
-				  (funcall filter-predicate buffer)))))
+			      (or file show-non-file))))
 	    (push (list buffer
 			(vector (cond
                                  ((eq buffer old-buffer) ".")