From b2fec1f08df316e26ff2c756ff23a9605746a864 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 21 Jun 2024 17:51:56 -0400 Subject: [PATCH] Optimized Case.as_sql() default compilation. Moving compiler.compile(self.default) to the else clause avoids the call when it's unneeded in the FullResultSet case. --- django/db/models/expressions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 4ee22420d964..dcba973ff48b 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -1613,7 +1613,6 @@ def as_sql( template_params = {**self.extra, **extra_context} case_parts = [] sql_params = [] - default_sql, default_params = compiler.compile(self.default) for case in self.cases: try: case_sql, case_params = compiler.compile(case) @@ -1624,6 +1623,8 @@ def as_sql( break case_parts.append(case_sql) sql_params.extend(case_params) + else: + default_sql, default_params = compiler.compile(self.default) if not case_parts: return default_sql, default_params case_joiner = case_joiner or self.case_joiner