[PATCH 1 of 2] revlog: add __contains__ for fast membership test

Pierre-Yves David pierre-yves.david at ens-lyon.org
Wed Feb 4 16:21:05 UTC 2015



On 02/04/2015 01:27 PM, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya at tcha.org>
> # Date 1423052757 -32400
> #      Wed Feb 04 21:25:57 2015 +0900
> # Node ID f65a44d4905a5b4767e4b901de6ed1efe703611f
> # Parent  e1dbe0b215ae137eec53ceb12440536d570a83d2
> revlog: add __contains__ for fast membership test
>
> Because revlog implements __iter__, "rev in revlog" works but does silly O(n)
> lookup unexpectedly. So it seems good to add fast version of __contains__.
>
> This allows "rev in repo.changelog" in the next patch.

Pushed to the clowncopter with a couple of follow up idea.

>
> diff --git a/mercurial/changelog.py b/mercurial/changelog.py
> --- a/mercurial/changelog.py
> +++ b/mercurial/changelog.py
> @@ -143,6 +143,11 @@ class changelog(revlog.revlog):
>               if i not in self.filteredrevs:
>                   return self.node(i)
>
> +    def __contains__(self, rev):
> +        """filtered version of revlog.__contains__"""
> +        return (revlog.revlog.__contains__(self, rev)
> +                and rev not in self.filteredrevs)

I think there is some magical optimisation to do here, like hard coding 
the __contains__ content and maybe test for the existence of any 
filtered revision before checking for members


-- 
Pierre-Yves David



More information about the Mercurial-devel mailing list