#35733: Page.__len__ could skip a query if self.paginator.count == 0
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  Jacob
         Type:                       |  Walls
  Cleanup/optimization               |                   Status:  assigned
    Component:  Core (Other)         |                  Version:  dev
     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
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * stage:  Unreviewed => Accepted

Comment:

 In this case I think it's safe to move forward with the optimization, the
 closest ticket I could find is #23771.

 Note that a similar optimization could have done the other way around
 though where when a page is requested `if len(page.object_list) <
 paginator.per_page` then `count` can be deduced from `self.paginator.count
 = (self.number - 1) * self.paginator.per_page + len(self.object_list)`.

 {{{#!diff
 diff --git a/django/core/paginator.py b/django/core/paginator.py
 index 7b3189cc8b..6b0aafc6d0 100644
 --- a/django/core/paginator.py
 +++ b/django/core/paginator.py
 @@ -89,7 +89,7 @@ def page(self, number):
          number = self.validate_number(number)
          bottom = (number - 1) * self.per_page
          top = bottom + self.per_page
 -        if top + self.orphans >= self.count:
 +        if self.orphans and top + self.orphans >= self.count:
              top = self.count
          return self._get_page(self.object_list[bottom:top], number, self)

 @@ -188,7 +188,14 @@ def __repr__(self):
          return "<Page %s of %s>" % (self.number,
 self.paginator.num_pages)

      def __len__(self):
 -        return len(self.object_list)
 +        if (paginator_count := self.paginator.__dict__.get("count")) ==
 0:
 +            return 0
 +        object_list_len = len(self.object_list)
 +        if paginator_count is None and object_list_len <
 self.paginator.per_page:
 +            self.paginator.count = (
 +                self.number - 1
 +            ) * self.paginator.per_page + object_list_len
 +        return object_list_len

      def __getitem__(self, index):
          if not isinstance(index, (int, slice)):
 }}}

 Unfortunately because `Paginator.get_page` validates against
 `self.num_pages` which triggers the calculation of `self.count` this is
 not feasible. On the bright side though, this means that `get_page` will
 systematically trigger `self.count` calculation which strengthen the
 assumption that it's safe to use directly in `Page.__len__`.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35733#comment:3>
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/01070191c4cd9d2a-abe03ac5-1631-463e-961a-34fe31f57b81-000000%40eu-central-1.amazonses.com.

Reply via email to