From 41c01b9e4dfcfb63e45eb7693f10758220b7f443 Mon Sep 17 00:00:00 2001 From: Ignas Kiela Date: Thu, 17 Mar 2022 23:22:38 +0200 Subject: [PATCH] Get all author users with a single query for log Right now repos log page is the slowest page on git.sr.ht, and a big reason for that is that it currently averages on 19 queries per view, most of them coming from the template looking up users to show links to their user pages. We can get all the users we have before that, and pass that on to the template. With these changes, a page view always takes 5 queries consistently. --- gitsrht/blueprints/repo.py | 7 +++++-- gitsrht/templates/log.html | 2 +- gitsrht/templates/utils.html | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 07c42c2..78c5680 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -12,7 +12,7 @@ from gitsrht.editorconfig import EditorConfig from gitsrht.git import Repository as GitRepository, commit_time, annotate_tree from gitsrht.git import diffstat, get_log, diff_for_commit, strip_pgp_signature from gitsrht.rss import generate_feed -from gitsrht.types import Artifact +from gitsrht.types import Artifact, User from io import BytesIO from jinja2 import Markup from jinja2.utils import url_quote, escape @@ -484,10 +484,13 @@ def log(owner, repo, ref, path): entry = commit.tree[path] has_more = commits and len(commits) == 21 + + author_emails = set((commit.author.email for commit in commits[:20])) + authors = {user.email:user for user in User.query.filter(User.email.in_(author_emails)).all()} return render_template("log.html", view="log", owner=owner, repo=repo, ref=ref, path=path.split("/"), commits=commits[:20], refs=refs, entry=entry, pygit2=pygit2, - has_more=has_more) + has_more=has_more, authors=authors) @repo.route("///log/rss.xml", defaults={"ref": None}) diff --git a/gitsrht/templates/log.html b/gitsrht/templates/log.html index 5ede399..1939886 100644 --- a/gitsrht/templates/log.html +++ b/gitsrht/templates/log.html @@ -39,7 +39,7 @@ {% set full_path = path_join(*path) %} {% for c in commits %}
- {{ utils.commit_event(repo, c, True, refs, path=full_path) }} + {{ utils.commit_event(repo, c, True, refs, path=full_path, lookup=authors.get) }}
{% else %}
diff --git a/gitsrht/templates/utils.html b/gitsrht/templates/utils.html index 8258167..14d6c75 100644 --- a/gitsrht/templates/utils.html +++ b/gitsrht/templates/utils.html @@ -73,7 +73,8 @@ endif %}{% endfor %} {% macro commit_event(repo, c, full_body=False, refs={}, full_id=False, diff=None, href=None, - parents=False, skip_body=False, target_blank=False, path=None) %} + parents=False, skip_body=False, target_blank=False, path=None, + lookup=lookup_user) %}
{% if full_id %} {{c.id.hex}} @@ -92,7 +93,7 @@ endif %}{% endfor %} >{{c.id.hex[:8]}} {% endif %} — - {% set author_user = lookup_user(c.author.email) %} + {% set author_user = lookup(c.author.email) %} {% if author_user %} {{c.author.name}} -- 2.38.4