#36987: UUIDField.get_db_prep_value does not observe prepared argument
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  Jacob
         Type:                       |  Walls
  Cleanup/optimization               |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Jacob Walls:

Old description:

> The
> [https://docs.djangoproject.com/en/6.0/ref/models/fields/#django.db.models.Field.get_db_prep_value
> 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 [https://dryorm.xterm.info/uuidfield-get-db-prep-value fiddle]
> on DryORM with my fix branch checked out. Re-run the fiddle against main
> and notice 4 fewer calls to isinstance (2 * n objects).
>
> Discovered while reviewing the profile in a
> [https://github.com/django/django/pull/20779/changes micro-optimization
> PR]
> ----
>
> I acknowledge there is some exploratory effort toward removing this
> argument in a [https://github.com/django/django/pull/19051 draft PR].

New description:

 The
 
[https://docs.djangoproject.com/en/6.0/ref/models/fields/#django.db.models.Field.get_db_prep_value
 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 [https://dryorm.xterm.info/uuidfield-get-db-prep-value 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
 [https://github.com/django/django/pull/20779/changes micro-optimization
 PR]
 ----

 I acknowledge there is some exploratory effort toward removing this
 argument in a [https://github.com/django/django/pull/19051 draft PR].

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36987#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019cf6db63ff-1843d453-0ea6-40d8-9960-cb5e8610298d-000000%40eu-central-1.amazonses.com.

Reply via email to