#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.

-- 
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/0107018f19ae54dc-c924e0b2-ab39-4b00-b808-b9e886fe2996-000000%40eu-central-1.amazonses.com.

Reply via email to