>From f94e7f15c77ff1d8b5736adc49bd1e9bd54c7270 Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Fri, 24 May 2024 13:12:28 -0400 Subject: [PATCH] In rgrep, check matching files before excluding files There are a lot of excluding globs, and checking them all is expensive. The files glob (i.e. the glob for files we actually want) is usually just one or two entries, so it's quite fast to check. If find checks the files glob first and then the excluding glob, it has to do much less checking (since the files glob will substantially narrow down the set of files on its own), and find performance is much better. In my benchmarking, this takes (rgrep "foo" "*.el" "~/src/emacs/trunk/") from ~410ms to ~130ms. * lisp/progmodes/grep.el (rgrep-default-command): Move the excluded files glob to part of the "files" argument. --- lisp/progmodes/grep.el | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index ce54c57aabc..84b3b352faa 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -1383,7 +1383,17 @@ rgrep-default-command (split-string files) (concat " -o " find-name-arg " ")) " " - (shell-quote-argument ")" grep-quoting-style)) + (shell-quote-argument ")" grep-quoting-style) + (when-let ((ignored-files (grep-find-ignored-files dir))) + (concat " " (shell-quote-argument "!" grep-quoting-style) + " " (shell-quote-argument "(" grep-quoting-style) + ;; we should use shell-quote-argument here + " -name " + (mapconcat + (lambda (ignore) (shell-quote-argument ignore grep-quoting-style)) + ignored-files + " -o -name ") + " " (shell-quote-argument ")" grep-quoting-style)))) dir (concat (when-let ((ignored-dirs (rgrep-find-ignored-directories dir))) @@ -1398,18 +1408,6 @@ rgrep-default-command " -o -path ") " " (shell-quote-argument ")" grep-quoting-style) - " -prune -o ")) - (when-let ((ignored-files (grep-find-ignored-files dir))) - (concat (shell-quote-argument "!" grep-quoting-style) " -type d " - (shell-quote-argument "(" grep-quoting-style) - ;; we should use shell-quote-argument here - " -name " - (mapconcat - (lambda (ignore) (shell-quote-argument ignore grep-quoting-style)) - ignored-files - " -o -name ") - " " - (shell-quote-argument ")" grep-quoting-style) " -prune -o "))))) (defun grep-find-toggle-abbreviation () -- 2.39.3