From 89bf6d80a999eb31ee4a69b229b887fbfb2ed12a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 14 Dec 2019 11:39:06 -0500 Subject: Traversal and clause generation performance improvements Added one traversal test, callcounts have been brought from 29754 to 5173 so far. Change-Id: I164e9831600709ee214c1379bb215fdad73b39aa --- lib/sqlalchemy/sql/elements.py | 47 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'lib/sqlalchemy/sql/elements.py') diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index eda31dc61..da7568330 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -198,12 +198,7 @@ class ClauseElement( _order_by_label_element = None - @property - def _cache_key_traversal(self): - try: - return self._traverse_internals - except AttributeError: - return NO_CACHE + _cache_key_traversal = None def _clone(self): """Create a shallow copy of this ClauseElement. @@ -1344,16 +1339,21 @@ class BindParameter(roles.InElementRole, ColumnElement): return c def _gen_cache_key(self, anon_map, bindparams): - if self in anon_map: - return (anon_map[self], self.__class__) + idself = id(self) + if idself in anon_map: + return (anon_map[idself], self.__class__) + else: + # inline of + # id_ = anon_map[idself] + anon_map[idself] = id_ = str(anon_map.index) + anon_map.index += 1 - id_ = anon_map[self] bindparams.append(self) return ( id_, self.__class__, - self.type._gen_cache_key, + self.type._static_cache_key, traversals._resolve_name_for_compare(self, self.key, anon_map), ) @@ -3239,6 +3239,33 @@ class BinaryExpression(ColumnElement): """ + def _gen_cache_key(self, anon_map, bindparams): + # inlined for performance + + idself = id(self) + + if idself in anon_map: + return (anon_map[idself], self.__class__) + else: + # inline of + # id_ = anon_map[idself] + anon_map[idself] = id_ = str(anon_map.index) + anon_map.index += 1 + + if self._cache_key_traversal is NO_CACHE: + anon_map[NO_CACHE] = True + return None + + result = (id_, self.__class__) + + return result + ( + ("left", self.left._gen_cache_key(anon_map, bindparams)), + ("right", self.right._gen_cache_key(anon_map, bindparams)), + ("operator", self.operator), + ("negate", self.negate), + ("modifiers", self.modifiers), + ) + def __init__( self, left, right, operator, type_=None, negate=None, modifiers=None ): -- cgit v1.2.1