grep: speed up `hg grep --all-files some/path` by using ctx.matches(match)
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 09 Jan 2020 15:41:40 -0800
changeset 44049 6cfaebb625d3
parent 44047 de5d34ca01bd
child 44050 2ecbc4ec87d8
grep: speed up `hg grep --all-files some/path` by using ctx.matches(match) ctx.matches(match) avoids walking unintersting parts of the tree when using tree manifests. That can make a very big difference when grepping in a small subset of the tree (2.0s -> 0.7s in my case, but can of course be made more extreme by picking a smaller subset of files). Differential Revision: https://phab.mercurial-scm.org/D7820
mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Thu Jan 09 10:17:10 2020 -0500
+++ b/mercurial/cmdutil.py	Thu Jan 09 15:41:40 2020 -0800
@@ -2429,12 +2429,16 @@
 
                     def fns_generator():
                         if allfiles:
-                            fiter = iter(ctx)
+
+                            def bad(f, msg):
+                                pass
+
+                            for f in ctx.matches(matchmod.badmatch(match, bad)):
+                                yield f
                         else:
-                            fiter = ctx.files()
-                        for f in fiter:
-                            if match(f):
-                                yield f
+                            for f in ctx.files():
+                                if match(f):
+                                    yield f
 
                     fns = fns_generator()
                 prepare(ctx, fns)