Opened 10 months ago

Last modified 10 months ago

#35241 closed Cleanup/optimization

Cache Model._meta.get_parent_list() — at Version 3

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 (3)

comment:1 by Natalia Bidart, 10 months ago

Triage Stage: UnreviewedAccepted

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

comment:2 by Natalia Bidart, 10 months ago

Patch needs improvement: set

comment:3 by Adam Johnson, 10 months ago

Description: modified (diff)

I added concrete numbers to the description.

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