#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):

 > I suspect this would impact other benchmarks as well, hash functions
 should be as fast as possible but I understand why that is challenging for
 something like expressions. inspect.signature is a very slow function that
 does a huge amount of processing to build a Signature object for the given
 callable.

 yeah that's the crux of the issue here. This would require having each
 expression implement their own correct identity functions based on their
 `__init__` signature which would be a significant maintenance burden and
 also backward incompatible for third party expressions.

 I think we can get a lot of benefit by ''caching'' `inspect.signature`
 calls like the following.

 {{{#!diff
 diff --git a/django/db/models/expressions.py
 b/django/db/models/expressions.py
 index c20de5995a..0703a134a9 100644
 --- a/django/db/models/expressions.py
 +++ b/django/db/models/expressions.py
 @@ -485,15 +485,22 @@ def select_format(self, compiler, sql, params):
  class Expression(BaseExpression, Combinable):
      """An expression that can be combined with other expressions."""

 +    def __init_subclass__(cls, /, **kwargs):
 +        super().__init_subclass__(**kwargs)
 +        cls._constructor_signature = inspect.signature(cls.__init__)
 +
 +    _constructor_signature = inspect.signature(BaseExpression.__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:10>
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/0107018cfa206b09-c302c08c-4a01-4b1a-9e96-c59e7614bed9-000000%40eu-central-1.amazonses.com.

Reply via email to