summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2008-06-20 16:18:46 +0000
committerZac Medico <zmedico@gentoo.org>2008-06-20 16:18:46 +0000
commit89a06fc9d0ab02ace3f8847da2357c5c6f9794a1 (patch)
tree5a5caebb79319d983f14a63eac719bbe604a868b
parentInstead of having Atom inherit from str, just emulate the interface. This (diff)
downloadportage-idfetch-89a06fc9d0ab02ace3f8847da2357c5c6f9794a1.tar.gz
portage-idfetch-89a06fc9d0ab02ace3f8847da2357c5c6f9794a1.tar.bz2
portage-idfetch-89a06fc9d0ab02ace3f8847da2357c5c6f9794a1.zip
Use a metaclass to cache Atom instances transparently. This should improve
performance and conserve memory in cases when the same atom is more than once. svn path=/main/trunk/; revision=10741
-rw-r--r--pym/portage/dep.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/pym/portage/dep.py b/pym/portage/dep.py
index 03f42849..a1908276 100644
--- a/pym/portage/dep.py
+++ b/pym/portage/dep.py
@@ -392,6 +392,15 @@ class _use_dep(object):
tokens.extend(self.conditional_disabled.difference(use))
return _use_dep(tokens)
+class _AtomCache(type):
+ atoms = {}
+ def __call__(cls, s):
+ instance = cls.atoms.get(s)
+ if instance is None:
+ instance = super(_AtomCache, cls).__call__(s)
+ cls.atoms[s] = instance
+ return instance
+
class Atom(object):
"""
@@ -399,6 +408,8 @@ class Atom(object):
class emulates most of the str methods that are useful with atoms.
"""
+ __metaclass__ = _AtomCache
+
_str_methods = ("endswith", "find", "index", "lstrip", "replace",
"startswith", "strip", "rindex", "rfind", "rstrip", "__getitem__",
"__len__", "__repr__", "__str__")