#31732: Cache function signatures
-------------------------------------------------+------------------------
               Reporter:  Adam (Chainz) Johnson  |          Owner:  nobody
                   Type:  Cleanup/optimization   |         Status:  new
              Component:  Utilities              |        Version:  master
               Severity:  Normal                 |       Keywords:
           Triage Stage:  Unreviewed             |      Has patch:  0
    Needs documentation:  0                      |    Needs tests:  0
Patch needs improvement:  0                      |  Easy pickings:  0
                  UI/UX:  0                      |
-------------------------------------------------+------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31732>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.ba19d7686458a982264fef8d2a98f306%40djangoproject.com.

Reply via email to