]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Cache polymorphic_iterator in UOWTask; substantial savings for polymorphism-heavy...
authorJason Kirtland <jek@discorporate.us>
Fri, 17 Oct 2008 19:19:05 +0000 (19:19 +0000)
committerJason Kirtland <jek@discorporate.us>
Fri, 17 Oct 2008 19:19:05 +0000 (19:19 +0000)
lib/sqlalchemy/orm/unitofwork.py
lib/sqlalchemy/util.py

index 8412887a45b46d27db818f14115d71938436865c..52bf1578e40db15edc021ce96fc4557775ee4ead 100644 (file)
@@ -342,6 +342,10 @@ class UOWTask(object):
         self.dependencies = set()
         self.cyclical_dependencies = set()
 
+    @util.lazy_property
+    def inheriting_mappers(self):
+        return list(self.mapper.polymorphic_iterator())
+
     @property
     def polymorphic_tasks(self):
         """Return an iterator of UOWTask objects corresponding to the
@@ -372,7 +376,7 @@ class UOWTask(object):
         those mappers.
 
         """
-        for mapper in self.mapper.polymorphic_iterator():
+        for mapper in self.inheriting_mappers:
             t = self.base_task._inheriting_tasks.get(mapper, None)
             if t is not None:
                 yield t
index 88245945e232accf6bf51f5c7e0a3f26e47b1476..770b48796c7981bc6743fdf6dc43d248c75807fa 100644 (file)
@@ -395,6 +395,19 @@ def iterate_attributes(cls):
                 yield (key, c.__dict__[key])
                 break
 
+class lazy_property(object):
+    """A read-only @property that is only evaluated once."""
+    def __init__(self, fget, doc=None):
+        self.fget = fget
+        self.__doc__ = doc or fget.__doc__
+        self.__name__ = fget.__name__
+
+    def __get__(self, obj, cls):
+        if obj is None:
+            return None
+        obj.__dict__[self.__name__] = result = self.fget(obj)
+        return result
+
 # from paste.deploy.converters
 def asbool(obj):
     if isinstance(obj, (str, unicode)):