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
@@ -16,96 +16,97 @@ | |||
16 | #include <log-tree.h> | 16 | #include <log-tree.h> |
17 | #include <archive.h> | 17 | #include <archive.h> |
18 | #include <xdiff/xdiff.h> | 18 | #include <xdiff/xdiff.h> |
19 | #include <utf8.h> | 19 | #include <utf8.h> |
20 | 20 | ||
21 | 21 | ||
22 | /* | 22 | /* |
23 | * Dateformats used on misc. pages | 23 | * Dateformats used on misc. pages |
24 | */ | 24 | */ |
25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S (%Z)" | 25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S (%Z)" |
26 | #define FMT_SHORTDATE "%Y-%m-%d" | 26 | #define FMT_SHORTDATE "%Y-%m-%d" |
27 | #define FMT_ATOMDATE "%Y-%m-%dT%H:%M:%SZ" | 27 | #define FMT_ATOMDATE "%Y-%m-%dT%H:%M:%SZ" |
28 | 28 | ||
29 | 29 | ||
30 | /* | 30 | /* |
31 | * Limits used for relative dates | 31 | * Limits used for relative dates |
32 | */ | 32 | */ |
33 | #define TM_MIN 60 | 33 | #define TM_MIN 60 |
34 | #define TM_HOUR (TM_MIN * 60) | 34 | #define TM_HOUR (TM_MIN * 60) |
35 | #define TM_DAY (TM_HOUR * 24) | 35 | #define TM_DAY (TM_HOUR * 24) |
36 | #define TM_WEEK (TM_DAY * 7) | 36 | #define TM_WEEK (TM_DAY * 7) |
37 | #define TM_YEAR (TM_DAY * 365) | 37 | #define TM_YEAR (TM_DAY * 365) |
38 | #define TM_MONTH (TM_YEAR / 12.0) | 38 | #define TM_MONTH (TM_YEAR / 12.0) |
39 | 39 | ||
40 | 40 | ||
41 | /* | 41 | /* |
42 | * Default encoding | 42 | * Default encoding |
43 | */ | 43 | */ |
44 | #define PAGE_ENCODING "UTF-8" | 44 | #define PAGE_ENCODING "UTF-8" |
45 | 45 | ||
46 | typedef void (*configfn)(const char *name, const char *value); | 46 | typedef void (*configfn)(const char *name, const char *value); |
47 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 47 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
48 | typedef void (*linediff_fn)(char *line, int len); | 48 | typedef void (*linediff_fn)(char *line, int len); |
49 | 49 | ||
50 | struct cgit_repo { | 50 | struct cgit_repo { |
51 | char *url; | 51 | char *url; |
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; |
76 | unsigned long author_date; | 77 | unsigned long author_date; |
77 | char *committer; | 78 | char *committer; |
78 | char *committer_email; | 79 | char *committer_email; |
79 | unsigned long committer_date; | 80 | unsigned long committer_date; |
80 | char *subject; | 81 | char *subject; |
81 | char *msg; | 82 | char *msg; |
82 | char *msg_encoding; | 83 | char *msg_encoding; |
83 | }; | 84 | }; |
84 | 85 | ||
85 | struct taginfo { | 86 | struct taginfo { |
86 | char *tagger; | 87 | char *tagger; |
87 | char *tagger_email; | 88 | char *tagger_email; |
88 | unsigned long tagger_date; | 89 | unsigned long tagger_date; |
89 | char *msg; | 90 | char *msg; |
90 | }; | 91 | }; |
91 | 92 | ||
92 | struct refinfo { | 93 | struct refinfo { |
93 | const char *refname; | 94 | const char *refname; |
94 | struct object *object; | 95 | struct object *object; |
95 | union { | 96 | union { |
96 | struct taginfo *tag; | 97 | struct taginfo *tag; |
97 | struct commitinfo *commit; | 98 | struct commitinfo *commit; |
98 | }; | 99 | }; |
99 | }; | 100 | }; |
100 | 101 | ||
101 | struct reflist { | 102 | struct reflist { |
102 | struct refinfo **refs; | 103 | struct refinfo **refs; |
103 | int alloc; | 104 | int alloc; |
104 | int count; | 105 | int count; |
105 | }; | 106 | }; |
106 | 107 | ||
107 | struct cgit_query { | 108 | struct cgit_query { |
108 | int has_symref; | 109 | int has_symref; |
109 | int has_sha1; | 110 | int has_sha1; |
110 | char *raw; | 111 | char *raw; |
111 | char *repo; | 112 | char *repo; |
@@ -15,96 +15,97 @@ int cgit_cmd; | |||
15 | int chk_zero(int result, char *msg) | 15 | int chk_zero(int result, char *msg) |
16 | { | 16 | { |
17 | if (result != 0) | 17 | if (result != 0) |
18 | die("%s: %s", msg, strerror(errno)); | 18 | die("%s: %s", msg, strerror(errno)); |
19 | return result; | 19 | return result; |
20 | } | 20 | } |
21 | 21 | ||
22 | int chk_positive(int result, char *msg) | 22 | int chk_positive(int result, char *msg) |
23 | { | 23 | { |
24 | if (result <= 0) | 24 | if (result <= 0) |
25 | die("%s: %s", msg, strerror(errno)); | 25 | die("%s: %s", msg, strerror(errno)); |
26 | return result; | 26 | return result; |
27 | } | 27 | } |
28 | 28 | ||
29 | int chk_non_negative(int result, char *msg) | 29 | int chk_non_negative(int result, char *msg) |
30 | { | 30 | { |
31 | if (result < 0) | 31 | if (result < 0) |
32 | die("%s: %s",msg, strerror(errno)); | 32 | die("%s: %s",msg, strerror(errno)); |
33 | return result; | 33 | return result; |
34 | } | 34 | } |
35 | 35 | ||
36 | struct cgit_repo *cgit_add_repo(const char *url) | 36 | struct cgit_repo *cgit_add_repo(const char *url) |
37 | { | 37 | { |
38 | struct cgit_repo *ret; | 38 | struct cgit_repo *ret; |
39 | 39 | ||
40 | if (++cgit_repolist.count > cgit_repolist.length) { | 40 | if (++cgit_repolist.count > cgit_repolist.length) { |
41 | if (cgit_repolist.length == 0) | 41 | if (cgit_repolist.length == 0) |
42 | cgit_repolist.length = 8; | 42 | cgit_repolist.length = 8; |
43 | else | 43 | else |
44 | cgit_repolist.length *= 2; | 44 | cgit_repolist.length *= 2; |
45 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 45 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
46 | cgit_repolist.length * | 46 | cgit_repolist.length * |
47 | sizeof(struct cgit_repo)); | 47 | sizeof(struct cgit_repo)); |
48 | } | 48 | } |
49 | 49 | ||
50 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 50 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
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; |
75 | } | 76 | } |
76 | return NULL; | 77 | return NULL; |
77 | } | 78 | } |
78 | 79 | ||
79 | void *cgit_free_commitinfo(struct commitinfo *info) | 80 | void *cgit_free_commitinfo(struct commitinfo *info) |
80 | { | 81 | { |
81 | free(info->author); | 82 | free(info->author); |
82 | free(info->author_email); | 83 | free(info->author_email); |
83 | free(info->committer); | 84 | free(info->committer); |
84 | free(info->committer_email); | 85 | free(info->committer_email); |
85 | free(info->subject); | 86 | free(info->subject); |
86 | free(info->msg); | 87 | free(info->msg); |
87 | free(info->msg_encoding); | 88 | free(info->msg_encoding); |
88 | free(info); | 89 | free(info); |
89 | return NULL; | 90 | return NULL; |
90 | } | 91 | } |
91 | 92 | ||
92 | char *trim_end(const char *str, char c) | 93 | char *trim_end(const char *str, char c) |
93 | { | 94 | { |
94 | int len; | 95 | int len; |
95 | char *s, *t; | 96 | char *s, *t; |
96 | 97 | ||
97 | if (str == NULL) | 98 | if (str == NULL) |
98 | return NULL; | 99 | return NULL; |
99 | t = (char *)str; | 100 | t = (char *)str; |
100 | len = strlen(t); | 101 | len = strlen(t); |
101 | while(len > 0 && t[len - 1] == c) | 102 | while(len > 0 && t[len - 1] == c) |
102 | len--; | 103 | len--; |
103 | 104 | ||
104 | if (len == 0) | 105 | if (len == 0) |
105 | return NULL; | 106 | return NULL; |
106 | 107 | ||
107 | c = t[len]; | 108 | c = t[len]; |
108 | t[len] = '\0'; | 109 | t[len] = '\0'; |
109 | s = xstrdup(t); | 110 | s = xstrdup(t); |
110 | t[len] = c; | 111 | t[len] = c; |
diff --git a/ui-repolist.c b/ui-repolist.c index cf27cb3..aa743bf 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -1,95 +1,103 @@ | |||
1 | /* ui-repolist.c: functions for generating the repolist page | 1 | /* ui-repolist.c: functions for generating the repolist page |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <time.h> | 9 | #include <time.h> |
10 | 10 | ||
11 | #include "cgit.h" | 11 | #include "cgit.h" |
12 | #include "html.h" | 12 | #include "html.h" |
13 | #include "ui-shared.h" | 13 | #include "ui-shared.h" |
14 | 14 | ||
15 | time_t read_agefile(char *path) | 15 | time_t read_agefile(char *path) |
16 | { | 16 | { |
17 | FILE *f; | 17 | FILE *f; |
18 | static char buf[64], buf2[64]; | 18 | static char buf[64], buf2[64]; |
19 | 19 | ||
20 | if (!(f = fopen(path, "r"))) | 20 | if (!(f = fopen(path, "r"))) |
21 | return -1; | 21 | return -1; |
22 | if (fgets(buf, sizeof(buf), f) == NULL) | 22 | if (fgets(buf, sizeof(buf), f) == NULL) |
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) |
60 | return 1; | 68 | return 1; |
61 | if (repo->url && strcasestr(repo->url, ctx.qry.search)) | 69 | if (repo->url && strcasestr(repo->url, ctx.qry.search)) |
62 | return 1; | 70 | return 1; |
63 | if (repo->name && strcasestr(repo->name, ctx.qry.search)) | 71 | if (repo->name && strcasestr(repo->name, ctx.qry.search)) |
64 | return 1; | 72 | return 1; |
65 | if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) | 73 | if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) |
66 | return 1; | 74 | return 1; |
67 | if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) | 75 | if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) |
68 | return 1; | 76 | return 1; |
69 | return 0; | 77 | return 0; |
70 | } | 78 | } |
71 | 79 | ||
72 | int is_in_url(struct cgit_repo *repo) | 80 | int is_in_url(struct cgit_repo *repo) |
73 | { | 81 | { |
74 | if (!ctx.qry.url) | 82 | if (!ctx.qry.url) |
75 | return 1; | 83 | return 1; |
76 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) | 84 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) |
77 | return 1; | 85 | return 1; |
78 | return 0; | 86 | return 0; |
79 | } | 87 | } |
80 | 88 | ||
81 | void print_sort_header(const char *title, const char *sort) | 89 | void print_sort_header(const char *title, const char *sort) |
82 | { | 90 | { |
83 | htmlf("<th class='left'><a href='./?s=%s", sort); | 91 | htmlf("<th class='left'><a href='./?s=%s", sort); |
84 | if (ctx.qry.search) { | 92 | if (ctx.qry.search) { |
85 | html("&q="); | 93 | html("&q="); |
86 | html_url_arg(ctx.qry.search); | 94 | html_url_arg(ctx.qry.search); |
87 | } | 95 | } |
88 | htmlf("'>%s</a></th>", title); | 96 | htmlf("'>%s</a></th>", title); |
89 | } | 97 | } |
90 | 98 | ||
91 | void print_header(int columns) | 99 | void print_header(int columns) |
92 | { | 100 | { |
93 | html("<tr class='nohover'>"); | 101 | html("<tr class='nohover'>"); |
94 | print_sort_header("Name", "name"); | 102 | print_sort_header("Name", "name"); |
95 | print_sort_header("Description", "desc"); | 103 | print_sort_header("Description", "desc"); |