Skip to content

Very poor performance of Pagination.{pages,iter_pages} #622

@wodim

Description

@wodim

The performance of those two methods is really really bad when you have thousands of pages. In my case, with 100k pages, painting the paginator takes over 300 ms.

Pagination.iter_pages is slow by itself. Pagination.pages is not that slow but it's called once for every page from Pagination.iter_pages when you consume the iterator, and calling it 100k times really adds up. Python is a very slow language and calling the same method so many times is undesirable even if the method seems simple.

The low-hanging fruit here is caching the result of Pagination.pages. That fixes more than half of the problem (only the call to Pagination.iter_pages remains). I did so with this subclass, which might be helpful to someone. (I'm far from an expert in Python, so this may hurt your eyes)

class MyPagination(Pagination):

    def __init__(self, query, page, per_page, total, items):
        super().__init__(query, page, per_page, total, items)
        self._pages = super().pages

    @property
    def pages(self):
        return self._pages

But of course a better solution is desirable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions