Using standard `len` hier will lead to loading all ORM objects into memory, use `docs.count()` instead.
Since the following line overwrites the docs variable I would suggest to store the result of paginate in a `paginated_docs` variable and add the `docs.count()` call to line 776.