emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] scratch/project-files-pipe-grep c708231 3/3: Try to avoid


From: Dmitry Gutov
Subject: [Emacs-diffs] scratch/project-files-pipe-grep c708231 3/3: Try to avoid writing a temporary file
Date: Thu, 27 Dec 2018 19:30:53 -0500 (EST)

branch: scratch/project-files-pipe-grep
commit c708231803712bd37154c140afdfd8468cac603e
Author: Dmitry Gutov <address@hidden>
Commit: Dmitry Gutov <address@hidden>

    Try to avoid writing a temporary file
    
    The result is actually slower (by ~10% in my experiment).
---
 lisp/progmodes/project.el | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index c7f8e99..3ed1cea 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -365,34 +365,41 @@ pattern to search for."
 (defun project-files-pipe-grep (regexp)
   (pcase-let*
       ((files (project-files (project-current t)))
-       (infile (make-temp-file "pftg"))
        (output (get-buffer-create " *project grep output*"))
        (`(,grep-re ,file-group ,line-group . ,_) (car grep-regexp-alist))
-       (status nil)
+       (command `("xargs" "-0" "grep"
+                  ,@(when (and case-fold-search
+                              (isearch-no-upper-case-p regexp t))
+                      (list "-i"))
+                  "-nHe"
+                  ,(shell-quote-argument (xref--regexp-to-extended regexp))))
        (hits nil)
-       (xrefs nil))
-    (with-temp-buffer
-      (insert (mapconcat #'identity files "\0"))
-      ;; FIXME: Try without a temporary file.
-      (write-region (point-min) (point-max) infile))
+       (xrefs nil)
+       (process nil)
+       (process-connection-type nil))
     (with-current-buffer output
       (erase-buffer)
-      (setq status
-            (process-file-shell-command
-             (format "xargs -0 -P 1 grep %s -nHe %s"
-                     (if (and case-fold-search
-                              (isearch-no-upper-case-p regexp t))
-                         "-i"
-                       "")
-                     (shell-quote-argument (xref--regexp-to-extended regexp)))
-             infile
-             t))
+      (setq process
+            (apply
+             #'start-process
+             "project-files-pipe-to-grep"
+             output
+             command))
+      (dolist (f files)
+        (process-send-string process f)
+        (process-send-string process "\0"))
+      (process-send-eof process)
+      (with-local-quit
+        (while (process-live-p process)
+          (accept-process-output process 1)))
+      (when quit-flag
+        (delete-process process))
       (goto-char (point-min))
       (when (and (/= (point-min) (point-max))
                  (not (looking-at grep-re))
                  ;; TODO: Show these matches as well somehow?
                  (not (looking-at "Binary file .* matches")))
-        (user-error "Search failed with status %d: %s" status
+        (user-error "Search failed with status %d: %s" (process-exit-status 
process)
                     (buffer-substring (point-min) (line-end-position))))
       (while (re-search-forward grep-re nil t)
         (push (list (string-to-number (match-string line-group))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]