[PATCH] mq: refactor usage of repo.branchmap().iteritems() with itervalues()

Brodie Rao brodie at sf.io
Wed Sep 25 17:43:05 CDT 2013


On Wed, Sep 25, 2013 at 2:49 PM, Kevin Bullock
<kbullock+mercurial at ringworld.org> wrote:
> On 25 Sep 2013, at 4:38 PM, Brodie Rao wrote:
>
>> # HG changeset patch
>> # User Brodie Rao <brodie at sf.io>
>> # Date 1364871676 25200
>> #      Mon Apr 01 20:01:16 2013 -0700
>> # Node ID 5ddfc6e253794f76691b27f805b1275d54708298
>> # Parent  50d721553198cea51c30f53b76d41dc919280097
>> mq: refactor usage of repo.branchmap().iteritems() with itervalues()
>>
>> diff --git a/hgext/mq.py b/hgext/mq.py
>> --- a/hgext/mq.py
>> +++ b/hgext/mq.py
>> @@ -1222,9 +1222,7 @@ class queue(object):
>>         diffopts = self.diffopts()
>>         wlock = repo.wlock()
>>         try:
>> -            heads = []
>> -            for b, ls in repo.branchmap().iteritems():
>> -                heads += ls
>> +            heads = [h for hs in repo.branchmap().itervalues() for h in hs]
>
> The double-`for` is really confusing to me here.

If you insert some imaginary punctuation, it might make more sense:

[h <- for hs in repo.branchmap().itervalues(): for h in hs:]

That said, I just realized this could theoretically make the code
microscopically slower:

>>> timeit.timeit('things = [n for ns in stuff.itervalues() for n in ns]', 'stuff = {n: range(10 * n) for n in range(10)}') / 1000000.0
2.6555580139160157e-05

>>> timeit.timeit('things = []\nfor ns in stuff.itervalues():\n    things += ns\n', 'stuff = {n: range(10 * n) for n in range(10)}') / 1000000.0
4.299727916717529e-06

I guess the latter is faster because the final list ends up doing less
resizing as it's added to. I'm not sure if the performance difference
really matters though.


More information about the Mercurial-devel mailing list