Opened 9 years ago

Closed 3 years ago

#7116 closed Cleanup/optimization (fixed)

Optimize RequestContext construction

Reported by: Bastian Kleineidam <calvin@…> Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: lrekucki@…, FunkyBob Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The current RequestContext constructor pushes a new context variable
onto the stack for each template processor result. This is not
necessary. This patch just updates the top dict entry of the
current context instead of pushing a new one onto the context dict stack.

This might break some apps that pop() template processor contexts after
they have been added, but that would be using an undocumented feature, and its
a bad idea: why add the contexts in the first place if they get removed afterwards.

Attachments (1)

commit-5ec2f17 (440 bytes) - added by Bastian Kleineidam <calvin@…> 9 years ago.

Download all attachments as: .zip

Change History (9)

Changed 9 years ago by Bastian Kleineidam <calvin@…>

Attachment: commit-5ec2f17 added

comment:1 Changed 8 years ago by Jeff Anderson

Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 6 years ago by Luke Plant

Severity: Normal
Type: Cleanup/optimization

comment:3 Changed 5 years ago by Alex Gaynor

Easy pickings: unset
Triage Stage: Design decision neededAccepted
UI/UX: unset

comment:4 Changed 4 years ago by Łukasz Rekucki

Needs tests: set
Patch needs improvement: set

Modifying dict[0] isn't a good idea as those most likely would be the builtins that are initialized in BaseContext._reset_dicts, which means they get overshadowed by dict_ passed to the constructor.

Currently context processors overshadow the dict_ (I seen people debug their code for hours before realizing that, a real PITA), so any change to this behavior needs to be documented. A test case to show that no extra dicts are created while the right order is preserved would be good too :).

comment:5 Changed 4 years ago by Łukasz Rekucki

Cc: lrekucki@… added

comment:6 Changed 3 years ago by FunkyBob

Cc: FunkyBob added

Given that most of the rest of the dict-ish methods on BaseContext use self.dicts[-1] ... it would make sense for this to, also.

I've tested it locally, and it passes all current tests.

comment:7 Changed 3 years ago by FunkyBob

I've created a PR for this - https://github.com/django/django/pull/1527

I put all the context processor data into its own stack dict, on the off chance someone relies on the dict they pass to RequestContext not being altered.

comment:8 Changed 3 years ago by Anssi Kääriäinen <akaariai@…>

Resolution: fixed
Status: newclosed

In 8d473b2c54035cbcd3aacef0cb83a9769cd05ad3:

Fixed #7116 -- Optimize RequestContext construction

Note: See TracTickets for help on using tickets.
Back to Top