Opened 16 years ago
Closed 16 years ago
#9874 closed (duplicate)
Added template cache in template loader
Reported by: | novoya | Owned by: | novoya |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | template cache | |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The following view module fails to make good reuse to the template
For each new request, all inherited/included templates will be recreated and compiled
tpl = loader.get_template('mytemplate.html') def view_func1(request): ''' Any inherited templates from tpl will created and recompiled again and again ''' return HttpResponse(tpl.render(context))
Attached a very simple patch to solve the problem:
1.Simple template caching in the django.template.loader
2.Minimum change in django.template.loader_tags
Attachments (2)
Change History (16)
by , 16 years ago
Attachment: | template_cache.diff added |
---|
comment:1 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 16 years ago
milestone: | → post-1.0 |
---|---|
Needs tests: | set |
Resolution: | → fixed |
Status: | assigned → closed |
Summary: | Template reuse is worthless(when inheretiance or inclusion involved) because parent templates get re-compiled every time → Added template cache in template loader |
comment:4 by , 16 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
follow-up: 8 comment:5 by , 16 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
It wasn't fixed. Somebody needs to learn how to read the documentation on the ticket process to find out what "fixed" means (hint: not "some random person uploaded a patch").
follow-up: 9 comment:6 by , 16 years ago
This is almost certainly going to cause rednering bugs. The Python objects representating templates are not just the compiled form, they also (intentionally) contain rendering state. It also assumes that the same name will load the same template in every thread, which isn't true (a dynamic template loader, for example). So there are larger considerations that don't seem to have been addressed here.
Right now, this is probably the wrong approach to solving the perceived problem, but want to think about it some more.
comment:7 by , 16 years ago
It also is a hit in terms of memory usage -- every single template used in the system is going to get stored in memory, if I read the patch correctly. This could be a much worse penalty than any benefit it gives. I read somewhere that Adrian and co had originally intended templates to be stored in some compiled form, but they realised later that it simply wasn't worth the overhead.
comment:8 by , 16 years ago
Replying to ubernostrum:
It wasn't fixed. Somebody needs to learn how to read the documentation on the ticket process to find out what "fixed" means (hint: not "some random person uploaded a patch").
Yes. Sorry guys. My mistake. I isn't fixed
Anyway, I didn't tested too much, but from my tests, my SIMPLE fix boosts performance considerably (for each refresh- 0 template reloads + compiles instead of 5 or more in a typical web app)
About memory usage concerns, it is very easy to limit the size of the cache in code.
comment:9 by , 16 years ago
Replying to mtredinnick:
This is almost certainly going to cause rednering bugs. The Python objects representating templates are not just the compiled form, they also (intentionally) contain rendering state. It also assumes that the same name will load the same template in every thread, which isn't true (a dynamic template loader, for example). So there are larger considerations that don't seem to have been addressed here.
Right now, this is probably the wrong approach to solving the perceived problem, but want to think about it some more.
How can template contain rendering state, if each time a template needed, it gets reloaded and recompiled ??
comment:10 by , 16 years ago
Some benchmarking might be useful here. I could imagine the benefit being negligent on simple templates; but it could give a big plus for high traffic templates and/or large, complex templates (eg with lots of includes, multiple levels of inheritance, loading lots of tag libraries). Maybe it could be a setting, eg TEMPLATE_CACHE = 100, set zero by default. I have noticed before that after the database has been optimised and memcache used wherever possible, a unnecessarily high number of cycles are chewed up on template rendering.
by , 16 years ago
Attachment: | template-cache.diff added |
---|
Fixes for cache key name, thread safety. Adds config, tests.
comment:12 by , 16 years ago
Needs tests: | unset |
---|
Y'all, here's a new patch which fixes a couple of the concerns raised above:
Add TEMPLATE_CACHE config setting (default off), update the docs.
Don't cache templates when we're in TEMPLATE_DEBUG mode.
Use auxiliary template load dirs as part of cache key name.
Add some locking around the template cache.
Add some tests.
Not only is template caching faster, but it opens the door for further tuning. For example, we could walk the compiled template and coalesce adjacent TextNode's.
What else needs to be done to get this accepted?
comment:13 by , 16 years ago
See also #9154, it contains a patch with a similar implementation and it seems to take in account problems found with the {{ block.super}}
variable.
templace cache patch