diff options
author | Eric Wong | 2019-01-01 13:03:08 +0000 |
---|---|---|
committer | June McEnroe | 2022-02-13 11:55:52 -0500 |
commit | a8e11db6570261d2a3d7d9396b796b72998b7ec5 (patch) | |
tree | f8f37dcb98ef6f5eb6976223c06c9b59aeed329f | |
parent | Remove redundant title on repo anchors (diff) | |
download | cgit-a8e11db6570261d2a3d7d9396b796b72998b7ec5.tar.gz cgit-a8e11db6570261d2a3d7d9396b796b72998b7ec5.zip |
Use buffered stdio
Our generation of HTML triggers many small write(2) syscalls which is inefficient. Time output on a horrible query against my git.git mirror shows significant performance improvement: QUERY_STRING='id=2b93bfac0f5bcabbf60f174f4e7bfa9e318e64d5&id2=d6da71a9d16b8cf27f9d8f90692d3625c849cbc8' PATH_INFO=/mirrors/git.git/diff export QUERY_STRING PATH_INFO time ./cgit >/dev/null Before: real 0m1.585s user 0m0.904s sys 0m0.658s After: real 0m0.750s user 0m0.666s sys 0m0.076s
-rw-r--r-- | cache.c | 7 | ||||
-rw-r--r-- | cgit.c | 2 | ||||
-rw-r--r-- | filter.c | 22 | ||||
-rw-r--r-- | html.c | 2 | ||||
-rw-r--r-- | ui-snapshot.c | 3 |
5 files changed, 33 insertions, 3 deletions
diff --git a/cache.c b/cache.c index 2c70be7..580c0e8 100644 --- a/cache.c +++ b/cache.c | |||
@@ -265,6 +265,13 @@ static int process_slot(struct cache_slot *slot) | |||
265 | { | 265 | { |
266 | int err; | 266 | int err; |
267 | 267 | ||
268 | /* | ||
269 | * Make sure any buffered data is flushed before we redirect, | ||
270 | * do sendfile(2) or write(2) | ||
271 | */ | ||
272 | if (fflush(stdout)) | ||
273 | return errno; | ||
274 | |||
268 | err = open_slot(slot); | 275 | err = open_slot(slot); |
269 | if (!err && slot->match) { | 276 | if (!err && slot->match) { |
270 | if (is_expired(slot)) { | 277 | if (is_expired(slot)) { |
diff --git a/cgit.c b/cgit.c index c4320f0..d8ea221 100644 --- a/cgit.c +++ b/cgit.c | |||
@@ -674,7 +674,7 @@ static inline void authenticate_post(void) | |||
674 | len = MAX_AUTHENTICATION_POST_BYTES; | 674 | len = MAX_AUTHENTICATION_POST_BYTES; |
675 | if ((len = read(STDIN_FILENO, buffer, len)) < 0) | 675 | if ((len = read(STDIN_FILENO, buffer, len)) < 0) |
676 | die_errno("Could not read POST from stdin"); | 676 | die_errno("Could not read POST from stdin"); |
677 | if (write(STDOUT_FILENO, buffer, len) < 0) | 677 | if (fwrite(buffer, 1, len, stdout) < len) |
678 | die_errno("Could not write POST to stdout"); | 678 | die_errno("Could not write POST to stdout"); |
679 | cgit_close_filter(ctx.cfg.auth_filter); | 679 | cgit_close_filter(ctx.cfg.auth_filter); |
680 | exit(0); | 680 | exit(0); |
diff --git a/filter.c b/filter.c index 70f5b74..fba26aa 100644 --- a/filter.c +++ b/filter.c | |||
@@ -48,6 +48,7 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap) | |||
48 | for (i = 0; i < filter->base.argument_count; i++) | 48 | for (i = 0; i < filter->base.argument_count; i++) |
49 | filter->argv[i + 1] = va_arg(ap, char *); | 49 | filter->argv[i + 1] = va_arg(ap, char *); |
50 | 50 | ||
51 | chk_zero(fflush(stdout), "unable to flush STDOUT"); | ||
51 | filter->old_stdout = chk_positive(dup(STDOUT_FILENO), | 52 | filter->old_stdout = chk_positive(dup(STDOUT_FILENO), |
52 | "Unable to duplicate STDOUT"); | 53 | "Unable to duplicate STDOUT"); |
53 | chk_zero(pipe(pipe_fh), "Unable to create pipe to subprocess"); | 54 | chk_zero(pipe(pipe_fh), "Unable to create pipe to subprocess"); |
@@ -71,6 +72,7 @@ static int close_exec_filter(struct cgit_filter *base) | |||
71 | struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; | 72 | struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; |
72 | int i, exit_status = 0; | 73 | int i, exit_status = 0; |
73 | 74 | ||
75 | chk_zero(fflush(stdout), "unable to flush STDOUT"); | ||
74 | chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), | 76 | chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), |
75 | "Unable to restore STDOUT"); | 77 | "Unable to restore STDOUT"); |
76 | close(filter->old_stdout); | 78 | close(filter->old_stdout); |
@@ -143,17 +145,32 @@ void cgit_init_filters(void) | |||
143 | #endif | 145 | #endif |
144 | 146 | ||
145 | #ifndef NO_LUA | 147 | #ifndef NO_LUA |
146 | static ssize_t (*libc_write)(int fd, const void *buf, size_t count); | 148 | static size_t (*libc_fwrite)(const void *buf, size_t size, size_t n, FILE *); |
149 | static ssize_t (*libc_write)(int fd, const void *buf, size_t size); | ||
147 | static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL; | 150 | static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL; |
148 | static struct cgit_filter *current_write_filter = NULL; | 151 | static struct cgit_filter *current_write_filter = NULL; |
149 | 152 | ||
150 | void cgit_init_filters(void) | 153 | void cgit_init_filters(void) |
151 | { | 154 | { |
155 | /* | ||
156 | * we need to wrap both functions since the Lua filter may | ||
157 | * have code which calls write(2) directly, bypassing fwrite(3) | ||
158 | */ | ||
159 | libc_fwrite = dlsym(RTLD_NEXT, "fwrite"); | ||
160 | if (!libc_fwrite) | ||
161 | die("Could not locate libc's write function"); | ||
152 | libc_write = dlsym(RTLD_NEXT, "write"); | 162 | libc_write = dlsym(RTLD_NEXT, "write"); |
153 | if (!libc_write) | 163 | if (!libc_write) |
154 | die("Could not locate libc's write function"); | 164 | die("Could not locate libc's write function"); |
155 | } | 165 | } |
156 | 166 | ||
167 | size_t fwrite(const void *buf, size_t size, size_t n, FILE *f) | ||
168 | { | ||
169 | if (f != stdout || !filter_write) | ||
170 | return libc_fwrite(buf, size, n, f); | ||
171 | return filter_write(current_write_filter, buf, size * n); | ||
172 | } | ||
173 | |||
157 | ssize_t write(int fd, const void *buf, size_t count) | 174 | ssize_t write(int fd, const void *buf, size_t count) |
158 | { | 175 | { |
159 | if (fd != STDOUT_FILENO || !filter_write) | 176 | if (fd != STDOUT_FILENO || !filter_write) |
@@ -305,6 +322,9 @@ static int open_lua_filter(struct cgit_filter *base, va_list ap) | |||
305 | struct lua_filter *filter = (struct lua_filter *)base; | 322 | struct lua_filter *filter = (struct lua_filter *)base; |
306 | int i; | 323 | int i; |
307 | 324 | ||
325 | if (fflush(stdout)) | ||
326 | return 1; | ||
327 | |||
308 | if (init_lua_filter(filter)) | 328 | if (init_lua_filter(filter)) |
309 | return 1; | 329 | return 1; |
310 | 330 | ||
diff --git a/html.c b/html.c index 7f81965..cefcf5e 100644 --- a/html.c +++ b/html.c | |||
@@ -80,7 +80,7 @@ char *fmtalloc(const char *format, ...) | |||
80 | 80 | ||
81 | void html_raw(const char *data, size_t size) | 81 | void html_raw(const char *data, size_t size) |
82 | { | 82 | { |
83 | if (write(STDOUT_FILENO, data, size) != size) | 83 | if (fwrite(data, 1, size, stdout) != size) |
84 | die_errno("write error on html output"); | 84 | die_errno("write error on html output"); |
85 | } | 85 | } |
86 | 86 | ||
diff --git a/ui-snapshot.c b/ui-snapshot.c index 556d3ed..8b81e37 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c | |||
@@ -37,6 +37,9 @@ static int write_archive_type(const char *format, const char *hex, const char *p | |||
37 | /* argv_array guarantees a trailing NULL entry. */ | 37 | /* argv_array guarantees a trailing NULL entry. */ |
38 | memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1)); | 38 | memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1)); |
39 | 39 | ||
40 | if (fflush(stdout)) | ||
41 | return errno; | ||
42 | |||
40 | result = write_archive(argv.argc, nargv, NULL, the_repository, NULL, 0); | 43 | result = write_archive(argv.argc, nargv, NULL, the_repository, NULL, 0); |
41 | argv_array_clear(&argv); | 44 | argv_array_clear(&argv); |
42 | free(nargv); | 45 | free(nargv); |