#35246: Make Field.unique a plain attribute
-------------------------------------+-------------------------------------
     Reporter:  Adam Johnson         |                    Owner:  Adam
         Type:                       |  Johnson
  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 Adam Johnson:

Old description:

> Another candidate for caching, like #35230, #35232 and #35241, following
> the same system check profiling.
>
> `Field.unique` is a simple property that computes whether a field is
> unique from two inputs:
>
> {{{
> @property
>     def unique(self):
>         return self._unique or self.primary_key
> }}}
>
> The result is immutable because the two input attributes shouldn’t
> change.
>
> I found this method was called 3543 times during system checks, taking
> ~0.7% (~0.3ms) of the total runtime on a Python 3.12 project with 118
> models. After moving it to a plain attribute, this cost is eliminated.
> (cProfile’s overhead biases the cost of function calls upwards, so the
> actual saving may be smaller, but it still seems worth the minimal
> change.)
>
> `unique` is accessed in many other code paths so this change will help
> those paths too.

New description:

 Another candidate for caching, like #35230, #35232 and #35241, following
 the same system check profiling.

 `Field.unique` is a simple property that computes whether a field is
 unique from two inputs:

 {{{
 @property
 def unique(self):
     return self._unique or self.primary_key
 }}}

 The result is immutable because the two input attributes shouldn’t change.

 I found this method was called 3543 times during system checks, taking
 ~0.7% (~0.3ms) of the total runtime on a Python 3.12 project with 118
 models. After moving it to a plain attribute, this cost is eliminated.
 (cProfile’s overhead biases the cost of function calls upwards, so the
 actual saving may be smaller, but it still seems worth the minimal
 change.)

 `unique` is accessed in many other code paths so this change will help
 those paths too.

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35246#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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018dd5dc57c5-d0266246-eb56-45c2-9c4c-fa982a0726ef-000000%40eu-central-1.amazonses.com.

Reply via email to