mirror of https://github.com/django/django.git
Refs #35102 -- Optimized replace_expressions()/relabelling aliases by adding early return.
This avoids costly hashing. Thanks Anthony Shaw for the report. Co-Authored-By: Simon Charette <charette.s@gmail.com>
This commit is contained in:
parent
d074c7530b
commit
f3d10546a8
|
@ -402,10 +402,13 @@ class BaseExpression:
|
||||||
return clone
|
return clone
|
||||||
|
|
||||||
def replace_expressions(self, replacements):
|
def replace_expressions(self, replacements):
|
||||||
|
if not replacements:
|
||||||
|
return self
|
||||||
if replacement := replacements.get(self):
|
if replacement := replacements.get(self):
|
||||||
return replacement
|
return replacement
|
||||||
|
if not (source_expressions := self.get_source_expressions()):
|
||||||
|
return self
|
||||||
clone = self.copy()
|
clone = self.copy()
|
||||||
source_expressions = clone.get_source_expressions()
|
|
||||||
clone.set_source_expressions(
|
clone.set_source_expressions(
|
||||||
[
|
[
|
||||||
expr.replace_expressions(replacements) if expr else None
|
expr.replace_expressions(replacements) if expr else None
|
||||||
|
|
|
@ -972,6 +972,8 @@ class Query(BaseExpression):
|
||||||
relabelling any references to them in select columns and the where
|
relabelling any references to them in select columns and the where
|
||||||
clause.
|
clause.
|
||||||
"""
|
"""
|
||||||
|
if not change_map:
|
||||||
|
return self
|
||||||
# If keys and values of change_map were to intersect, an alias might be
|
# If keys and values of change_map were to intersect, an alias might be
|
||||||
# updated twice (e.g. T4 -> T5, T5 -> T6, so also T4 -> T6) depending
|
# updated twice (e.g. T4 -> T5, T5 -> T6, so also T4 -> T6) depending
|
||||||
# on their order in change_map.
|
# on their order in change_map.
|
||||||
|
|
|
@ -204,6 +204,8 @@ class WhereNode(tree.Node):
|
||||||
Relabel the alias values of any children. 'change_map' is a dictionary
|
Relabel the alias values of any children. 'change_map' is a dictionary
|
||||||
mapping old (current) alias values to the new values.
|
mapping old (current) alias values to the new values.
|
||||||
"""
|
"""
|
||||||
|
if not change_map:
|
||||||
|
return self
|
||||||
for pos, child in enumerate(self.children):
|
for pos, child in enumerate(self.children):
|
||||||
if hasattr(child, "relabel_aliases"):
|
if hasattr(child, "relabel_aliases"):
|
||||||
# For example another WhereNode
|
# For example another WhereNode
|
||||||
|
@ -225,6 +227,8 @@ class WhereNode(tree.Node):
|
||||||
return clone
|
return clone
|
||||||
|
|
||||||
def replace_expressions(self, replacements):
|
def replace_expressions(self, replacements):
|
||||||
|
if not replacements:
|
||||||
|
return self
|
||||||
if replacement := replacements.get(self):
|
if replacement := replacements.get(self):
|
||||||
return replacement
|
return replacement
|
||||||
clone = self.create(connector=self.connector, negated=self.negated)
|
clone = self.create(connector=self.connector, negated=self.negated)
|
||||||
|
|
Loading…
Reference in New Issue