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

Comment (by Mariusz Felisiak):

 Thanks for the report, I think we could make it faster by avoiding cloning
 every time:
 {{{#!diff
 diff --git a/django/db/models/expressions.py
 b/django/db/models/expressions.py
 index c20de5995a..80b8fc8e6a 100644
 --- a/django/db/models/expressions.py
 +++ b/django/db/models/expressions.py
 @@ -404,8 +404,9 @@ class BaseExpression:
      def replace_expressions(self, replacements):
          if replacement := replacements.get(self):
              return replacement
 +        if not (source_expressions := self.get_source_expressions()):
 +            return self
          clone = self.copy()
 -        source_expressions = clone.get_source_expressions()
          clone.set_source_expressions(
              [
                  expr.replace_expressions(replacements) if expr else None

 }}}
 Does it work for you?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35102#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/0107018cf787a27f-578e2632-e359-4498-ae65-5df5521fe96c-000000%40eu-central-1.amazonses.com.

Reply via email to