From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Dmitry Gutov Newsgroups: gmane.emacs.devel Subject: Re: project-find-regexp using ripgrep Date: Mon, 22 Jun 2020 03:01:42 +0300 Message-ID: <15b1e532-99e7-5f43-defc-3c32072af3d6@yandex.ru> References: <49f66d46-da8d-9658-ec85-ced39a99ad87@yandex.ru> <86tuz69y51.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------F0C43CFB5748594229E4A589" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="109886"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 Cc: emacs-devel To: "andres.ramirez" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Jun 22 02:02:26 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jn9ur-000SXL-Ck for ged-emacs-devel@m.gmane-mx.org; Mon, 22 Jun 2020 02:02:25 +0200 Original-Received: from localhost ([::1]:44774 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jn9uq-0005iD-EQ for ged-emacs-devel@m.gmane-mx.org; Sun, 21 Jun 2020 20:02:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33778) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jn9uJ-0005JK-AT for emacs-devel@gnu.org; Sun, 21 Jun 2020 20:01:51 -0400 Original-Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]:39642) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jn9uG-00045h-7m for emacs-devel@gnu.org; Sun, 21 Jun 2020 20:01:50 -0400 Original-Received: by mail-wm1-x335.google.com with SMTP id t194so14053354wmt.4 for ; Sun, 21 Jun 2020 17:01:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=R1dKjHj9nHLpkAY7UN5zN9m0JEbuybE7MRkW6oWAa6A=; b=XCMHayFepuXk3BrrQPDlX/gK/BovCWaVM87tYP32Yxfbqjz5woz0lkN4CkkTmaX1BM Qy4pELssfzPDDvMYJGYK7AsdwEfhgtm4m+FacjxYxUe9HzD3JJul/1SWQZVU3srMGHOH Wk3ec4qlGKOe8h4Xs9edP0XWbpM0D2azsR0PP023GqvHPmZqbiyziEeH4u5UwDvUXTKj J4HBLnaV2vARBwTLzYmFbo8wpoXR2KJmPH5GblVKFqOM3frcazM9dpDC+czzmSk9l5YE XiJHbTEymgnWXIj7obXDKwvW9wCg+8IrvhLOG4LW8w8FrrWX4eNR//BSReo33Krqovq6 GNMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-language; bh=R1dKjHj9nHLpkAY7UN5zN9m0JEbuybE7MRkW6oWAa6A=; b=PB/L9WhJMoD2vUNEm72HY72HUC/4ENhzjFVew+2neTsHHX8m+aK1f8SmZlMHMrh+OB 6fajir+wZlq7xDF9l5LMB55IbEUd1j1sNWAwW4mtUyLxGFbDiLa18eWiFnaCR8MPLQlk wGX8khFjyEphhM1TWn3mMZMzONywrsyTfDHGfxUGQrip/8q1sdLdni2IOKPr/TUIza3C qxcaYCZw8rMHCuftOuYrHnCwatfQrkzUfoOu7pNYXL7IszYpgepnYnH79h5ll7PfPqSu R/3NZXq0CoY0/arBCcMlkHUQdiFJ5zS1MQyH3Sm/GDomg1ZEeaeM65muaY1e4QHhTTe1 PICw== X-Gm-Message-State: AOAM533mdohARa0BFjyqUvGRZEfTBE7++qes+rvb42s0aiCTQjX8QxCD B94gPS8FkmWiNexFAgEHzaxn74+w X-Google-Smtp-Source: ABdhPJy9ozkOxTB8TF7rc2P55cs4Qq/iVQE8TtqjVLMbB4H74CyE+4aE69nVWjxGw+HCh84//+zldg== X-Received: by 2002:a7b:c5c4:: with SMTP id n4mr10551938wmk.67.1592784105016; Sun, 21 Jun 2020 17:01:45 -0700 (PDT) Original-Received: from [192.168.0.3] ([66.205.73.129]) by smtp.googlemail.com with ESMTPSA id b17sm6912091wrp.32.2020.06.21.17.01.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 21 Jun 2020 17:01:44 -0700 (PDT) In-Reply-To: <86tuz69y51.fsf@gmail.com> Content-Language: en-US Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=raaahh@gmail.com; helo=mail-wm1-x335.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: 0 X-Spam_score: 0.0 X-Spam_bar: / X-Spam_report: (0.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=1, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:252501 Archived-At: This is a multi-part message in MIME format. --------------F0C43CFB5748594229E4A589 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 20.06.2020 07:09, andres.ramirez wrote: > Dmitry> Anybody? > > Before the patch: > --8<---------------cut here---------------start------------->8--- > Elapsed time: 6.010101s > Elapsed time: 5.863914s > --8<---------------cut here---------------end--------------->8--- > > After installing ripgrep and patch: > --8<---------------cut here---------------start------------->8--- > Elapsed time: 3.261737s > Elapsed time: 1.742008s > --8<---------------cut here---------------end--------------->8--- Thanks, Andres. Looks promising. Here's the latest version of the patch, if you'd like to test. I don't expect major changes in performance, but it does add a pipe to 'sort', which creates some overhead proportional to the number of search results. To enable ripgrep with this, one needs to 'M-x customize-variable xref-search-command-template'. --------------F0C43CFB5748594229E4A589 Content-Type: text/x-patch; charset=UTF-8; name="xref-ripgrep.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="xref-ripgrep.diff" diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index 3e3a37f6da..a8283d0d4a 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -1246,12 +1246,45 @@ xref-matches-in-directory (declare-function tramp-tramp-file-p "tramp") (declare-function tramp-file-local-name "tramp") +;; '-s' because 'git ls-files' can output broken symlinks. +(defconst xref-grep-search-template + "xargs -0 grep -snHE -e " + "Use Grep to search a list of files piped from stdin.") + +;; See https://github.com/BurntSushi/ripgrep/issues/152 on +;; the subject of non-deterministic output. +(defconst xref-ripgrep-search-template + "xargs -0 rg -nH --no-messages -g '!*/' -e | sort -t: -k1 -k2n" + "Use ripgrep to search a list of files piped from stdin. + +The arguments are chosen carefully so that the output format is +compatible with Grep. As well as its '-s' argument. + +Note: by default, ripgrep's output order is non-deterministic +because it does the search in parallel. You can use the template +without the '| sort ...' part if GNU sort is not available on +your system and/or stable ordering is not important to you.") + +(defcustom xref-search-command-template xref-grep-search-template + "Command template to search a list of files piped from stdin. + +Allowed fields: + + for extra arguments such as -i and --color + for the regexp itself (in Extended format)" + :type `(choice + (const :tag "Use Grep" ,xref-grep-search-template) + (const :tag "Use ripgrep" ,xref-ripgrep-search-template) + (string :tag "User defined"))) + ;;;###autoload (defun xref-matches-in-files (regexp files) "Find all matches for REGEXP in FILES. Return a list of xref values. FILES must be a list of absolute file names." (cl-assert (consp files)) + (require 'grep) + (defvar grep-highlight-matches) (pcase-let* ((output (get-buffer-create " *project grep output*")) (`(,grep-re ,file-group ,line-group . ,_) (car grep-regexp-alist)) @@ -1261,13 +1294,12 @@ xref-matches-in-files ;; first file is remote, they all are, and on the same host. (dir (file-name-directory (car files))) (remote-id (file-remote-p dir)) - ;; 'git ls-files' can output broken symlinks. - (command (format "xargs -0 grep %s -snHE -e %s" - (if (and case-fold-search - (isearch-no-upper-case-p regexp t)) - "-i" - "") - (shell-quote-argument (xref--regexp-to-extended regexp))))) + ;; The 'auto' default would be fine too, but ripgrep can't handle + ;; the options we pass in that case. + (grep-highlight-matches) + (command (grep-expand-template xref-search-command-template + (xref--regexp-to-extended regexp) + regexp))) (when remote-id (require 'tramp) (setq files (mapcar --------------F0C43CFB5748594229E4A589--