summaryrefslogtreecommitdiffabout
path: root/ui-repolist.c
authorLars Hjemli <hjemli@gmail.com>2008-11-29 15:46:37 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-11-29 15:46:37 (UTC)
commit8813170390f3c3a0f4743afbc92ede42953fa3b0 (patch) (side-by-side diff)
tree39305350baee1eb564aae00294634bbe544983d3 /ui-repolist.c
parent54272e60965ec6a98b49cbf67d72a4b1f5adc55b (diff)
downloadcgit-8813170390f3c3a0f4743afbc92ede42953fa3b0.zip
cgit-8813170390f3c3a0f4743afbc92ede42953fa3b0.tar.gz
cgit-8813170390f3c3a0f4743afbc92ede42953fa3b0.tar.bz2
ui-repolist: implement lazy caching of repo->mtime
When sorting the list of repositories by their last modification time, cgit would (in the worst case) invoke fstat(3) four times and open(3) twice for each callback from qsort(3). This obviously scales very badly. Now, the calculated modtime for each repo is saved in repo->mtime, thus keeping the number of stat/open invocations identical for sorted and unsorted repo-listings. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'ui-repolist.c') (more/less context) (show whitespace changes)
-rw-r--r--ui-repolist.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/ui-repolist.c b/ui-repolist.c
index cf27cb3..aa743bf 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -1,143 +1,151 @@
/* ui-repolist.c: functions for generating the repolist page
*
* Copyright (C) 2006 Lars Hjemli
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
*/
#include <time.h>
#include "cgit.h"
#include "html.h"
#include "ui-shared.h"
time_t read_agefile(char *path)
{
FILE *f;
static char buf[64], buf2[64];
if (!(f = fopen(path, "r")))
return -1;
if (fgets(buf, sizeof(buf), f) == NULL)
return -1;
fclose(f);
if (parse_date(buf, buf2, sizeof(buf2)))
return strtoul(buf2, NULL, 10);
else
return 0;
}
static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
{
char *path;
struct stat s;
+ struct cgit_repo *r = (struct cgit_repo *)repo;
+ if (repo->mtime != -1) {
+ *mtime = repo->mtime;
+ return 1;
+ }
path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
if (stat(path, &s) == 0) {
*mtime = read_agefile(path);
+ r->mtime = *mtime;
return 1;
}
path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
- if (stat(path, &s) == 0) {
+ if (stat(path, &s) == 0)
*mtime = s.st_mtime;
- return 1;
- }
- return 0;
+ else
+ *mtime = 0;
+
+ r->mtime = *mtime;
+ return (r->mtime != 0);
}
static void print_modtime(struct cgit_repo *repo)
{
time_t t;
if (get_repo_modtime(repo, &t))
cgit_print_age(t, -1, NULL);
}
int is_match(struct cgit_repo *repo)
{
if (!ctx.qry.search)
return 1;
if (repo->url && strcasestr(repo->url, ctx.qry.search))
return 1;
if (repo->name && strcasestr(repo->name, ctx.qry.search))
return 1;
if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
return 1;
if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
return 1;
return 0;
}
int is_in_url(struct cgit_repo *repo)
{
if (!ctx.qry.url)
return 1;
if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
return 1;
return 0;
}
void print_sort_header(const char *title, const char *sort)
{
htmlf("<th class='left'><a href='./?s=%s", sort);
if (ctx.qry.search) {
html("&q=");
html_url_arg(ctx.qry.search);
}
htmlf("'>%s</a></th>", title);
}
void print_header(int columns)
{
html("<tr class='nohover'>");
print_sort_header("Name", "name");
print_sort_header("Description", "desc");
print_sort_header("Owner", "owner");
print_sort_header("Idle", "idle");
if (ctx.cfg.enable_index_links)
html("<th class='left'>Links</th>");
html("</tr>\n");
}
void print_pager(int items, int pagelen, char *search)
{
int i;
html("<div class='pager'>");
for(i = 0; i * pagelen < items; i++)
cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
search, i * pagelen);
html("</div>");
}
static int cmp(const char *s1, const char *s2)
{
if (s1 && s2)
return strcmp(s1, s2);
if (s1 && !s2)
return -1;
if (s2 && !s1)
return 1;
return 0;
}
static int sort_name(const void *a, const void *b)
{
const struct cgit_repo *r1 = a;
const struct cgit_repo *r2 = b;
return cmp(r1->name, r2->name);
}
static int sort_desc(const void *a, const void *b)
{
const struct cgit_repo *r1 = a;
const struct cgit_repo *r2 = b;
return cmp(r1->desc, r2->desc);
}
static int sort_owner(const void *a, const void *b)
{
const struct cgit_repo *r1 = a;