summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/coercions.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-05-25 22:36:44 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-05-28 14:38:56 -0400
commit77f1b7d236dba6b1c859bb428ef32d118ec372e6 (patch)
tree7fae8eaaf303d6ce02bd423abf216550001e2f7b /lib/sqlalchemy/sql/coercions.py
parent366e88ea0e5c5417184c1dd4776cff752560631d (diff)
downloadsqlalchemy-77f1b7d236dba6b1c859bb428ef32d118ec372e6.tar.gz
callcount reductions and refinement for cached queries
This commit includes that we've removed the "_orm_query" attribute from compile state as well as query context. The attribute created reference cycles and also added method call overhead. As part of this change, the interface for ORMExecuteState changes a bit, as well as the interface for the horizontal sharding extension which now deprecates the "query_chooser" callable in favor of "execute_chooser", which receives the contextual object. This will also work more nicely when we implement the new execution path for bulk updates and deletes. Pre-merge execution options for statement, connection, arguments all up front in Connection. that way they can be passed to the before_execute / after_execute events, and the ExecutionContext doesn't have to merge as second time. Core execute is pretty close to 1.3 now. baked wasn't using the new one()/first()/one_or_none() methods, fixed that. Convert non-buffered cursor strategy to be a stateless singleton. inline all the paths by which the strategy gets chosen, oracle and SQL Server dialects make use of the already-invoked post_exec() hook to establish the alternate strategies, and this is actually much nicer than it was before. Add caching to mapper instance processor for getters. Identified a reference cycle per query that was showing up as a lot of gc cleanup, fixed that. After all that, performance not budging much. Even test_baked_query now runs with significantly fewer function calls than 1.3, still 40% slower. Basically something about the new patterns just makes this slower and while I've walked a whole bunch of them back, it hardly makes a dent. that said, the performance issues are relatively small, in the 20-40% time increase range, and the new caching feature does provide for regular ORM and Core queries that are cached, and they are faster than non-cached. Change-Id: I7b0b0d8ca550c05f79e82f75cd8eff0bbfade053
Diffstat (limited to 'lib/sqlalchemy/sql/coercions.py')
-rw-r--r--lib/sqlalchemy/sql/coercions.py91
1 files changed, 44 insertions, 47 deletions
diff --git a/lib/sqlalchemy/sql/coercions.py b/lib/sqlalchemy/sql/coercions.py
index d8ef0222a..7503faf5b 100644
--- a/lib/sqlalchemy/sql/coercions.py
+++ b/lib/sqlalchemy/sql/coercions.py
@@ -50,16 +50,50 @@ def _document_text_coercion(paramname, meth_rst, param_rst):
)
-def expect(role, element, apply_propagate_attrs=None, **kw):
+def expect(role, element, apply_propagate_attrs=None, argname=None, **kw):
# major case is that we are given a ClauseElement already, skip more
# elaborate logic up front if possible
impl = _impl_lookup[role]
+ original_element = element
+
if not isinstance(
element,
(elements.ClauseElement, schema.SchemaItem, schema.FetchedValue),
):
- resolved = impl._resolve_for_clause_element(element, **kw)
+ resolved = None
+
+ if impl._resolve_string_only:
+ resolved = impl._literal_coercion(element, **kw)
+ else:
+
+ original_element = element
+
+ is_clause_element = False
+
+ while hasattr(element, "__clause_element__"):
+ is_clause_element = True
+ if not getattr(element, "is_clause_element", False):
+ element = element.__clause_element__()
+ else:
+ break
+
+ if not is_clause_element:
+ if impl._use_inspection:
+ insp = inspection.inspect(element, raiseerr=False)
+ if insp is not None:
+ insp._post_inspect
+ try:
+ resolved = insp.__clause_element__()
+ except AttributeError:
+ impl._raise_for_expected(original_element, argname)
+
+ if resolved is None:
+ resolved = impl._literal_coercion(
+ element, argname=argname, **kw
+ )
+ else:
+ resolved = element
else:
resolved = element
@@ -72,10 +106,12 @@ def expect(role, element, apply_propagate_attrs=None, **kw):
if impl._role_class in resolved.__class__.__mro__:
if impl._post_coercion:
- resolved = impl._post_coercion(resolved, **kw)
+ resolved = impl._post_coercion(resolved, argname=argname, **kw)
return resolved
else:
- return impl._implicit_coercions(element, resolved, **kw)
+ return impl._implicit_coercions(
+ original_element, resolved, argname=argname, **kw
+ )
def expect_as_key(role, element, **kw):
@@ -107,51 +143,13 @@ class RoleImpl(object):
raise NotImplementedError()
_post_coercion = None
+ _resolve_string_only = False
def __init__(self, role_class):
self._role_class = role_class
self.name = role_class._role_name
self._use_inspection = issubclass(role_class, roles.UsesInspection)
- def _resolve_for_clause_element(self, element, argname=None, **kw):
- original_element = element
-
- is_clause_element = False
-
- while hasattr(element, "__clause_element__"):
- is_clause_element = True
- if not getattr(element, "is_clause_element", False):
- element = element.__clause_element__()
- else:
- return element
-
- if not is_clause_element:
- if self._use_inspection:
- insp = inspection.inspect(element, raiseerr=False)
- if insp is not None:
- insp._post_inspect
- try:
- element = insp.__clause_element__()
- except AttributeError:
- self._raise_for_expected(original_element, argname)
- else:
- return element
-
- return self._literal_coercion(element, argname=argname, **kw)
- else:
- return element
-
- if self._use_inspection:
- insp = inspection.inspect(element, raiseerr=False)
- if insp is not None:
- insp._post_inspect
- try:
- element = insp.__clause_element__()
- except AttributeError:
- self._raise_for_expected(original_element, argname)
-
- return self._literal_coercion(element, argname=argname, **kw)
-
def _implicit_coercions(self, element, resolved, argname=None, **kw):
self._raise_for_expected(element, argname, resolved)
@@ -191,8 +189,7 @@ class _Deannotate(object):
class _StringOnly(object):
__slots__ = ()
- def _resolve_for_clause_element(self, element, argname=None, **kw):
- return self._literal_coercion(element, **kw)
+ _resolve_string_only = True
class _ReturnsStringKey(object):
@@ -465,7 +462,7 @@ class ByOfImpl(_CoerceLiterals, _ColumnCoercions, RoleImpl, roles.ByOfRole):
class OrderByImpl(ByOfImpl, RoleImpl):
__slots__ = ()
- def _post_coercion(self, resolved):
+ def _post_coercion(self, resolved, **kw):
if (
isinstance(resolved, self._role_class)
and resolved._order_by_label_element is not None
@@ -490,7 +487,7 @@ class GroupByImpl(ByOfImpl, RoleImpl):
class DMLColumnImpl(_ReturnsStringKey, RoleImpl):
__slots__ = ()
- def _post_coercion(self, element, as_key=False):
+ def _post_coercion(self, element, as_key=False, **kw):
if as_key:
return element.key
else: