Patchwork D8586: files: speed up `hg files` when no flags change display

login
register
mail settings
Submitter phabricator
Date May 26, 2020, 12:37 p.m.
Message ID <differential-rev-PHID-DREV-3figz2kx2ji3zbtkxema-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46379/
State Superseded
Headers show

Comments

phabricator - May 26, 2020, 12:37 p.m.
valentin.gatienbaron created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8586

AFFECTED FILES
  mercurial/cmdutil.py

CHANGE DETAILS




To: valentin.gatienbaron, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -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 = b''
+        for f in ctx.matches(m):
+            buf += fmt % uipathfn(f)
+            if len(buf) > 5000:
+                ui.write(buf)
+                buf = b''
+            ret = 0
+        if buf:
+            ui.write(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)