#35407: Cache Model._meta.swapped
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: nobody
Johnson |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Another candidate for caching, like #35232 before.
The `Model._meta.swapped` property returns the model that this one has
been swapped for. Since most models are not swappable (only `auth.User` is
officially swappable), it returns `None` in nearly all cases.
I found this property was the most called function in Django when
profiling a subset of Django’s tests, with:
{{{
$ python -m cProfile -o profile runtests.py --parallel 1 *model*
$ python -m pstats profile <<< 'sort ncalls
stats 10000' | less
}}}
This showed 439,484 calls to `swapped`, taking 29ms of the 5.597s test
run, or 0.5% of the total runtime.
After adding `@cached_property`, this is reduced to 3,653 calls, rounding
down to 0ms.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35407>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.