Opened 10 months ago
Closed 10 months ago
#35230 closed Cleanup/optimization (fixed)
Cache ForeignObjectRel.get_accessor_name()
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: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This method computes the name for the accessor descriptor, used in many places. Its results are stable, unless the optional model
parameter is provided, used only in forms.
Adding a cached property accessor_name
for the default value can speed up many code paths. In particular, I profiled the system checks for a project and found it was taking ~2% of the runtime, which can be basically eliminated with caching. Results:
Before: 6625 calls taking 2ms
After: 9 calls taking ~0ms
Change History (4)
comment:1 by , 10 months ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 10 months ago
Thanks for the note Simon.
Yes, it will be interesting to see the benchmark results. For a quick check, I ran a bunch of model tests with ./runtests.py --parallel 1 "*models*
under hyperfine and got the following results.
Before:
`
Time (mean ± σ): 2.745 s ± 0.031 s [User: 2.628 s, System: 0.100 s]
Range (min … max): 2.697 s … 2.796 s 10 runs
`
After:
`
Time (mean ± σ): 2.727 s ± 0.021 s [User: 2.611 s, System: 0.099 s]
Range (min … max): 2.699 s … 2.764 s 10 runs
`
The mean is lower but a statistical T test calculates this as not statistically significant.
comment:3 by , 10 months ago
Owner: | changed from | to
---|---|
Triage Stage: | Accepted → Ready for checkin |
This is worth doing but if anyone runs into issues with this change future when running data migrations the root cause is likely tickets such as #27737 until #29898 is completed. It is well known that operating from rendered models can cause all of sorts of issues with regard to caching of relationship attributes, it should be a reason to avoid using rendered models though and not to avoid caching relationship attributes.
It will be interesting to see if this has a noticeable effect on the benchmarks.