[Django] #35407: Cache Model._meta.swapped

14 views
Skip to first unread message

Django

unread,
Apr 26, 2024, 5:13:31 AMApr 26
to django-...@googlegroups.com
#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.

Django

unread,
Apr 26, 2024, 8:06:38 AMApr 26
to django-...@googlegroups.com
#35407: Cache Model._meta.swapped
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(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 Natalia Bidart):

* stage: Unreviewed => Accepted

Comment:

Thank you Adam! I a little on the fence on this one but let's give it a
chance. Could you please a test in the PR to cover for the new (non
trivial) logic?
--
Ticket URL: <https://code.djangoproject.com/ticket/35407#comment:1>

Django

unread,
Apr 26, 2024, 8:06:53 AMApr 26
to django-...@googlegroups.com
#35407: Cache Model._meta.swapped
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* needs_tests: 0 => 1

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

Django

unread,
May 3, 2024, 11:57:19 AMMay 3
to django-...@googlegroups.com
#35407: Cache Model._meta.swapped
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(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 Adam Johnson):

* needs_tests: 1 => 0

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

Django

unread,
May 7, 2024, 4:51:43 AMMay 7
to django-...@googlegroups.com
#35407: Cache Model._meta.swapped
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(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 Sarah Boyce):

* stage: Accepted => Ready for checkin

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

Django

unread,
May 7, 2024, 8:26:11 AMMay 7
to django-...@googlegroups.com
#35407: Cache Model._meta.swapped
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: dev
(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 Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"7abe5112f4cb50c15b79a2afd4c1b68f0767b243" 7abe511]:
{{{#!CommitTicketReference repository=""
revision="7abe5112f4cb50c15b79a2afd4c1b68f0767b243"
Fixed #35407 -- Cached model's Options.swapped.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35407#comment:5>
Reply all
Reply to author
Forward
0 new messages