Patchwork [1,of,2] lazymanifest: fix pure hg iterkeys()

login
register
mail settings
Submitter Martin von Zweigbergk
Date March 13, 2015, 2:26 a.m.
Message ID <5d8bfcd6e7bb3ccbc60f.1426213569@martinvonz.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/8040/
State Superseded
Headers show

Comments

Martin von Zweigbergk - March 13, 2015, 2:26 a.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1426211624 25200
#      Thu Mar 12 18:53:44 2015 -0700
# Node ID 5d8bfcd6e7bb3ccbc60f23344b79dc4bff2e9361
# Parent  2b7ab29627fd93ca7f5cb838403c2f6c728469bd
lazymanifest: fix pure hg iterkeys()

I broke pure hg when I just added iterkeys() to the native version in
2b7ab29627fd. I forgot to make the pure version sorted. Fix it.
Siddharth Agarwal - March 13, 2015, 3:03 a.m.
On 03/12/2015 07:26 PM, Martin von Zweigbergk wrote:
> +    def iterkeys(self):
> +        return (f for f in sorted(dict.iterkeys(self)))
> +

Maybe better written as

keys = self.keys()
keys.sort()
return iter(keys)
Martin von Zweigbergk - March 13, 2015, 3:51 a.m.
On Thu, Mar 12, 2015 at 8:04 PM Siddharth Agarwal <sid@less-broken.com>
wrote:

> On 03/12/2015 07:26 PM, Martin von Zweigbergk wrote:
> > +    def iterkeys(self):
> > +        return (f for f in sorted(dict.iterkeys(self)))
> > +
>
> Maybe better written as
>
> keys = self.keys()
> keys.sort()
> return iter(keys)
>

By "better", you mean faster? Yes, that seems faster:

$ python -m timeit -s 'd = dict([(i,None) for i in xrange(10000)])'
'keys = d.keys(); keys.sort(); list(iter(keys))'

1000 loops, best of 3: 246 usec per loop

$ python -m timeit -s 'd = dict([(i,None) for i in xrange(10000)])'
'list((f for f in sorted(d.iterkeys())))'
1000 loops, best of 3: 609 usec per loop


Even faster (and less code):

$ python -m timeit -s 'd = dict([(i,None) for i in xrange(10000)])'
'iter(sorted(d.keys()))'
1000 loops, best of 3: 209 usec per loop


Thanks! I somehow didn't think about iter(). I'll resend with that form.

Patch

diff -r 2b7ab29627fd -r 5d8bfcd6e7bb mercurial/manifest.py
--- a/mercurial/manifest.py	Wed Mar 11 13:46:15 2015 -0700
+++ b/mercurial/manifest.py	Thu Mar 12 18:53:44 2015 -0700
@@ -46,6 +46,9 @@ 
     def __iter__(self):
         return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
 
+    def iterkeys(self):
+        return (f for f in sorted(dict.iterkeys(self)))
+
     def copy(self):
         c = _lazymanifest('')
         c.update(self)