#31732 closed Cleanup/optimization (fixed)
Cache function signatures in django.utils.inspect.
Reported by: | Adam Johnson | Owned by: | Tim Park |
---|---|---|---|
Component: | Utilities | 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
I found a bit of a performance regression in Django 2.1 and 2.2, gone in 3.0. There, SQLCompiler.get_converters()
ran func_supports_parameter()
for every field converter
Unfortunately the information isn't cached and inspect.signature()
isn't particularly fast.
Using py-spy on a client test suite, I found func_supports_parameter()
taking 0.25% of the total run time. This isn't much absolutely, but it was reachable only through pathways using Query.get_aggregation()
(2.1%) and Query.get_count()
(0.63%). So from those paths, func_supports_parameter()
took 0.25 / (2.1 + .63) = ~10% of the time to execute aggregates, for easily cached information.
Given that function objects are immutable, I think all the use of inspect.signature
in django.utils.inspect
could go through a wrapper that caches with a WeakKeyDictionary
or functools.lru_cache
to avoid re-inspecting the same signatures.
It doesn't look like there is any usage of those functions in hot paths in Django 3.0+ but it could be useful to have this protection for any future function argument deprecations.
Change History (9)
comment:1 by , 4 years ago
Summary: | Cache function signatures → Cache function signatures in django.utils.inspect. |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 4 years ago
Has patch: | set |
---|
comment:4 by , 4 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
PR