flask-sqlalchemy
flask-sqlalchemy copied to clipboard
Very poor performance of Pagination.{pages,iter_pages}
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.
https://github.com/mitsuhiko/flask-sqlalchemy/pull/393
fixed in #1087