Patchwork [2,of,5] manifest: switch add() to heapq.merge (available in Py2.6+)

login
register
mail settings
Submitter timeless@mozdev.org
Date Sept. 9, 2015, 1 a.m.
Message ID <143d8109b4e6e646faad.1441760458@waste.org>
Download mbox | patch
Permalink /patch/10426/
State Accepted
Headers show

Comments

timeless@mozdev.org - Sept. 9, 2015, 1 a.m.
# HG changeset patch
# User timeless@mozdev.org
# Date 1441360678 14400
#      Fri Sep 04 05:57:58 2015 -0400
# Node ID 143d8109b4e6e646faad6d6ebae0916025fd88c6
# Parent  86cde82f7a356f189678df401831646e32499522
manifest: switch add() to heapq.merge (available in Py2.6+)
Yuya Nishihara - Sept. 9, 2015, 2:51 p.m.
On Tue, 08 Sep 2015 20:00:58 -0500, timeless@mozdev.org wrote:
> # HG changeset patch
> # User timeless@mozdev.org
> # Date 1441360678 14400
> #      Fri Sep 04 05:57:58 2015 -0400
> # Node ID 143d8109b4e6e646faad6d6ebae0916025fd88c6
> # Parent  86cde82f7a356f189678df401831646e32499522
> manifest: switch add() to heapq.merge (available in Py2.6+)
> 
> diff --git a/mercurial/manifest.py b/mercurial/manifest.py
> --- a/mercurial/manifest.py
> +++ b/mercurial/manifest.py
> @@ -9,6 +9,7 @@
>  import mdiff, parsers, error, revlog, util
>  import array, struct
>  import os
> +import heapq
>  
>  propertycache = util.propertycache
>  
> @@ -970,12 +971,8 @@
>              # revlog layer.
>  
>              _checkforbidden(added)
> -            # combine the changed lists into one list for sorting
> -            work = [(x, False) for x in added]
> -            work.extend((x, True) for x in removed)
> -            # this could use heapq.merge() (from Python 2.6+) or equivalent
> -            # since the lists are already sorted
> -            work.sort()
> +            # combine the changed lists into one sorted list
> +            work = heapq.merge([(x, False) for x in added], [(x, True) for x in removed])

Nit: heapq.merge() returns an iterator, so the comment "into one sorted list"
seems misleading.
Augie Fackler - Sept. 9, 2015, 2:56 p.m.
On Wed, Sep 9, 2015 at 10:51 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> On Tue, 08 Sep 2015 20:00:58 -0500, timeless@mozdev.org wrote:
>> # HG changeset patch
>> # User timeless@mozdev.org
>> # Date 1441360678 14400
>> #      Fri Sep 04 05:57:58 2015 -0400
>> # Node ID 143d8109b4e6e646faad6d6ebae0916025fd88c6
>> # Parent  86cde82f7a356f189678df401831646e32499522
>> manifest: switch add() to heapq.merge (available in Py2.6+)
>>
>> diff --git a/mercurial/manifest.py b/mercurial/manifest.py
>> --- a/mercurial/manifest.py
>> +++ b/mercurial/manifest.py
>> @@ -9,6 +9,7 @@
>>  import mdiff, parsers, error, revlog, util
>>  import array, struct
>>  import os
>> +import heapq
>>
>>  propertycache = util.propertycache
>>
>> @@ -970,12 +971,8 @@
>>              # revlog layer.
>>
>>              _checkforbidden(added)
>> -            # combine the changed lists into one list for sorting
>> -            work = [(x, False) for x in added]
>> -            work.extend((x, True) for x in removed)
>> -            # this could use heapq.merge() (from Python 2.6+) or equivalent
>> -            # since the lists are already sorted
>> -            work.sort()
>> +            # combine the changed lists into one sorted list
>> +            work = heapq.merge([(x, False) for x in added], [(x, True) for x in removed])
>
> Nit: heapq.merge() returns an iterator, so the comment "into one sorted list"
> seems misleading.

Good point. Fixing in flight, as well as a minor check-code violation.

> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/manifest.py b/mercurial/manifest.py
--- a/mercurial/manifest.py
+++ b/mercurial/manifest.py
@@ -9,6 +9,7 @@ 
 import mdiff, parsers, error, revlog, util
 import array, struct
 import os
+import heapq
 
 propertycache = util.propertycache
 
@@ -970,12 +971,8 @@ 
             # revlog layer.
 
             _checkforbidden(added)
-            # combine the changed lists into one list for sorting
-            work = [(x, False) for x in added]
-            work.extend((x, True) for x in removed)
-            # this could use heapq.merge() (from Python 2.6+) or equivalent
-            # since the lists are already sorted
-            work.sort()
+            # combine the changed lists into one sorted list
+            work = heapq.merge([(x, False) for x in added], [(x, True) for x in removed])
 
             arraytext, deltatext = m.fastdelta(self._mancache[p1][1], work)
             cachedelta = self.rev(p1), deltatext