#20806 closed New feature (fixed)
Cached template loader doesn't cache find_template
Reported by: | Owned by: | ||
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
django.template.loaders.cached
memoizes the result of load_template
, but not find_template
. I have an application that calls select_template([a long list])
many times, and find_template
tries to find the same templates 100s of times. Adding a cache to find_template
resulted in a huge speedup for my application.
It's pretty easy to implement this and results in a nice speedup. I'll attach a patch shortly.
Attachments (1)
Change History (8)
comment:1 by , 11 years ago
Has patch: | set |
---|
comment:2 by , 11 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → New feature |
Thanks for the suggestion and pull request. IMO a change like this does need a test; not so much to verify the performance gain, as simply to give coverage of the added lines/branches for the "found in cache" case. Without that coverage, a change could break that code path and the tests would not reveal the breakage.
comment:3 by , 11 years ago
The find_template
method actually already has 100% coverage, including the code paths for 'found in cache' and 'not found in cache'. I'll attach the coverage report.
I did notice that there's no test for adding template_dirs
to the cache key (this was added in #13573, but there was no test). Would it be appropriate to add the test for that here?
comment:4 by , 11 years ago
Needs tests: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
If we've already got coverage of both those code paths, then I think I'm ok with this performance enhancement going in without an additional test. I don't see it as real useful to add a test actually asserting that the second find_template
doesn't go back to the filesystem.
Thanks for noticing the lack of coverage for template_dirs
in cache key! There actually was a test for that added in #13573 (see 8a6cb3d969), but that test was never run (CachedLoaderTests
was not imported in tests.py
). I've fixed that just now in 8f3aefdec33.
Marking this RFC as it looks good to me and tests pass, but I'd like someone else to confirm that it's ok to go in without additional tests.
comment:6 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch/pull request: https://github.com/django/django/pull/1400