#35102: Performance regression on Model.objects.count() between Django 4.2 and 
5.0
-------------------------------------+-------------------------------------
     Reporter:  Anthony Shaw         |                    Owner:  (none)
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Alternate patch that avoids the cost at class definition time as that
 might have an effect on Django bootstrapping time

 {{{#!diff
 diff --git a/django/db/models/expressions.py
 b/django/db/models/expressions.py
 index c20de5995a..0ecda38353 100644
 --- a/django/db/models/expressions.py
 +++ b/django/db/models/expressions.py
 @@ -14,7 +14,7 @@
  from django.db.models.constants import LOOKUP_SEP
  from django.db.models.query_utils import Q
  from django.utils.deconstruct import deconstructible
 -from django.utils.functional import cached_property
 +from django.utils.functional import cached_property, classproperty
  from django.utils.hashable import make_hashable


 @@ -485,15 +485,21 @@ def select_format(self, compiler, sql, params):
  class Expression(BaseExpression, Combinable):
      """An expression that can be combined with other expressions."""

 +    @classproperty
 +    @functools.lru_cache(maxsize=128)
 +    def _constructor_signature(cls):
 +        return inspect.signature(cls.__init__)
 +
      @cached_property
      def identity(self):
 -        constructor_signature = inspect.signature(self.__init__)
          args, kwargs = self._constructor_args
 -        signature = constructor_signature.bind_partial(*args, **kwargs)
 +        signature = self._constructor_signature.bind_partial(self, *args,
 **kwargs)
          signature.apply_defaults()
          arguments = signature.arguments.items()
          identity = [self.__class__]
          for arg, value in arguments:
 +            if arg == 'self':
 +                continue
              if isinstance(value, fields.Field):
                  if value.name and value.model:
                      value = (value.model._meta.label, value.name)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35102#comment:13>
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/0107018cfac08008-b9817c3e-3101-4498-adb6-823a4b5f1c8a-000000%40eu-central-1.amazonses.com.

Reply via email to