#24590: Cache return value of `swappable_setting`
----------------------------------------------+--------------------
Reporter: knbk | Owner: knbk
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
`Field.swappable_setting` is called very often during model rendering. It
loops over all models until it finds a match or otherwise returns `None`.
Most models aren't swappable, so in most cases it never breaks out of the
loop.
This method should be cached to speed up rendering in migrations. The
`Field` instances are constantly copied, so that's not a good place, but
it always uses `django.apps.apps`. Moving this looping over models to
`Apps.get_swappable_setting` and using an `lru_cache` speeds up rendering
quite a bit.
Both `AUTH_USER_MODEL` and the models contained in the main `apps`
instance should never change, except for testing scenario's. The cache
should never have to be cleared in non-testing scenario's, which results
in another very significant speed-up, for a total of about 35% faster
rendering in my benchmarks.
--
Ticket URL: <https://code.djangoproject.com/ticket/24590>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.db7c8f87b904e6eecbe7db3aa1a26d40%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.