Opened 5 months ago

Closed 5 months ago

#35241 closed Cleanup/optimization (fixed)

Cache Model._meta.get_parent_list()

Reported by: Adam Johnson Owned by: Adam Johnson
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adam Johnson)

Another candidate for caching, like #35230 and #35232, following the same system check profiling.

Model._meta.get_parent_list() computes the flattened list of ancestor models by MRO. This list is immutable, deterministic, and recursively constructed through the inheritance hierarchy. These properties make it a prime candidate for caching.

I found this method was taking ~1% (0.39ms) of the total runtime for system checks on a project with 118 models. After adding caching, it’s reduced to a negligible amount (<0.01ms).

get_parent_list() is called in many other code paths, so caching will help all of those too.

Change History (8)

comment:1 by Natalia Bidart, 5 months ago

Triage Stage: UnreviewedAccepted

Thank you Adam! Do you have concrete benchmark numbers to share?

comment:2 by Natalia Bidart, 5 months ago

Patch needs improvement: set

comment:3 by Adam Johnson, 5 months ago

Description: modified (diff)

I added concrete numbers to the description.

comment:4 by Adam Johnson, 5 months ago

Patch needs improvement: unset

comment:5 by Natalia Bidart, 5 months ago

Patch needs improvement: set

comment:6 by Adam Johnson, 5 months ago

Patch needs improvement: unset

comment:7 by Mariusz Felisiak, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In 73d5eb8:

Fixed #35241 -- Cached model's full parent list.

co-authored-by: Keryn Knight <keryn@…>
co-authored-by: Natalia <124304+nessita@…>
co-authored-by: David Smith <smithdc@…>
co-authored-by: Paolo Melchiorre <paolo@…>

Note: See TracTickets for help on using tickets.
Back to Top