summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/annotation.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-03-09 17:12:35 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-04-01 16:12:23 -0400
commita9b62055bfa61c11e9fe0b2984437e2c3e32bf0e (patch)
tree366027c7069edd56d49e9d540ae6a14fbe9e16fe /lib/sqlalchemy/sql/annotation.py
parente6250123a30e457068878394e49b7ca07ca4d3b0 (diff)
downloadsqlalchemy-a9b62055bfa61c11e9fe0b2984437e2c3e32bf0e.tar.gz
Try to measure new style caching in the ORM, take two
Supercedes: If78fbb557c6f2cae637799c3fec2cbc5ac248aaf Trying to see if by making the cache key memoized, we still can have the older "identity" form of caching which is the cheapest of all, at the same time as the newer "cache key each time" version that is not nearly as cheap; but still much cheaper than no caching at all. Also needed is a per-execution update of _keymap when we invoke from a cached select, so that Column objects that are anonymous or otherwise adapted will match up. this is analogous to the adaption of bound parameters from the cache key. Adds test coverage for the keymap / construct_params() changes related to caching. Also hones performance to a large extent for statement construction and cache key generation. Also includes a new memoized attribute approach that vastly simplifies the previous approach of "group_expirable_memoized_property" and finally integrates cleanly with _clone(), _generate(), etc. no more hardcoding of attributes is needed, as well as that most _reset_memoization() calls are no longer needed as the reset is inherent in a _generate() call; this also has dramatic performance improvements. Change-Id: I95c560ffcbfa30b26644999412fb6a385125f663
Diffstat (limited to 'lib/sqlalchemy/sql/annotation.py')
-rw-r--r--lib/sqlalchemy/sql/annotation.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/annotation.py b/lib/sqlalchemy/sql/annotation.py
index 7984dc7ea..d895e730c 100644
--- a/lib/sqlalchemy/sql/annotation.py
+++ b/lib/sqlalchemy/sql/annotation.py
@@ -13,6 +13,7 @@ associations.
from . import operators
from .base import HasCacheKey
+from .traversals import anon_map
from .visitors import InternalTraversal
from .. import util
@@ -20,12 +21,13 @@ from .. import util
class SupportsAnnotations(object):
@util.memoized_property
def _annotations_cache_key(self):
+ anon_map_ = anon_map()
return (
"_annotations",
tuple(
(
key,
- value._gen_cache_key(None, [])
+ value._gen_cache_key(anon_map_, [])
if isinstance(value, HasCacheKey)
else value,
)
@@ -38,7 +40,7 @@ class SupportsCloneAnnotations(SupportsAnnotations):
_annotations = util.immutabledict()
_clone_annotations_traverse_internals = [
- ("_annotations_cache_key", InternalTraversal.dp_plain_obj)
+ ("_annotations", InternalTraversal.dp_annotations_key)
]
def _annotate(self, values):
@@ -133,6 +135,8 @@ class Annotated(object):
"""
+ _is_column_operators = False
+
def __new__(cls, *args):
if not args:
# clone constructor
@@ -200,7 +204,7 @@ class Annotated(object):
return self._hash
def __eq__(self, other):
- if isinstance(self.__element, operators.ColumnOperators):
+ if self._is_column_operators:
return self.__element.__class__.__eq__(self, other)
else:
return hash(other) == hash(self)
@@ -208,7 +212,9 @@ class Annotated(object):
# hard-generate Annotated subclasses. this technique
# is used instead of on-the-fly types (i.e. type.__new__())
-# so that the resulting objects are pickleable.
+# so that the resulting objects are pickleable; additionally, other
+# decisions can be made up front about the type of object being annotated
+# just once per class rather than per-instance.
annotated_classes = {}
@@ -310,8 +316,11 @@ def _new_annotation_type(cls, base_cls):
if "_traverse_internals" in cls.__dict__:
anno_cls._traverse_internals = list(cls._traverse_internals) + [
- ("_annotations_cache_key", InternalTraversal.dp_plain_obj)
+ ("_annotations", InternalTraversal.dp_annotations_key)
]
+
+ anno_cls._is_column_operators = issubclass(cls, operators.ColumnOperators)
+
return anno_cls