[PATCH] lazy import of ElementTree

John A Meinel john at arbash-meinel.com
Mon Jun 27 02:46:49 BST 2005


I was doing some testing, and I found that on my machine 'time bzr root'
takes 0.55 seconds, which seems like a lot.

I tracked it down to find that:
time python -c 'from cElementTree import ElementTree, Element'
takes 0.3 seconds and
time python -c 'from elementtree.ElementTree import ElementTree, Element'
takes 0.28 seconds.

So the attached patch refactors how importing ElementTree works.
Basically, I route all imports through xml.py (which should have been
done anyway), and then I create a wrapper function, which does a lazy
import of ElementTree.

It means that you cannot subclass from ElementTree/Element, but I
haven't seen anywhere that we do that (or even if we want to).

I'm slowly going through the rest of the code, trying to find places
where we can be a little bit lazier about importing.
But just this change by itself improves:
time ./bzr root
to 0.34 seconds

Which is an improvement of 0.2 seconds, almost half of the load time.

Admittedly, for most real functionality, you have to load ElementTree.
But it does help the impression that something that should be fast, is.
(baz tree-root only takes 0.011s, over 50x faster).

John
=:->

-------------- next part --------------
A non-text attachment was scrubbed...
Name: lazy-elementtree-import.patch
Type: text/x-patch
Size: 3298 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050626/c8179f25/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 253 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050626/c8179f25/attachment.pgp 


More information about the bazaar mailing list