@matheusbsbs / matheus-bagliano / commits / 06f59e9f5d
Fork 0

Don't fflush(stdout) when it's not helpful

This patch arose from a discussion started by Jim Meyering's patch whose intention was to provide better diagnostics for failed writes. Linus proposed a better way to do things, which also had the added benefit that adding a fflush() to git-log-* operations and incremental git-blame operations could improve interactive respose time feel, at the cost of making things a bit slower when we aren't piping the output to a downstream program. This patch skips the fflush() calls when stdout is a regular file, or if the environment variable GIT_FLUSH is set to "0". This latter can speed up a command such as: GIT_FLUSH=0 strace -c -f -e write time git-rev-list HEAD | wc -l a tiny amount. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Theodore Ts'o committed Jun 29, 2007 at 13:40 UTC 06f59e9f5daa06fc4bd51cf4c508b3edd3ed514a
6 files changed +55 -1
Documentation/git.txt
+10
@@ -396,6 +396,16 @@ other
396 'GIT_PAGER'::
397 This environment variable overrides `$PAGER`.
398
399 +'GIT_FLUSH'::
400 + If this environment variable is set to "1", then commands such
401 + as git-blame (in incremental mode), git-rev-list, git-log,
402 + git-whatchanged, etc., will force a flush of the output stream
403 + after each commit-oriented record have been flushed. If this
404 + variable is set to "0", the output of these commands will be done
405 + using completely buffered I/O. If this environment variable is
406 + not set, git will choose buffered or record-oriented flushing
407 + based on whether stdout appears to be redirected to a file or not.
408 +
409 'GIT_TRACE'::
410 If this variable is set to "1", "2" or "true" (comparison
411 is case insensitive), git will print `trace:` messages on
builtin-blame.c
+1
@@ -1459,6 +1459,7 @@ static void found_guilty_entry(struct blame_entry *ent)
1459 printf("boundary\n");
1460 }
1461 write_filename_info(suspect->path);
1462 + maybe_flush_or_die(stdout, "stdout");
1463 }
1464 }
1465
builtin-rev-list.c
+1 -1
@@ -100,7 +100,7 @@ static void show_commit(struct commit *commit)
100 printf("%s%c", buf, hdr_termination);
101 free(buf);
102 }
103 - fflush(stdout);
103 + maybe_flush_or_die(stdout, "stdout");
104 if (commit->parents) {
105 free_commit_list(commit->parents);
106 commit->parents = NULL;
cache.h
+2
@@ -532,6 +532,8 @@ extern char git_default_name[MAX_GITNAME];
532 extern const char *git_commit_encoding;
533 extern const char *git_log_output_encoding;
534
535 +/* IO helper functions */
536 +extern void maybe_flush_or_die(FILE *, const char *);
537 extern int copy_fd(int ifd, int ofd);
538 extern int read_in_full(int fd, void *buf, size_t count);
539 extern int write_in_full(int fd, const void *buf, size_t count);
log-tree.c
+1
@@ -408,5 +408,6 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
408 shown = 1;
409 }
410 opt->loginfo = NULL;
411 + maybe_flush_or_die(stdout, "stdout");
412 return shown;
413 }
write_or_die.c
+40
@@ -1,5 +1,45 @@
1 #include "cache.h"
2
3 +/*
4 + * Some cases use stdio, but want to flush after the write
5 + * to get error handling (and to get better interactive
6 + * behaviour - not buffering excessively).
7 + *
8 + * Of course, if the flush happened within the write itself,
9 + * we've already lost the error code, and cannot report it any
10 + * more. So we just ignore that case instead (and hope we get
11 + * the right error code on the flush).
12 + *
13 + * If the file handle is stdout, and stdout is a file, then skip the
14 + * flush entirely since it's not needed.
15 + */
16 +void maybe_flush_or_die(FILE *f, const char *desc)
17 +{
18 + static int skip_stdout_flush = -1;
19 + struct stat st;
20 + char *cp;
21 +
22 + if (f == stdout) {
23 + if (skip_stdout_flush < 0) {
24 + cp = getenv("GIT_FLUSH");
25 + if (cp)
26 + skip_stdout_flush = (atoi(cp) == 0);
27 + else if ((fstat(fileno(stdout), &st) == 0) &&
28 + S_ISREG(st.st_mode))
29 + skip_stdout_flush = 1;
30 + else
31 + skip_stdout_flush = 0;
32 + }
33 + if (skip_stdout_flush && !ferror(f))
34 + return;
35 + }
36 + if (fflush(f)) {
37 + if (errno == EPIPE)
38 + exit(0);
39 + die("write failure on %s: %s", desc, strerror(errno));
40 + }
41 +}
42 +
43 int read_in_full(int fd, void *buf, size_t count)
44 {
45 char *p = buf;