Mercurial > public > mercurial-scm > hg-stable
diff mercurial/ancestor.py @ 40300:72b94f946e90
rust: rustlazyancestors.__contains__
This changeset provides a Rust implementation of
the iteration performed by lazyancestor.__contains__
It has the advantage over the Python iteration to use
the 'seen' set encapsuled into the dedicated iterator (self._containsiter),
rather than storing emitted items in another set (self._containsseen),
and hence should reduce the memory footprint.
Also, there's no need to convert intermediate emitted revisions back into
Python integers.
At this point, it would be tempting to implement the whole lazyancestor object
in Rust, but that would lead to more C wrapping code (two objects) for
little expected benefits.
author | Georges Racinet <gracinet@anybox.fr> |
---|---|
date | Mon, 08 Oct 2018 19:11:41 +0200 |
parents | 9cadb0f5f227 |
children | 4856c9b8cbaf |
line wrap: on
line diff
--- a/mercurial/ancestor.py Sun Oct 14 01:39:22 2018 -0400 +++ b/mercurial/ancestor.py Mon Oct 08 19:11:41 2018 +0200 @@ -383,7 +383,7 @@ self._containsiter = None return False -class rustlazyancestors(lazyancestors): +class rustlazyancestors(object): def __init__(self, index, revs, stoprev=0, inclusive=False): self._index = index @@ -395,12 +395,26 @@ # 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 __nonzero__(self): + """False if the set is empty, True otherwise. + + It's better to duplicate this essentially trivial method than + to subclass lazyancestors + """ + try: + next(iter(self)) + return True + except StopIteration: + return False + def __iter__(self): return parsers.rustlazyancestors(self._index, self._initrevs, self._stoprev, self._inclusive) + + def __contains__(self, target): + return target in self._containsiter