Patchwork [1,of,2,V4] rust: hooking into Python code

login
register
mail settings
Submitter Georges Racinet
Date Oct. 14, 2018, 1:22 p.m.
Message ID <c04176c0f8b9aeaf196b.1539523326@ishtar>
Download mbox | patch
Permalink /patch/35993/
State Accepted
Headers show

Comments

Georges Racinet - Oct. 14, 2018, 1:22 p.m.
# HG changeset patch
# User Georges Racinet <gracinet@anybox.fr>
# Date 1538060144 -7200
#      Thu Sep 27 16:55:44 2018 +0200
# Node ID c04176c0f8b9aeaf196bd1eac00207d526f3d53b
# Parent  3b275f5497771d8a71336273a77575dbf4882798
# EXP-Topic rustancestors-contains
rust: hooking into Python code

We introduce a new class called 'rustlazyancestors'
in the ancestors module, which is used only if
parsers.rustlazyancestors does exist.

The implementation of __contains__ stays unchanged,
but is now backed by the Rust iterator. It would
probably be a good candidate for further development,
though, as it is mostly looping, and duplicates the
'seen' set.

The Rust code could be further optimized, however it already
gives rise to performance improvements:

median timing from hg perfancestors:
- on pypy:
    before: 0.077566s
    after:  0.016676s -79%
- on mozilla central:
    before: 0.190037s
    after:  0.082225s -58%
- on a private repository (about one million revisions):
    before: 0.567085s
    after:  0.108816s -80%
- on another private repository (about 400 000 revisions):
    before: 1.440918s
    after:  0.290116s -80%

median timing for hg perfbranchmap base
- on pypy:
    before:  1.383413s
    after:   0.507993s -63%
- on mozilla central:
    before:  2.821940s
    after:   1.258902s -55%
- on a private repository (about one million revisions):
    before: 77.065076s
    after:   16.158475s -80%
- on another private repository (about 401 000 revisions):
    before:  7.835503s
    after:   3.545331s -54%
Yuya Nishihara - Oct. 14, 2018, 3:38 p.m.
On Sun, 14 Oct 2018 15:22:06 +0200, Georges Racinet wrote:
> # HG changeset patch
> # User Georges Racinet <gracinet@anybox.fr>
> # Date 1538060144 -7200
> #      Thu Sep 27 16:55:44 2018 +0200
> # Node ID c04176c0f8b9aeaf196bd1eac00207d526f3d53b
> # Parent  3b275f5497771d8a71336273a77575dbf4882798
> # EXP-Topic rustancestors-contains
> rust: hooking into Python code

Queued this, thanks.

Patch

diff -r 3b275f549777 -r c04176c0f8b9 mercurial/ancestor.py
--- a/mercurial/ancestor.py	Thu Sep 27 16:56:15 2018 +0200
+++ b/mercurial/ancestor.py	Thu Sep 27 16:55:44 2018 +0200
@@ -11,9 +11,12 @@ 
 
 from .node import nullrev
 from . import (
+    policy,
     pycompat,
 )
 
+parsers = policy.importmod(r'parsers')
+
 def commonancestorsheads(pfunc, *nodes):
     """Returns a set with the heads of all common ancestors of all nodes,
     heads(::nodes[0] and ::nodes[1] and ...) .
@@ -379,3 +382,25 @@ 
             # free up memory.
             self._containsiter = None
             return False
+
+class rustlazyancestors(lazyancestors):
+
+    def __init__(self, index, revs, stoprev=0, inclusive=False):
+        self._index = index
+        self._stoprev = stoprev
+        self._inclusive = inclusive
+        # no need to prefilter out init revs that are smaller than stoprev,
+        # it's done by rustlazyancestors constructor.
+        # we need to convert to a list, because our ruslazyancestors
+        # constructor (from C code) doesn't understand anything else yet
+        self._initrevs = initrevs = list(revs)
+
+        self._containsseen = set()
+        self._containsiter = parsers.rustlazyancestors(
+            index, initrevs, stoprev, inclusive)
+
+    def __iter__(self):
+        return parsers.rustlazyancestors(self._index,
+                                         self._initrevs,
+                                         self._stoprev,
+                                         self._inclusive)
diff -r 3b275f549777 -r c04176c0f8b9 mercurial/revlog.py
--- a/mercurial/revlog.py	Thu Sep 27 16:56:15 2018 +0200
+++ b/mercurial/revlog.py	Thu Sep 27 16:55:44 2018 +0200
@@ -763,6 +763,10 @@ 
         for r in revs:
             checkrev(r)
         # and we're sure ancestors aren't filtered as well
+        if util.safehasattr(parsers, 'rustlazyancestors'):
+            return ancestor.rustlazyancestors(
+                self.index, revs,
+                stoprev=stoprev, inclusive=inclusive)
         return ancestor.lazyancestors(self._uncheckedparentrevs, revs,
                                       stoprev=stoprev, inclusive=inclusive)