Make extended_cp_match() cache regular expressions for re-use.
authorZac Medico <zmedico@gentoo.org>
Sun, 25 Jul 2010 19:11:19 +0000 (12:11 -0700)
committerZac Medico <zmedico@gentoo.org>
Sun, 25 Jul 2010 19:11:19 +0000 (12:11 -0700)
pym/portage/dep/__init__.py

index 80e2b4c7870609a3473a5436036cd76f7b31e593..9720a510e609a4b85fb8593d7540f5e92d31c0fe 100644 (file)
@@ -754,14 +754,20 @@ class Atom(_atom_base):
                memo[id(self)] = self
                return self
 
+_extended_cp_re_cache = {}
+
 def extended_cp_match(extended_cp, other_cp):
        """
        Checks if an extended syntax cp matches a non extended cp
        """
        # Escape special '+' and '.' characters which are allowed in atoms,
        # and convert '*' to regex equivalent.
-       extended_cp_re = re.compile("^" + extended_cp.replace("+", r"\+").replace(
-               ".", r"\.").replace('*', '[^/]*') + "$")
+       global _extended_cp_re_cache
+       extended_cp_re = _extended_cp_re_cache.get(extended_cp)
+       if extended_cp_re is None:
+               extended_cp_re = re.compile("^" + extended_cp.replace(
+                       "+", r"\+").replace(".", r"\.").replace('*', '[^/]*') + "$")
+               _extended_cp_re_cache[extended_cp] = extended_cp_re
        return extended_cp_re.match(other_cp) is not None
 
 class ExtendedAtomDict(object):