Mercurial > hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
40299:3570adf20d91 | 40300:72b94f946e90 |
---|---|
381 # Set to None to indicate fast-path can be used next time, and to | 381 # Set to None to indicate fast-path can be used next time, and to |
382 # free up memory. | 382 # free up memory. |
383 self._containsiter = None | 383 self._containsiter = None |
384 return False | 384 return False |
385 | 385 |
386 class rustlazyancestors(lazyancestors): | 386 class rustlazyancestors(object): |
387 | 387 |
388 def __init__(self, index, revs, stoprev=0, inclusive=False): | 388 def __init__(self, index, revs, stoprev=0, inclusive=False): |
389 self._index = index | 389 self._index = index |
390 self._stoprev = stoprev | 390 self._stoprev = stoprev |
391 self._inclusive = inclusive | 391 self._inclusive = inclusive |
393 # it's done by rustlazyancestors constructor. | 393 # it's done by rustlazyancestors constructor. |
394 # we need to convert to a list, because our ruslazyancestors | 394 # we need to convert to a list, because our ruslazyancestors |
395 # constructor (from C code) doesn't understand anything else yet | 395 # constructor (from C code) doesn't understand anything else yet |
396 self._initrevs = initrevs = list(revs) | 396 self._initrevs = initrevs = list(revs) |
397 | 397 |
398 self._containsseen = set() | |
399 self._containsiter = parsers.rustlazyancestors( | 398 self._containsiter = parsers.rustlazyancestors( |
400 index, initrevs, stoprev, inclusive) | 399 index, initrevs, stoprev, inclusive) |
400 | |
401 def __nonzero__(self): | |
402 """False if the set is empty, True otherwise. | |
403 | |
404 It's better to duplicate this essentially trivial method than | |
405 to subclass lazyancestors | |
406 """ | |
407 try: | |
408 next(iter(self)) | |
409 return True | |
410 except StopIteration: | |
411 return False | |
401 | 412 |
402 def __iter__(self): | 413 def __iter__(self): |
403 return parsers.rustlazyancestors(self._index, | 414 return parsers.rustlazyancestors(self._index, |
404 self._initrevs, | 415 self._initrevs, |
405 self._stoprev, | 416 self._stoprev, |
406 self._inclusive) | 417 self._inclusive) |
418 | |
419 def __contains__(self, target): | |
420 return target in self._containsiter |