author | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 15:46:37 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 15:46:37 (UTC) |
commit | 8813170390f3c3a0f4743afbc92ede42953fa3b0 (patch) (unidiff) | |
tree | 39305350baee1eb564aae00294634bbe544983d3 | |
parent | 54272e60965ec6a98b49cbf67d72a4b1f5adc55b (diff) | |
download | cgit-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>
-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | shared.c | 1 | ||||
-rw-r--r-- | ui-repolist.c | 16 |
3 files changed, 14 insertions, 4 deletions
@@ -52,24 +52,25 @@ struct cgit_repo { | |||
52 | char *name; | 52 | char *name; |
53 | char *path; | 53 | char *path; |
54 | char *desc; | 54 | char *desc; |
55 | char *owner; | 55 | char *owner; |
56 | char *defbranch; | 56 | char *defbranch; |
57 | char *group; | 57 | char *group; |
58 | char *module_link; | 58 | char *module_link; |
59 | char *readme; | 59 | char *readme; |
60 | char *clone_url; | 60 | char *clone_url; |
61 | int snapshots; | 61 | int snapshots; |
62 | int enable_log_filecount; | 62 | int enable_log_filecount; |
63 | int enable_log_linecount; | 63 | int enable_log_linecount; |
64 | time_t mtime; | ||
64 | }; | 65 | }; |
65 | 66 | ||
66 | struct cgit_repolist { | 67 | struct cgit_repolist { |
67 | int length; | 68 | int length; |
68 | int count; | 69 | int count; |
69 | struct cgit_repo *repos; | 70 | struct cgit_repo *repos; |
70 | }; | 71 | }; |
71 | 72 | ||
72 | struct commitinfo { | 73 | struct commitinfo { |
73 | struct commit *commit; | 74 | struct commit *commit; |
74 | char *author; | 75 | char *author; |
75 | char *author_email; | 76 | char *author_email; |
@@ -51,24 +51,25 @@ struct cgit_repo *cgit_add_repo(const char *url) | |||
51 | ret->url = trim_end(url, '/'); | 51 | ret->url = trim_end(url, '/'); |
52 | ret->name = ret->url; | 52 | ret->name = ret->url; |
53 | ret->path = NULL; | 53 | ret->path = NULL; |
54 | ret->desc = "[no description]"; | 54 | ret->desc = "[no description]"; |
55 | ret->owner = NULL; | 55 | ret->owner = NULL; |
56 | ret->group = ctx.cfg.repo_group; | 56 | ret->group = ctx.cfg.repo_group; |
57 | ret->defbranch = "master"; | 57 | ret->defbranch = "master"; |
58 | ret->snapshots = ctx.cfg.snapshots; | 58 | ret->snapshots = ctx.cfg.snapshots; |
59 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; | 59 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
60 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; | 60 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
61 | ret->module_link = ctx.cfg.module_link; | 61 | ret->module_link = ctx.cfg.module_link; |
62 | ret->readme = NULL; | 62 | ret->readme = NULL; |
63 | ret->mtime = -1; | ||
63 | return ret; | 64 | return ret; |
64 | } | 65 | } |
65 | 66 | ||
66 | struct cgit_repo *cgit_get_repoinfo(const char *url) | 67 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
67 | { | 68 | { |
68 | int i; | 69 | int i; |
69 | struct cgit_repo *repo; | 70 | struct cgit_repo *repo; |
70 | 71 | ||
71 | for (i=0; i<cgit_repolist.count; i++) { | 72 | for (i=0; i<cgit_repolist.count; i++) { |
72 | repo = &cgit_repolist.repos[i]; | 73 | repo = &cgit_repolist.repos[i]; |
73 | if (!strcmp(repo->url, url)) | 74 | if (!strcmp(repo->url, url)) |
74 | return repo; | 75 | return repo; |
diff --git a/ui-repolist.c b/ui-repolist.c index cf27cb3..aa743bf 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -23,37 +23,45 @@ time_t read_agefile(char *path) | |||
23 | return -1; | 23 | return -1; |
24 | fclose(f); | 24 | fclose(f); |
25 | if (parse_date(buf, buf2, sizeof(buf2))) | 25 | if (parse_date(buf, buf2, sizeof(buf2))) |
26 | return strtoul(buf2, NULL, 10); | 26 | return strtoul(buf2, NULL, 10); |
27 | else | 27 | else |
28 | return 0; | 28 | return 0; |
29 | } | 29 | } |
30 | 30 | ||
31 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) | 31 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) |
32 | { | 32 | { |
33 | char *path; | 33 | char *path; |
34 | struct stat s; | 34 | struct stat s; |
35 | struct cgit_repo *r = (struct cgit_repo *)repo; | ||
35 | 36 | ||
37 | if (repo->mtime != -1) { | ||
38 | *mtime = repo->mtime; | ||
39 | return 1; | ||
40 | } | ||
36 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); | 41 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
37 | if (stat(path, &s) == 0) { | 42 | if (stat(path, &s) == 0) { |
38 | *mtime = read_agefile(path); | 43 | *mtime = read_agefile(path); |
44 | r->mtime = *mtime; | ||
39 | return 1; | 45 | return 1; |
40 | } | 46 | } |
41 | 47 | ||
42 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); | 48 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); |
43 | if (stat(path, &s) == 0) { | 49 | if (stat(path, &s) == 0) |
44 | *mtime = s.st_mtime; | 50 | *mtime = s.st_mtime; |
45 | return 1; | 51 | else |
46 | } | 52 | *mtime = 0; |
47 | return 0; | 53 | |
54 | r->mtime = *mtime; | ||
55 | return (r->mtime != 0); | ||
48 | } | 56 | } |
49 | 57 | ||
50 | static void print_modtime(struct cgit_repo *repo) | 58 | static void print_modtime(struct cgit_repo *repo) |
51 | { | 59 | { |
52 | time_t t; | 60 | time_t t; |
53 | if (get_repo_modtime(repo, &t)) | 61 | if (get_repo_modtime(repo, &t)) |
54 | cgit_print_age(t, -1, NULL); | 62 | cgit_print_age(t, -1, NULL); |
55 | } | 63 | } |
56 | 64 | ||
57 | int is_match(struct cgit_repo *repo) | 65 | int is_match(struct cgit_repo *repo) |
58 | { | 66 | { |
59 | if (!ctx.qry.search) | 67 | if (!ctx.qry.search) |