flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Very poor performance of Pagination.{pages,iter_pages}

Open wodim opened this issue 7 years ago • 1 comments

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.

wodim avatar May 16 '18 14:05 wodim

https://github.com/mitsuhiko/flask-sqlalchemy/pull/393

dourvaris avatar May 16 '18 15:05 dourvaris

fixed in #1087

davidism avatar Sep 18 '22 17:09 davidism