Opened 9 years ago

Closed 9 years ago

#24590 closed Cleanup/optimization (fixed)

Cache return value of `swappable_setting`

Reported by: Marten Kenbeek Owned by: Markus Holtermann
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

comment:2 by Markus Holtermann, 9 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:4 by Markus Holtermann, 9 years ago

Owner: changed from Marten Kenbeek to Markus Holtermann
Status: newassigned

comment:5 by Markus Holtermann, 9 years ago

Needs documentation: set
Patch needs improvement: unset

comment:6 by Markus Holtermann, 9 years ago

Needs documentation: unset

comment:7 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Markus Holtermann <info@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In e1427cc6:

Fixed #24590 -- Cached calls to swappable_setting.

Moved the lookup in Field.swappable_setting to Apps, and added
an lru_cache to cache the results.

Refs #24743

Thanks Marten Kenbeek for the initial work on the patch. Thanks Aymeric
Augustin and Tim Graham for the review.

comment:9 by Tim Graham, 9 years ago

Has patch: unset
Triage Stage: Ready for checkinAccepted

Looks like the new tests aren't isolated as they fail in reverse: $ ./tests/runtests.py --reverse migrations

comment:10 by Tim Graham, 9 years ago

Resolution: fixed
Status: closednew

comment:11 by Markus Holtermann, 9 years ago

This change isolates the tests in migrations that rely on a replaced swappable model: https://github.com/django/django/pull/5199

comment:12 by Markus Holtermann <info@…>, 9 years ago

In 8e631a3:

Refs #24590 -- Ensured isolation between autodetector tests

Fixed a regression introduced in e1427cc609fa6ab247501b101cfb3c0092aba55b when running tests in reverse order.

comment:13 by Markus Holtermann, 9 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top