#35285: Optimize ForeignObject._check_unique_target
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: Adam Johnson
Johnson |
Type: | Status: assigned
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 |
-------------------------------------+-------------------------------------
Continuing my project to optimize the system checks, I found some
optimizations for `ForeignObject._check_unique_target`, which I found to
take ~6% of the total runtime for checks.
Most of this function’s runtime was spent creating the set-of-frozensets
describing all unique constraints on the target model. But this set is not
needed to determine success in the most typical cases, where a primary key
or other single unique field is targeted.
To optimize, we can check for a single unique field first. This
essentially means restoring the “fast path” replaced in #25535 /
80dac8c33e7f6f22577e4346f44e4c5ee89b648c with a broad general algorithm to
cover unique constraints.
Before optimization stats:
Profiling running checks 100 times, 20,800 calls took 180ms, or ~6% of the
total runtime.
After optimization:
29ms or ~1% of the total runtime.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35285>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.