[Django] #24590: Cache return value of `swappable_setting`

10 views
Skip to first unread message

Django

unread,
Apr 6, 2015, 2:07:59 PM4/6/15
to django-...@googlegroups.com
#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.

Django

unread,
Apr 6, 2015, 2:26:20 PM4/6/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: knbk
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by knbk):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

PR: https://github.com/django/django/pull/4459

--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:1>

Django

unread,
Apr 6, 2015, 8:37:56 PM4/6/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: knbk

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MarkusH):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:2>

Django

unread,
Apr 16, 2015, 11:31:53 AM4/16/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: knbk

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:3>

Django

unread,
Aug 22, 2015, 10:55:04 AM8/22/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: assigned

Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MarkusH):

* status: new => assigned
* owner: knbk => MarkusH


Comment:

I updated the existing PR: https://github.com/django/django/pull/5176

--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:4>

Django

unread,
Aug 22, 2015, 10:55:17 AM8/22/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MarkusH):

* needs_docs: 0 => 1
* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:5>

Django

unread,
Aug 25, 2015, 10:45:55 AM8/25/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MarkusH):

* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:6>

Django

unread,
Aug 26, 2015, 9:16:09 AM8/26/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:7>

Django

unread,
Aug 27, 2015, 5:33:30 AM8/27/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Markus Holtermann <info@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"e1427cc609fa6ab247501b101cfb3c0092aba55b" e1427cc6]:
{{{
#!CommitTicketReference repository=""
revision="e1427cc609fa6ab247501b101cfb3c0092aba55b"
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.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:8>

Django

unread,
Aug 27, 2015, 5:34:29 PM8/27/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 1 => 0
* stage: Ready for checkin => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:9>

Django

unread,
Aug 27, 2015, 6:18:30 PM8/27/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: new

Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: closed => new
* resolution: fixed =>


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:10>

Django

unread,
Aug 27, 2015, 6:29:09 PM8/27/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by MarkusH):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:11>

Django

unread,
Aug 28, 2015, 1:24:38 AM8/28/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Markus Holtermann <info@…>):

In [changeset:"8e631a3175f145dbf3eec269f8e3c59a1520540f" 8e631a3]:
{{{
#!CommitTicketReference repository=""
revision="8e631a3175f145dbf3eec269f8e3c59a1520540f"
Refs #24590 -- Ensured isolation between autodetector tests

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:12>

Django

unread,
Aug 28, 2015, 1:37:53 AM8/28/15
to django-...@googlegroups.com
#24590: Cache return value of `swappable_setting`
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: MarkusH
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MarkusH):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/24590#comment:13>

Reply all
Reply to author
Forward
0 new messages