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)

template_cache.diff (2.0 KB ) - added by novoya 16 years ago.
templace cache patch
template-cache.diff (6.9 KB ) - added by groks 16 years ago.
Fixes for cache key name, thread safety. Adds config, tests.

Download all attachments as: .zip

Change History (16)

by novoya, 16 years ago

Attachment: template_cache.diff added

templace cache patch

comment:1 by novoya, 16 years ago

Owner: changed from nobody to novoya
Status: newassigned

comment:2 by novoya, 16 years ago

milestone: post-1.0
Needs tests: set
Resolution: fixed
Status: assignedclosed
Summary: Template reuse is worthless(when inheretiance or inclusion involved) because parent templates get re-compiled every timeAdded template cache in template loader

comment:3 by ElliottM, 16 years ago

I'm confused, when was this fixed?

comment:4 by James Bennett, 16 years ago

Resolution: fixed
Status: closedreopened

comment:5 by James Bennett, 16 years ago

Triage Stage: UnreviewedDesign 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").

comment:6 by Malcolm Tredinnick, 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 Luke Plant, 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.

in reply to:  5 comment:8 by novoya, 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.

in reply to:  6 comment:9 by novoya, 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 Simon Litchfield, 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.

comment:11 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

by groks, 16 years ago

Attachment: template-cache.diff added

Fixes for cache key name, thread safety. Adds config, tests.

comment:12 by groks, 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 Ramiro Morales, 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.

comment:14 by mrts, 16 years ago

Resolution: duplicate
Status: reopenedclosed

Duplicate of #9154.

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