files: speed up `hg files` when no flags change display
authorValentin Gatien-Baron <valentin.gatienbaron@gmail.com>
Tue, 26 May 2020 08:15:09 -0400
changeset 44861 065421e12248
parent 44860 5d77f571a563
child 44862 5fe8f02ced6d
files: speed up `hg files` when no flags change display It's not the first time I see slowness from this command slow down tools built on top of hg. The majority of the time is spent merely printing the result before this change, which is clearly not how it should be (especially since the computation of the result also looks slow). Running `hg files` in mozilla-central: parent revision: 1,260s this commit: 0,683s this commit without batching ui.write: 0,931s this commit replacing the body of the loop with `pass`: 0,566s This looks like a prime candidate for a rust fast path, but until then, it seems reasonable to optimize the python. Differential Revision: https://phab.mercurial-scm.org/D8586
mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Mon May 25 22:47:12 2020 -0400
+++ b/mercurial/cmdutil.py	Tue May 26 08:15:09 2020 -0400
@@ -2752,15 +2752,28 @@
     ret = 1
 
     needsfctx = ui.verbose or {b'size', b'flags'} & fm.datahint()
-    for f in ctx.matches(m):
-        fm.startitem()
-        fm.context(ctx=ctx)
-        if needsfctx:
-            fc = ctx[f]
-            fm.write(b'size flags', b'% 10d % 1s ', fc.size(), fc.flags())
-        fm.data(path=f)
-        fm.plain(fmt % uipathfn(f))
-        ret = 0
+    if fm.isplain() and not needsfctx:
+        # Fast path. The speed-up comes from skipping the formatter, and batching
+        # calls to ui.write.
+        buf = []
+        for f in ctx.matches(m):
+            buf.append(fmt % uipathfn(f))
+            if len(buf) > 100:
+                ui.write(b''.join(buf))
+                del buf[:]
+            ret = 0
+        if buf:
+            ui.write(b''.join(buf))
+    else:
+        for f in ctx.matches(m):
+            fm.startitem()
+            fm.context(ctx=ctx)
+            if needsfctx:
+                fc = ctx[f]
+                fm.write(b'size flags', b'% 10d % 1s ', fc.size(), fc.flags())
+            fm.data(path=f)
+            fm.plain(fmt % uipathfn(f))
+            ret = 0
 
     for subpath in sorted(ctx.substate):
         submatch = matchmod.subdirmatcher(subpath, m)