Patchwork localrepo: micro-optimize __len__() to bypass repoview

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 19, 2018, 1:14 p.m.
Message ID <03870a74aaca105aa0b0.1516367645@mimosa>
Download mbox | patch
Permalink /patch/26950/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 19, 2018, 1:14 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1516365551 -32400
#      Fri Jan 19 21:39:11 2018 +0900
# Node ID 03870a74aaca105aa0b0f1c5edd68754098be5bc
# Parent  f58245b9e3ea6e8945a81b951110cde155819345
localrepo: micro-optimize __len__() to bypass repoview

Since len(changelog) isn't overridden, we don't have to validate a cache of
unfiltered changelog.

  $ python -m timeit -n 10000 \
           -s 'from mercurial import hg, ui; repo = hg.repository(ui.ui());' \
           'len(repo)'
  orig) 10000 loops, best of 3: 32.1 usec per loop
  new)  10000 loops, best of 3: 1.79 usec per loop

Spotted by Jordi GutiƩrrez Hermoso.
Augie Fackler - Jan. 19, 2018, 7:30 p.m.
On Fri, Jan 19, 2018 at 10:14:05PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1516365551 -32400
> #      Fri Jan 19 21:39:11 2018 +0900
> # Node ID 03870a74aaca105aa0b0f1c5edd68754098be5bc
> # Parent  f58245b9e3ea6e8945a81b951110cde155819345
> localrepo: micro-optimize __len__() to bypass repoview

queued, thanks

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -762,7 +762,9 @@  class localrepository(object):
     __bool__ = __nonzero__
 
     def __len__(self):
-        return len(self.changelog)
+        # no need to pay the cost of repoview.changelog
+        unfi = self.unfiltered()
+        return len(unfi.changelog)
 
     def __iter__(self):
         return iter(self.changelog)