Django

Code

Ticket #3441 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

django.template.VariableDoesNotExist.__init__ prototype forces work to be done up front that is typically thrown away

Reported by: Brian Harring <ferringb@gmail.com> Assigned to: adrian
Milestone: Component: Template system
Version: SVN Keywords:
Cc: Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

Kind of a cornercase, but VariableDoesNotExist? derives from Exception, overriding nothing- meaning the str/repr for it must be done up front. Thus django.template.resolve_variable raises it via

raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current)

So... it's effectively raising VariableDoesNotExist?(msg). Problem here is that a rather large amount of code just catches the exception, and substitutes a different value, TEMPLATE_STRING_IF_INVALID for example- in other words, they don't even *care* about the msg, the exception is just a passing mechanism. Thus the msg creation is pure overhead to most codeblocks I've found in django.

Said overhead can add up; a context level lookup ( {{ foo }} for example), raising VariableDoesNotExist? results in repr(context), repr'ing all mappings in context- if say your base template is just checking for a var, if so, outputing a queryset, else not outputting it, the current code forces a repr on context, in other words forcing the query to be executed.

Which is fairly daft, if pretty much all code just throws out the exception, substituting a different value in.

Attached is a patch changing VariableDoesNotExist?'s init to (self, msg, params=()); via that, can delay the actual msg creation until it's required, making catching the exception just to substitute a different value far cheaper.

Within the code, only resolve_variable throws the exception- google code search, nobody else is throwing it, thus it looks pretty safe to change the init to delay the cost. Alternative, just derive NewStyleVariableDoesNotExist? from VariableDoesNotExist?, and have resolve_variable throw that; either way, it's work being done that isn't needed so should be tweaked.

Attachments

VariableDoesNotExist.patch (1.0 kB) - added by Brian Harring <ferringb@gmail.com> on 02/05/07 19:52:56.
VariableDoesNotExist?.init refactoring.

Change History

02/05/07 19:52:56 changed by Brian Harring <ferringb@gmail.com>

  • attachment VariableDoesNotExist.patch added.

VariableDoesNotExist?.init refactoring.

02/05/07 22:34:53 changed by ubernostrum

  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests set to 1.
  • needs_docs changed.

02/06/07 08:01:13 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

Fixed in [4461].


Add/Change #3441 (django.template.VariableDoesNotExist.__init__ prototype forces work to be done up front that is typically thrown away)




Change Properties
Action