#27974: Degraded performance when rendering ChoiceField with lots of options and
DEBUG=True
-------------------------------------+-------------------------------------
     Reporter:  karyon               |                    Owner:  kapil
         Type:                       |  garg
  Cleanup/optimization               |                   Status:  assigned
    Component:  Template system      |                  Version:  1.11
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by kapil garg):

 There are certain problems because of caching.

 1. cycle tag does not work in included template in a for loop just like
 this bug [https://code.djangoproject.com/ticket/23516 ifchanged bug] . The
 following patch seems to resolve the issue but it raises problem 2.

 {{{
 diff --git a/django/template/defaulttags.py
 b/django/template/defaulttags.py
 index e2be85b..3d6832e 100644
 --- a/django/template/defaulttags.py
 +++ b/django/template/defaulttags.py
 @@ -74,10 +74,11 @@ class CycleNode(Node):
          self.silent = silent

      def render(self, context):
 -        if self not in context.render_context:
 +        state_frame = self._get_context_stack_frame(context)
 +        if self not in state_frame:
              # First time the node is rendered in template
 -            context.render_context[self] =
 itertools_cycle(self.cyclevars)
 -        cycle_iter = context.render_context[self]
 +            state_frame[self] = itertools_cycle(self.cyclevars)
 +        cycle_iter = state_frame[self]
          value = next(cycle_iter).resolve(context)
          if self.variable_name:
              context.set_upward(self.variable_name, value)
 @@ -89,7 +90,20 @@ class CycleNode(Node):
          """
          Reset the cycle iteration back to the beginning.
          """
 -        context.render_context[self] = itertools_cycle(self.cyclevars)
 +        state_frame = self._get_context_stack_frame(context)
 +        state_frame[self] = itertools_cycle(self.cyclevars)
 +
 +    def _get_context_stack_frame(self, context):
 +        if 'forloop' in context:
 +            return context['forloop']
 +        else:
 +            return context.render_context

 }}}

 2. Nodes will share same state on multiple calls to include same template.
 Currently i can verify this for Cycle and Ifchanged nodes. The reason is
 that these nodes use "self" as a dictionary key to store their state and
 caching prevents generating new nodes on multiple include calls. One
 solution i can think of to solve this problem is to use "self" as
 dictionary key for template caching as this will create a new cache for
 each include node but this will lose the benefit of caching when there are
 100 hardcoded include tags instead of being in a forloop.

 These issues are related to caching so should we handle these on this
 ticket or create a new ticket ?

--
Ticket URL: <https://code.djangoproject.com/ticket/27974#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.360643102b44400570bc24e6236ba426%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to