[PATCH 1 of 3] filecache: use try-except for faster __dict__ lookup

Yuya Nishihara yuya at tcha.org
Tue Oct 23 12:43:26 UTC 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1540030385 -32400
#      Sat Oct 20 19:13:05 2018 +0900
# Node ID 6a3a42dfcdd0ccb628ae3b8f3129b26db32a7ff0
# Parent  dce0e0f78f0f10578a17b586fe061e1985dd5c5f
filecache: use try-except for faster __dict__ lookup

Python function call is slow, and the cost could be significant here.

  $ hg perfrevset 'branch(tip)' -R mercurial
  (orig) wall 0.139511 comb 0.140000 user 0.140000 sys 0.000000 (best of 66)
  (this) wall 0.114195 comb 0.110000 user 0.110000 sys 0.000000 (best of 81)

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -91,7 +91,13 @@ class _basefilecache(scmutil.filecache):
     def __get__(self, repo, type=None):
         if repo is None:
             return self
-        return super(_basefilecache, self).__get__(repo.unfiltered(), type)
+        # inlined the fast path as the cost of function call matters
+        unfi = repo.unfiltered()
+        try:
+            return unfi.__dict__[self.sname]
+        except KeyError:
+            pass
+        return super(_basefilecache, self).__get__(unfi, type)
     def __set__(self, repo, value):
         return super(_basefilecache, self).__set__(repo.unfiltered(), value)
     def __delete__(self, repo):
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -1292,9 +1292,10 @@ class filecache(object):
         if obj is None:
             return self
         # do we need to check if the file changed?
-        if self.sname in obj.__dict__:
-            assert self.name in obj._filecache, self.name
+        try:
             return obj.__dict__[self.sname]
+        except KeyError:
+            pass
 
         entry = obj._filecache.get(self.name)
 


More information about the Mercurial-devel mailing list