[PATCH 2 of 6] revbranchcache: store repo on the object

Durham Goode durham at fb.com
Mon Mar 16 22:36:14 CDT 2015


# HG changeset patch
# User Durham Goode <durham at fb.com>
# Date 1423627071 28800
#      Tue Feb 10 19:57:51 2015 -0800
# Node ID 99e73b9195b1d8bcb3d191db7aa430fd6f70fdcd
# Parent  05866edc57696fcafde7de5e75e65f6b824aa540
revbranchcache: store repo on the object

Previously we would instantiate the revbranchcache with a repo object, use it
briefly, then require it be passed in every time we wanted to fetch any
information. This seems unnecessary since it's obviously specific to that repo
(since it was constructed with it).

This patch stores the repo on the revbranchcache object, and removes the repo
parameter from the various functions on that class. This has the other nice
benefit of removing the double-revbranchcache-read that existed before (it was
read once for the branch revset, and once for the repo.revbranchcache).

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -98,7 +98,7 @@ def updatecache(repo):
         partial.write(repo)
 
     if repo._revbranchcache is not None:
-        repo._revbranchcache.write(repo)
+        repo._revbranchcache.write()
 
     assert partial.validfor(repo), filtername
     repo._branchcaches[repo.filtername] = partial
@@ -239,11 +239,9 @@ class branchcache(dict):
         cl = repo.changelog
         # collect new branch entries
         newbranches = {}
-        urepo = repo.unfiltered()
-        ucl = urepo.changelog
         getbranchinfo = repo.revbranchcache().branchinfo
         for r in revgen:
-            branch, closesbranch = getbranchinfo(ucl, r)
+            branch, closesbranch = getbranchinfo(r)
             newbranches.setdefault(branch, []).append(r)
             if closesbranch:
                 self._closednodes.add(cl.node(r))
@@ -331,6 +329,7 @@ class revbranchcache(object):
 
     def __init__(self, repo, readonly=True):
         assert repo.filtername is None
+        self._repo = repo
         self._names = [] # branch names in local encoding with static index
         self._rbcrevs = array('c') # structs of type _rbcrecfmt
         self._rbcsnameslen = 0
@@ -360,9 +359,10 @@ class revbranchcache(object):
         self._rbcnamescount = len(self._names) # number of good names on disk
         self._namesreverse = dict((b, r) for r, b in enumerate(self._names))
 
-    def branchinfo(self, changelog, rev):
+    def branchinfo(self, rev):
         """Return branch name and close flag for rev, using and updating
         persistent cache."""
+        changelog = self._repo.changelog
         rbcrevidx = rev * _rbcrecsize
 
         # if requested rev is missing, add and populate all missing revs
@@ -371,7 +371,7 @@ class revbranchcache(object):
             self._rbcrevs.extend('\0' * (len(changelog) * _rbcrecsize -
                                          len(self._rbcrevs)))
             for r in xrange(first, len(changelog)):
-                self._branchinfo(changelog, r)
+                self._branchinfo(r)
 
         # fast path: extract data from cache, use it if node is matching
         reponode = changelog.node(rev)[:_rbcnodelen]
@@ -384,10 +384,11 @@ class revbranchcache(object):
             return self._names[branchidx], close
         # fall back to slow path and make sure it will be written to disk
         self._rbcrevslen = min(self._rbcrevslen, rev)
-        return self._branchinfo(changelog, rev)
+        return self._branchinfo(rev)
 
-    def _branchinfo(self, changelog, rev):
+    def _branchinfo(self, rev):
         """Retrieve branch info from changelog and update _rbcrevs"""
+        changelog = self._repo.changelog
         b, close = changelog.branchinfo(rev)
         if b in self._namesreverse:
             branchidx = self._namesreverse[b]
@@ -404,8 +405,9 @@ class revbranchcache(object):
         self._rbcrevs[rbcrevidx:rbcrevidx + _rbcrecsize] = rec
         return b, close
 
-    def write(self, repo):
+    def write(self):
         """Save branch cache if it is dirty."""
+        repo = self._repo
         if self._rbcnamescount < len(self._names):
             try:
                 if self._rbcnamescount != 0:
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -528,10 +528,7 @@ def branch(repo, subset, x):
     a regular expression. To match a branch that actually starts with `re:`,
     use the prefix `literal:`.
     """
-    import branchmap
-    urepo = repo.unfiltered()
-    ucl = urepo.changelog
-    getbi = branchmap.revbranchcache(urepo, readonly=True).branchinfo
+    getbi = repo.revbranchcache().branchinfo
 
     try:
         b = getstring(x, '')
@@ -544,16 +541,16 @@ def branch(repo, subset, x):
             # note: falls through to the revspec case if no branch with
             # this name exists
             if pattern in repo.branchmap():
-                return subset.filter(lambda r: matcher(getbi(ucl, r)[0]))
+                return subset.filter(lambda r: matcher(getbi(r)[0]))
         else:
-            return subset.filter(lambda r: matcher(getbi(ucl, r)[0]))
+            return subset.filter(lambda r: matcher(getbi(r)[0]))
 
     s = getset(repo, fullreposet(repo), x)
     b = set()
     for r in s:
-        b.add(getbi(ucl, r)[0])
+        b.add(getbi(r)[0])
     c = s.__contains__
-    return subset.filter(lambda r: c(r) or getbi(ucl, r)[0] in b)
+    return subset.filter(lambda r: c(r) or getbi(r)[0] in b)
 
 def bumped(repo, subset, x):
     """``bumped()``


More information about the Mercurial-devel mailing list