bug#81525: 32.0.50; [PATCH] Performance improvement for rgrep's find command

Phil Sainty <[email protected]> Fri, 31 Jul 2026 18:39:39 +1200
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
`rgrep' generates a find command which prunes the directories
listed in `grep-find-ignored-directories', and the way it
does that turns out to be relatively slow.

For the entry "SCSS" (for example), it uses the find syntax
"-path \*/SCCS".  We can also write that as "-name SCSS",
and (on my system) this change to the find command syntax
knocks about 30% off the run time.  That seems significant
enough that I think we should generate the -name syntax
whenever appropriate (which is true for all default entries
of the list).

I'm using the following to benchmark just the "find" command
without actually grepping, finding all files under my home
directory:

  (require 'grep)
  (grep-compute-defaults)

  (let ((grep-find-template
         "time find -H <D> <X> -type f <F> -print 2>/dev/null"))
    (shell-command (rgrep-default-command
                    "ignore" "no-such-file" "$HOME")))

On my system, repeated test runs take ~23 seconds for the
original command using the -path syntax, and ~16 seconds
for the patched version.

I've attached a patch to change how the find command is
built, retaining the original -path syntax whenever there
is a slash in the name, but using -name otherwise.


-Phil



In GNU Emacs 32.0.50 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo
  version 1.16.0, Xaw scroll bars) of 2026-06-14 built on phil-lp
Repository revision: 1c0d6ac8fa86034521eefabe6472867ee8abbadf
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 
11.0.12101004
System Description: Ubuntu 22.04.5 LTS

Configured using:
  'configure --prefix=/home/phil/emacs/trunk/usr/local
  --without-native-compilation --with-x-toolkit=lucid --without-sound
  '--program-transform-name=s/^ctags$/ctags_emacs/''

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LCMS2 LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP SQLITE3
THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER WEBP X11 XDBE XIM XINERAMA
XPM XRANDR LUCID ZLIB

Important settings:
   value of $LC_MONETARY: en_NZ.UTF-8
   value of $LC_NUMERIC: en_NZ.UTF-8
   value of $LC_TIME: en_NZ.UTF-8
   value of $LANG: en_GB.UTF-8
   value of $XMODIFIERS: @im=ibus
   locale-coding-system: utf-8
0001-Performance-improvement-for-rgrep.patch (text/x-diff, 1.8 KB)
From 41f0fb911ae1e1e4f954bce1c9631cc7fa93b5e0 Mon Sep 17 00:00:00 2001
From: Phil Sainty <[email protected]>
Date: Fri, 31 Jul 2026 12:45:37 +1200
Subject: [PATCH] Performance improvement for rgrep

* lisp/progmodes/grep.el (rgrep-default-command): When generating
the find command, use -name in preference to -path whenever possible.
---
 lisp/progmodes/grep.el | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index a5ba32d26e8..dcefc41f981 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -1532,13 +1532,17 @@ rgrep-default-command
       (when-let* ((ignored-dirs (rgrep-find-ignored-directories dir)))
         (concat "-type d "
                 (shell-quote-argument "(" grep-quoting-style)
-                ;; we should use shell-quote-argument here
-                " -path "
-                (mapconcat
-                 (lambda (d)
-                   (shell-quote-argument (concat "*/" d) grep-quoting-style))
-                 ignored-dirs
-                 " -o -path ")
+                " "
+                (string-join
+                 (cl-loop for dir in grep-find-ignored-directories
+                          collect (if (string-match-p "/" dir)
+                                      (concat "-path "
+                                              (shell-quote-argument
+                                               (concat "*/" dir) grep-quoting-style))
+                                    ;; -name is faster than -path
+                                    (concat "-name "
+                                            (shell-quote-argument dir grep-quoting-style))))
+                 " -o ")
                 " "
                 (shell-quote-argument ")" grep-quoting-style)
                 " -prune -o "))
-- 
2.34.1