﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35270	Optimize Model._meta._property_names	Adam Johnson	Adam Johnson	"Continuing my project to optimize the system checks, I found some optimizations for `Options._meta._property_names`, which I found to take ~4% of the total runtime for checks.

Most of this function’s runtime was being spent running `inspect.getattr_static()`. This is not surprising as it [https://github.com/python/cpython/blob/9b9e819b5116302cb4e471763feb2764eb17dde8/Lib/inspect.py#L1852 jumps through many hoops] in order to avoid triggering attribute access.

I added use of `getattr_static()` back in #28269 / ed244199c72f5bbf33ab4547e06e69873d7271d0 to fix a bug with instance descriptors. But I think it’s overly cautious, and we can assume that accessing the `__dict__` of the model class will work fine.

Two optimizations make the function run in negligible time:

1. Changing the function to use  `__dict__` directly
2. Caching on a per-class basis. This requires using a weak-reference to classes, as we shouldn’t mutate base classes in the MRO, some of which can be non-model subclasses, like `Model` itself for the `pk` property, `object`, or any mixins.

Before optimization stats:

106 calls to `_property_names` took 26ms, or ~4% of the total runtime of system checks.

After optimization:

The same calls take 1ms, or ~0.2% of the total runtime. (The real runtime may be <1ms, but shows as 1 due to rounding up by cProfile.)"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed		Keryn Knight	Ready for checkin	1	0	0	0	0	0
