From patchwork Tue May 26 12:37:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D8586: files: speed up `hg files` when no flags change display From: phabricator X-Patchwork-Id: 46379 Message-Id: To: Phabricator Cc: mercurial-devel@mercurial-scm.org Date: Tue, 26 May 2020 12:37:54 +0000 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 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)