diff options
author | Ivan Habunek <ivan@habunek.com> | 2018-10-16 11:23:45 +0200 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-10-16 08:11:45 -0400 |
commit | 237f2c1b96467033e21adb3a99b2e82997d66499 (patch) | |
tree | 9e4f198a33eebfa34e91cbf9e4275ee977a02105 /gitsrht/blueprints/repo.py | |
parent | 63187f851ed6d7c8ef1a4e408e2492bbc3e7403d (diff) |
Speed up finding latest commits
Walking through the repo is very slow on larger repositories, this makes
loading the index page for large repos slow, for example it takes tens
of seconds to show https://git.sr.ht/~sircmpwn/linux
Since finding parents of commits is fast, and since the last 3 commits
(including the given one) must be within the parents and grandparents
of the given commit, the same task can be performed in much less time.
This implementation is instantaneous even for the Linux repo.
Diffstat (limited to 'gitsrht/blueprints/repo.py')
-rw-r--r-- | gitsrht/blueprints/repo.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index ab17575..707a1ce 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -73,6 +73,16 @@ def get_repo_or_redir(owner, repo): abort(redirect(url_for(request.endpoint, **view_args))) return owner, repo +def get_last_3_commits(commit): + commits = [commit] + for parent in commit.parents: + commits.append(parent) + for grandparent in parent.parents: + commits.append(grandparent) + + commits = sorted(commits, key=lambda c: commit_time(c), reverse=True) + return commits[:3] + @repo.route("/<owner>/<repo>") def summary(owner, repo): owner, repo = get_repo_or_redir(owner, repo) @@ -89,11 +99,7 @@ def summary(owner, repo): clone_urls=clone_urls) default_branch = git_repo.default_branch() tip = git_repo.get(default_branch.target) - commits = list() - for commit in git_repo.walk(tip.id, pygit2.GIT_SORT_TIME): - commits.append(commit) - if len(commits) >= 3: - break + commits = get_last_3_commits(tip) readme = get_readme(git_repo, tip) tags = [(ref, git_repo.get(git_repo.references[ref].target)) for ref in git_repo.listall_references() |