[PATCH] py3: handling .iteritems() vs .items()
Pulkit Goyal
7895pulkit at gmail.com
Mon Jun 6 15:08:14 EDT 2016
# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1465236376 -19800
# Mon Jun 06 23:36:16 2016 +0530
# Node ID 100a033a0ac8c003aaf3477b6abb6bb79745ba90
# Parent a881a47f98e8306443c93b7d7cc8660d01c6253a
py3: handling .iteritems() vs .items()
Using dict.items() instead of dict.iteritems() in
py2 just for py3 compatibility is not a good idea because
in py2 dict.items() returns a copy of dictionary's list of
pairs. This will take a lot of memory when dictionary is
large.
So it will be good to use .iteritems() in py2, because .viewitems()
is not available in py2.6, and .items() in py3 as rest are gone. The .items()
in py3 has an improved implementation.
Importing from the util module adds up the call cost which can be
mitigated by using methodcaller. This approach can be used for
mercurial/* and hgext/* modules.
In contrib we have to import mercurial/util.py, that will be costly
so handling it separately will be better. Also using .items() will
be costly in contrib. So we have to add this hack everywhere or find
another way out.
I will like to get more advice on this as to what can be improvised
in this approach and how can we handle the contrib section separately.
diff -r a881a47f98e8 -r 100a033a0ac8 mercurial/util.py
--- a/mercurial/util.py Mon May 30 00:57:41 2016 +0530
+++ b/mercurial/util.py Mon Jun 06 23:36:16 2016 +0530
@@ -23,6 +23,7 @@
import gc
import hashlib
import imp
+import operator
import os
import re as remod
import shutil
@@ -57,12 +58,19 @@
# This line is to make pyflakes happy:
urlreq = pycompat.urlreq
+methodcaller = operator.methodcaller
if os.name == 'nt':
from . import windows as platform
else:
from . import posix as platform
+try:
+ dict.iteritems
+ dictitems = methodcaller('iteritems')
+except AttributeError:
+ dictitems = methodcaller('items')
+
md5 = hashlib.md5
sha1 = hashlib.sha1
sha512 = hashlib.sha512
More information about the Mercurial-devel
mailing list