From 5353e7c2505c0d0ab8232ad9c131b3c99c833988 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Thu, 16 Sep 2021 11:03:04 +0100 Subject: [PATCH] Refs #27624 -- Optimized Query.clone() for non-combined queries. This avoids constructing a generator expression and a new tuple if the Query has no combined queries. --- django/db/models/sql/query.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 9427c05c21..388c60545f 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -306,7 +306,10 @@ class Query(BaseExpression): obj.annotations = self.annotations.copy() if self.annotation_select_mask is not None: obj.annotation_select_mask = self.annotation_select_mask.copy() - obj.combined_queries = tuple(query.clone() for query in self.combined_queries) + if self.combined_queries: + obj.combined_queries = tuple([ + query.clone() for query in self.combined_queries + ]) # _annotation_select_cache cannot be copied, as doing so breaks the # (necessary) state in which both annotations and # _annotation_select_cache point to the same underlying objects.