Cache return value of `swappable_setting`
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.
Change History
(13)
| Triage Stage: |
Unreviewed → Accepted
|
| Patch needs improvement: |
set
|
| Owner: |
changed from Marten Kenbeek to Markus Holtermann
|
| Status: |
new → assigned
|
| Needs documentation: |
set
|
| Patch needs improvement: |
unset
|
| Needs documentation: |
unset
|
| Triage Stage: |
Accepted → Ready for checkin
|
| Resolution: |
→ fixed
|
| Status: |
assigned → closed
|
| Has patch: |
unset
|
| Triage Stage: |
Ready for checkin → Accepted
|
| Resolution: |
fixed
|
| Status: |
closed → new
|
| Resolution: |
→ fixed
|
| Status: |
new → closed
|
PR: https://github.com/django/django/pull/4459