Opened 7 months ago

Closed 7 months ago

#36987 closed Cleanup/optimization (fixed)

UUIDField.get_db_prep_value does not observe prepared argument

Reported by: Jacob Walls Owned by: Jacob Walls
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Aarni Koskela, Tim Graham, Simon Charette 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 Jacob Walls)

The ​doc'd "default" pattern for fields is for get_db_prep_value() to call the get_prep_value() method if prepared is False. The idea being, I assume, to avoid wasted computation if the value is already prepared=True.

UUIDField doesn't follow this pattern, causing two extra calls to isinstance(val, UUID) per value, which can add up for a large bag of values, e.g. a prefetch_related() call.

Here is a ​fiddle on DryORM with my fix branch checked out. Re-run the fiddle against main and notice 4 more calls to isinstance (2 * n objects).

Discovered while reviewing the profile in a ​micro-optimization PR


I acknowledge there is some exploratory effort toward removing this argument in a ​draft PR.

Change History (5)

comment:1 by Jacob Walls, 7 months ago

comment:2 by Jacob Walls, 7 months ago

Description: modified (diff)

comment:3 by Simon Charette, 7 months ago

Triage Stage: Unreviewed → Accepted

comment:4 by Jacob Walls, 7 months ago

Triage Stage: Accepted → Ready for checkin

Clifford approved the PR on GitHub.

comment:5 by Jacob Walls <jacobtylerwalls@…>, 7 months ago

Resolution: → fixed
Status: assigned → closed

In 2e33abe:

Fixed #36987 -- Observed prepared argument in UUIDField.get_db_prep_value().

This avoids two isinstance() calls per UUID value.

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