Patchwork [1,of,2,V2] revlog: refactor out _finddeltainfo from _addrevision

login
register
mail settings
Submitter Paul Morelle
Date Jan. 19, 2018, 8:16 a.m.
Message ID <32bc4595737c2211dfcb.1516349800@taranis.localdomain>
Download mbox | patch
Permalink /patch/26947/
State Accepted
Headers show

Comments

Paul Morelle - Jan. 19, 2018, 8:16 a.m.
# HG changeset patch
# User Paul Morelle <paul.morelle@octobus.net>
# Date 1515936982 -3600
#      Sun Jan 14 14:36:22 2018 +0100
# Node ID 32bc4595737c2211dfcbf53cb499a366b9986dfd
# Parent  45b678bf3a787085d56fad5bee494e0c160aa120
# EXP-Topic refactor-revlog
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 32bc4595737c
revlog: refactor out _finddeltainfo from _addrevision

Splicing the code into smaller chunks should help understanding it,
and eventually override some parts in experimental branches to try
optimization.

Patch

diff -r 45b678bf3a78 -r 32bc4595737c mercurial/revlog.py
--- a/mercurial/revlog.py	Wed Jan 17 17:07:55 2018 +0100
+++ b/mercurial/revlog.py	Sun Jan 14 14:36:22 2018 +0100
@@ -279,6 +279,7 @@ 
     p1 = attr.ib()
     p2 = attr.ib()
     btext = attr.ib()
+    textlen = attr.ib()
     cachedelta = attr.ib()
     flags = attr.ib()
 
@@ -1987,6 +1988,33 @@ 
         return _deltainfo(dist, deltalen, (header, data), deltabase,
                          chainbase, chainlen, compresseddeltalen)
 
+    def _finddeltainfo(self, revinfo, fh):
+        """Find an acceptable delta against a candidate revision
+
+        revinfo: information about the revision (instance of _revisioninfo)
+        fh:      file handle to either the .i or the .d revlog file,
+                 depending on whether it is inlined or not
+
+        Returns the first acceptable candidate revision, as ordered by
+        _getcandidaterevs
+        """
+        cachedelta = revinfo.cachedelta
+        p1 = revinfo.p1
+        p2 = revinfo.p2
+
+        deltainfo = None
+        for candidaterevs in self._getcandidaterevs(p1, p2, cachedelta):
+            nominateddeltas = []
+            for candidaterev in candidaterevs:
+                candidatedelta = self._builddeltainfo(revinfo, candidaterev, fh)
+                if self._isgooddeltainfo(candidatedelta, revinfo.textlen):
+                    nominateddeltas.append(candidatedelta)
+            if nominateddeltas:
+                deltainfo = min(nominateddeltas, key=lambda x: x.deltalen)
+                break
+
+        return deltainfo
+
     def _addrevision(self, node, rawtext, transaction, link, p1, p2, flags,
                      cachedelta, ifh, dfh, alwayscache=False):
         """internal function to add revisions to the log
@@ -2016,7 +2044,6 @@ 
         curr = len(self)
         prev = curr - 1
         offset = self.end(prev)
-        deltainfo = None
         p1r, p2r = self.rev(p1), self.rev(p2)
 
         # full versions are inserted when the needed deltas
@@ -2027,16 +2054,8 @@ 
         else:
             textlen = len(rawtext)
 
-        revinfo = _revisioninfo(node, p1, p2, btext, cachedelta, flags)
-        for candidaterevs in self._getcandidaterevs(p1, p2, cachedelta):
-            nominateddeltas = []
-            for candidaterev in candidaterevs:
-                candidatedelta = self._builddeltainfo(revinfo, candidaterev, fh)
-                if self._isgooddeltainfo(candidatedelta, textlen):
-                    nominateddeltas.append(candidatedelta)
-            if nominateddeltas:
-                deltainfo = min(nominateddeltas, key=lambda x: x.deltalen)
-                break
+        revinfo = _revisioninfo(node, p1, p2, btext, textlen, cachedelta, flags)
+        deltainfo = self._finddeltainfo(revinfo, fh)
 
         if deltainfo is not None:
             base = deltainfo.base