From 96e7ff5e9ff6362d9a886545869ce4496ca4b0fb Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Fri, 7 Jan 2022 09:29:22 +0000 Subject: [PATCH] =?UTF-8?q?Avoided=20isinstance(=E2=80=A6,=20Variable)=20c?= =?UTF-8?q?alls=20in=20FilterExpression.resolve().?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By determining the variable type within __init__() instead of resolve() we can skip an isinstance() check at template runtime. Templates are executed in production more often than the parse trees themselves, assuming the cached Loader is used. --- django/template/base.py | 3 ++- django/templatetags/i18n.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/django/template/base.py b/django/template/base.py index 0dec9940ab..078be8b383 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -694,9 +694,10 @@ class FilterExpression: self.filters = filters self.var = var_obj + self.is_var = isinstance(var_obj, Variable) def resolve(self, context, ignore_failures=False): - if isinstance(self.var, Variable): + if self.is_var: try: obj = self.var.resolve(context) except VariableDoesNotExist: diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py index 8c123c7dd5..607ceb6aea 100644 --- a/django/templatetags/i18n.py +++ b/django/templatetags/i18n.py @@ -77,6 +77,7 @@ class TranslateNode(Node): self.message_context = message_context self.filter_expression = filter_expression if isinstance(self.filter_expression.var, str): + self.filter_expression.is_var = True self.filter_expression.var = Variable("'%s'" % self.filter_expression.var)