[Yum-devel] [PATCH 1/2] Lazily load cElementTree, saves about 10% of "import yum" time.

James Antill james at and.org
Wed Jun 2 20:32:14 UTC 2010


---
 yum/comps.py        |    6 +-----
 yum/metalink.py     |    6 +-----
 yum/misc.py         |   20 ++++++++++++++++++++
 yum/repoMDObject.py |    6 +-----
 yum/update_md.py    |    8 +-------
 5 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/yum/comps.py b/yum/comps.py
index bf070fa..5ccfba2 100755
--- a/yum/comps.py
+++ b/yum/comps.py
@@ -16,11 +16,6 @@
 import types
 import sys
 from constants import *
-try:
-    from xml.etree import cElementTree
-except ImportError:
-    import cElementTree
-iterparse = cElementTree.iterparse
 from Errors import CompsException
 #FIXME - compsexception isn't caught ANYWHERE so it's pointless to raise it
 # switch all compsexceptions to grouperrors after api break
@@ -28,6 +23,7 @@ import fnmatch
 import re
 from yum.i18n import to_unicode
 from misc import get_my_lang_code
+from yum.misc import cElementTree_iterparse as iterparse 
 
 lang_attr = '{http://www.w3.org/XML/1998/namespace}lang'
 
diff --git a/yum/metalink.py b/yum/metalink.py
index 24da633..20f0ea5 100755
--- a/yum/metalink.py
+++ b/yum/metalink.py
@@ -26,11 +26,7 @@ from urlgrabber.progress import format_number
 
 import Errors
 
-try:
-    from xml.etree import cElementTree
-except ImportError:
-    import cElementTree
-xmlparse = cElementTree.parse
+from yum.misc import cElementTree_xmlparse as xmlparse
 
 class MetaLinkRepoErrorParseFail(Errors.RepoError):
     """ An exception thrown for an unparsable MetaLinkRepoMD file. """
diff --git a/yum/misc.py b/yum/misc.py
index 7793807..b565ab1 100644
--- a/yum/misc.py
+++ b/yum/misc.py
@@ -1064,4 +1064,24 @@ def read_in_items_from_dot_dir(thisglob, line_as_list=True):
             results.append(line)
     return results
 
+__cached_cElementTree = None
+def _cElementTree_import():
+    """ Importing xElementTree all the time, when we often don't need it, is a
+        huge timesink. This makes python -c 'import yum' suck. So we hide it
+        behind this function. And have accessors. """
+    global __cached_cElementTree
+    if __cached_cElementTree is None:
+        try:
+            from xml.etree import cElementTree
+        except ImportError:
+            import cElementTree
+        __cached_cElementTree = cElementTree
+
+def cElementTree_iterparse(filename):
+    """ Lazily load/run: cElementTree.iterparse """
+    _cElementTree_import()
+    return __cached_cElementTree.iterparse(filename)
 
+def cElementTree_xmlparse(filename):
+    """ Lazily load/run: cElementTree.xmlparse """
+    return __cached_cElementTree.parse(filename)
diff --git a/yum/repoMDObject.py b/yum/repoMDObject.py
index 7a4593e..afc7947 100755
--- a/yum/repoMDObject.py
+++ b/yum/repoMDObject.py
@@ -14,11 +14,7 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # Copyright 2006 Duke University
 
-try:
-    from xml.etree import cElementTree
-except ImportError:
-    import cElementTree
-iterparse = cElementTree.iterparse
+from yum.misc import cElementTree_iterparse as iterparse 
 from Errors import RepoMDError
 
 import sys
diff --git a/yum/update_md.py b/yum/update_md.py
index cf8136a..9e492ba 100644
--- a/yum/update_md.py
+++ b/yum/update_md.py
@@ -27,17 +27,11 @@ from yum.i18n import utf8_text_wrap, to_utf8
 from yum.yumRepo import YumRepository
 from yum.packages import FakeRepository
 from yum.misc import to_xml, decompress
+from yum.misc import cElementTree_iterparse as iterparse 
 import Errors
 
 import rpmUtils.miscutils
 
-try:
-    from xml.etree import cElementTree
-except ImportError:
-    import cElementTree
-iterparse = cElementTree.iterparse
-
-
 class UpdateNoticeException(Exception):
     """ An exception thrown for bad UpdateNotice data. """
     pass
-- 
1.7.0.1



More information about the Yum-devel mailing list