[PATCH V2] smartset: add a "toset" method

Pierre-Yves David pierre-yves.david at ens-lyon.org
Sat Jun 3 19:29:55 EDT 2017



On 06/04/2017 01:07 AM, Augie Fackler wrote:
>
>> On Jun 3, 2017, at 5:06 PM, Jun Wu <quark at fb.com> wrote:
>>
>> # HG changeset patch
>> # User Jun Wu <quark at fb.com>
>> # Date 1496515319 25200
>> #      Sat Jun 03 11:41:59 2017 -0700
>> # Node ID a5f77662c4f22467b84fc3ce494998d23b0daa82
>> # Parent  783394c0c97807e83daad9da561179bd0719e159
>> # Available At https://bitbucket.org/quark-zju/hg-draft
>> #              hg pull https://bitbucket.org/quark-zju/hg-draft -r a5f77662c4f2
>> smartset: add a "toset" method
>>
>> This allows us to convert a smartset to a Python set using a customized
>> approach.  Namely, baseset may have a "_set" property already which could be
>> used directly to avoid __iter__ overhead.
>>
>> diff --git a/mercurial/smartset.py b/mercurial/smartset.py
>> --- a/mercurial/smartset.py
>> +++ b/mercurial/smartset.py
>> @@ -156,4 +156,8 @@ class abstractsmartset(object):
>>         return filteredset(self, condition, condrepr)
>>
>> +    def toset(self):
>> +        """Convert to unordered Python set"""
>> +        return set(self)
>> +
>> class baseset(abstractsmartset):
>>     """Basic data structure that represents a revset and contains the basic
>> @@ -169,5 +173,6 @@ class baseset(abstractsmartset):
>>
>>     Construct by a set:
>> -    >>> xs = baseset(set(x))
>> +    >>> xset = set(x)
>> +    >>> xs = baseset(xset)
>>>>> ys = baseset(set(y))
>>>>> [list(i) for i in [xs + ys, xs & ys, xs - ys]]
>> @@ -175,4 +180,6 @@ class baseset(abstractsmartset):
>>>>> [type(i).__name__ for i in [xs + ys, xs & ys, xs - ys]]
>>     ['addset', 'baseset', 'baseset']
>> +    >>> xset is xs.toset()
>> +    True
>>
>>     Construct by a list-like:
>> @@ -237,4 +244,7 @@ class baseset(abstractsmartset):
>>         return set(self._list)
>>
>> +    def toset(self):
>> +        return self._set
>
> It freaks me out just a little (maybe too much Rust today?) to leak self._set mutably like this. What do you think of making a copy? Should we just strongly admonish in the “convert to a set” docstring that callers of toset() _must not_ mutate the returned set?

Note: For the target code, copying sets is can be multiple percent of 
the total run time.

-- 
Pierre-Yves David


More information about the Mercurial-devel mailing list