changegroup: introduce requests to define delta generation
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 09 Aug 2018 09:28:26 -0700
changeset 39018 e793e11e1462
parent 39017 ef3d3a2f9aa5
child 39019 d0d197ab0646
changegroup: introduce requests to define delta generation Currently, we iterate through each revision we will be producing a delta for then call into 1 of 2 functions for generating that delta. Deltas are emitted as we iterate. A problem with this model is that revision generation is tightly coupled to the changegroup code. And the storage layer needs to expose APIs like deltaparent() so changegroup delta generation can produce a delta with that knowledge. Another problem is that in this model, deltas can only be produced sequentially after the previous delta was produced and emitted. Some storage backends might be capable of producing deltas in parallel (e.g. if the changegroup deltas are cached somewhere). This commit aims to solve these problems by turning delta generation into a 2 phase implementation where the first phase determines info about all the deltas that need to be generated and the 2nd phase resolves those deltas. We introduce a "revisiondeltarequest" object that holds data about a to-be-generated delta. We perform a full pass over all revisions whose delta is to be generated and generate a "revisiondeltarequest" for each. Then we iterate over the "revisiondeltarequest" instances and derive a "revisiondelta" for each. This patch was quite large. In order to avoid even more churn, aspects of the implementation are less than ideal. e.g. we're recording revision numbers instead of nodes in a few places and we don't yet have a formal API for resolving an iterable of revisiondeltarequest instances. Things will be improved in subsequent commits. Unfortunately, this commit reduces performance substantially. For `hg perfchangegroupchangelog` on my hg repo: ! wall 1.512607 comb 1.510000 user 1.490000 sys 0.020000 (best of 7) ! wall 2.150863 comb 2.150000 user 2.150000 sys 0.000000 (best of 5) And for `hg bundle -t none-v2 -a` for the mozilla-unified repo: 178.32user 4.22system 3:02.59elapsed 190.97user 4.17system 3:15.19elapsed Some of this was attributed to changelog slowdown. `hg perfchangegroupchangelog` on mozilla-unified: ! wall 21.688715 comb 21.690000 user 21.570000 sys 0.120000 (best of 3) ! wall 25.683659 comb 25.680000 user 25.540000 sys 0.140000 (best of 3) Profiling seems to reveal that the changelog slowdown is due to reading changelog revisions multiple times. First in the linknode callback to resolve the set of files changed. Second in the delta generation. Before, we likely had hit the last revision cache in the revlog when doing delta generation since we performed that immediately after performing the linknode callback. I'm not exactly sure where the other ~8s are being spent. It might be from overhead of constructing a few million revisiondeltarequest objects. I'm OK with the regression for now because it is in service of a larger cause (storage abstraction). I'll try to profile later and claw back the performance. Differential Revision: https://phab.mercurial-scm.org/D4215
mercurial/changegroup.py
--- a/mercurial/changegroup.py	Wed Aug 08 20:17:48 2018 -0700
+++ b/mercurial/changegroup.py	Thu Aug 09 09:28:26 2018 -0700
@@ -502,6 +502,32 @@
         return readexactly(self._fh, n)
 
 @attr.s(slots=True, frozen=True)
+class revisiondeltarequest(object):
+    """Describes a request to construct a revision delta.
+
+    Instances are converted into ``revisiondelta`` later.
+    """
+    # Revision whose delta will be generated.
+    node = attr.ib()
+
+    # Linknode value.
+    linknode = attr.ib()
+
+    # Parent revisions to record in ``revisiondelta`` instance.
+    p1node = attr.ib()
+    p2node = attr.ib()
+
+    # Base revision that delta should be generated against. If nullrev,
+    # the full revision data should be populated. If None, the delta
+    # may be generated against any base revision that is an ancestor of
+    # this revision. If any other numeric value, the delta should be
+    # produced against that revision.
+    baserev = attr.ib()
+
+    # Whether this should be marked as an ellipsis revision.
+    ellipsis = attr.ib(default=False)
+
+@attr.s(slots=True, frozen=True)
 class revisiondelta(object):
     """Describes a delta entry in a changegroup.
 
@@ -587,14 +613,21 @@
     key = lambda n: cl.rev(lookup(n))
     return [store.rev(n) for n in sorted(nodes, key=key)]
 
-def _revisiondeltanormal(store, rev, prev, linknode, forcedeltaparentprev):
-    """Construct a revision delta for non-ellipses changegroup generation."""
-    node = store.node(rev)
-    p1, p2 = store.parentrevs(rev)
+def _handlerevisiondeltarequest(store, request, prev):
+    """Obtain a revisiondelta from a revisiondeltarequest"""
+
+    node = request.node
+    rev = store.rev(node)
 
-    if forcedeltaparentprev:
-        base = prev
+    # Requesting a full revision.
+    if request.baserev == nullrev:
+        base = nullrev
+    # Requesting an explicit revision.
+    elif request.baserev is not None:
+        base = request.baserev
+    # Allowing us to choose.
     else:
+        p1, p2 = store.parentrevs(rev)
         dp = store.deltaparent(rev)
 
         if dp == nullrev and store.storedeltachains:
@@ -637,23 +670,23 @@
     else:
         delta = store.revdiff(base, rev)
 
-    p1n, p2n = store.parents(node)
+    extraflags = revlog.REVIDX_ELLIPSIS if request.ellipsis else 0
 
     return revisiondelta(
         node=node,
-        p1node=p1n,
-        p2node=p2n,
+        p1node=request.p1node,
+        p2node=request.p2node,
+        linknode=request.linknode,
         basenode=store.node(base),
-        linknode=linknode,
-        flags=store.flags(rev),
+        flags=store.flags(rev) | extraflags,
         baserevisionsize=baserevisionsize,
         revision=revision,
         delta=delta,
     )
 
-def _revisiondeltanarrow(cl, store, ischangelog, rev, linkrev,
-                         linknode, clrevtolocalrev, fullclnodes,
-                         precomputedellipsis):
+def _makenarrowdeltarequest(cl, store, ischangelog, rev, node, linkrev,
+                            linknode, clrevtolocalrev, fullclnodes,
+                            precomputedellipsis):
     linkparents = precomputedellipsis[linkrev]
     def local(clrev):
         """Turn a changelog revnum into a local revnum.
@@ -726,23 +759,16 @@
     else:
         p1, p2 = sorted(local(p) for p in linkparents)
 
-    n = store.node(rev)
-    p1n, p2n = store.node(p1), store.node(p2)
-    flags = store.flags(rev)
-    flags |= revlog.REVIDX_ELLIPSIS
+    p1node, p2node = store.node(p1), store.node(p2)
 
     # TODO: try and actually send deltas for ellipsis data blocks
-
-    return revisiondelta(
-        node=n,
-        p1node=p1n,
-        p2node=p2n,
-        basenode=nullid,
+    return revisiondeltarequest(
+        node=node,
+        p1node=p1node,
+        p2node=p2node,
         linknode=linknode,
-        flags=flags,
-        baserevisionsize=None,
-        revision=store.revision(n),
-        delta=None,
+        baserev=nullrev,
+        ellipsis=True,
     )
 
 def deltagroup(repo, revs, store, ischangelog, lookup, forcedeltaparentprev,
@@ -759,25 +785,32 @@
     if not revs:
         return
 
+    # We perform two passes over the revisions whose data we will emit.
+    #
+    # In the first pass, we obtain information about the deltas that will
+    # be generated. This involves computing linknodes and adjusting the
+    # request to take shallow fetching into account. The end result of
+    # this pass is a list of "request" objects stating which deltas
+    # to obtain.
+    #
+    # The second pass is simply resolving the requested deltas.
+
     cl = repo.changelog
 
+    # In the first pass, collect info about the deltas we'll be
+    # generating.
+    requests = []
+
     # Add the parent of the first rev.
     revs.insert(0, store.parentrevs(revs[0])[0])
 
-    # build deltas
-    progress = None
-    if units is not None:
-        progress = repo.ui.makeprogress(_('bundling'), unit=units,
-                                        total=(len(revs) - 1))
-
     for i in pycompat.xrange(len(revs) - 1):
-        if progress:
-            progress.update(i + 1)
-
         prev = revs[i]
         curr = revs[i + 1]
 
-        linknode = lookup(store.node(curr))
+        node = store.node(curr)
+        linknode = lookup(node)
+        p1node, p2node = store.parents(node)
 
         if ellipses:
             linkrev = cl.rev(linknode)
@@ -786,21 +819,47 @@
             # This is a node to send in full, because the changeset it
             # corresponds to was a full changeset.
             if linknode in fullclnodes:
-                delta = _revisiondeltanormal(store, curr, prev, linknode,
-                                             forcedeltaparentprev)
+                requests.append(revisiondeltarequest(
+                    node=node,
+                    p1node=p1node,
+                    p2node=p2node,
+                    linknode=linknode,
+                    baserev=None,
+                ))
+
             elif linkrev not in precomputedellipsis:
-                delta = None
+                pass
             else:
-                delta = _revisiondeltanarrow(
-                    cl, store, ischangelog, curr, linkrev, linknode,
+                requests.append(_makenarrowdeltarequest(
+                    cl, store, ischangelog, curr, node, linkrev, linknode,
                     clrevtolocalrev, fullclnodes,
-                    precomputedellipsis)
+                    precomputedellipsis))
         else:
-            delta = _revisiondeltanormal(store, curr, prev, linknode,
-                                         forcedeltaparentprev)
+            requests.append(revisiondeltarequest(
+                node=node,
+                p1node=p1node,
+                p2node=p2node,
+                linknode=linknode,
+                baserev=prev if forcedeltaparentprev else None,
+            ))
 
-        if delta:
-            yield delta
+    # We expect the first pass to be fast, so we only engage the progress
+    # meter for constructing the revision deltas.
+    progress = None
+    if units is not None:
+        progress = repo.ui.makeprogress(_('bundling'), unit=units,
+                                        total=len(requests))
+
+    prevrev = revs[0]
+    for i, request in enumerate(requests):
+        if progress:
+            progress.update(i + 1)
+
+        delta = _handlerevisiondeltarequest(store, request, prevrev)
+
+        yield delta
+
+        prevrev = store.rev(request.node)
 
     if progress:
         progress.complete()