Opened 2 months ago

Closed 2 months ago

#35266 closed Cleanup/optimization (fixed)

Optimize RelatedField._check_clashes()

Reported by: Adam Johnson Owned by: Adam Johnson
Component: Core (System checks) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Continuing my project to optimize the system checks, I found some easy optimizations in RelatedField._check_clashes(), which I found to take 6% of the total runtime for checks.

The function was formatting strings for error messages for each potential clash. This formatting is wasted when there is no error, which is the typical case. Moreover, the cost is exacerbated due to using the Model._meta.label property, invoking a function call.

Pushing the formatting to error cases speeds up the function significantly. I found this method was called 228 times during system checks for a project with 118 models, taking 4ms or 6% of the total total runtime. After optimizing, the cost is reduced to 1ms, or ~1.5%.

Change History (3)

comment:1 by Adam Johnson, 2 months ago

Has patch: set
Owner: changed from nobody to Adam Johnson
Status: newassigned

comment:2 by Mariusz Felisiak, 2 months ago

Triage Stage: UnreviewedAccepted

comment:3 by GitHub <noreply@…>, 2 months ago

Resolution: fixed
Status: assignedclosed

In ab22b7c:

Fixed #35266 -- Deferred formatting in RelatedField._check_clashes().

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