cat-file: add --buffer option

We use a direct write() to output the results of --batch and --batch-check. This is good for processes feeding the input and reading the output interactively, but it introduces measurable overhead if you do not want this feature. For example, on linux.git: $ git rev-list --objects --all | cut -d' ' -f1 >objects $ time git cat-file --batch-check='%(objectsize)' \ <objects >/dev/null real 0m5.440s user 0m5.060s sys 0m0.384s This patch adds an option to use regular stdio buffering: $ time git cat-file --batch-check='%(objectsize)' \ --buffer <objects >/dev/null real 0m4.975s user 0m4.888s sys 0m0.092s Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 22, 2015 at 06:45 UTC fc4937c37219347f4e2c25a271577b333942453f
2 files changed +26 -7
Documentation/git-cat-file.txt
+7
@@ -69,6 +69,13 @@ OPTIONS
69 not be combined with any other options or arguments. See the
70 section `BATCH OUTPUT` below for details.
71
72 +--buffer::
73 + Normally batch output is flushed after each object is output, so
74 + that a process can interactively read and write from
75 + `cat-file`. With this option, the output uses normal stdio
76 + buffering; this is much more efficient when invoking
77 + `--batch-check` on a large number of objects.
78 +
79 --allow-unknown-type::
80 Allow -s or -t to query broken/corrupt objects of unknown type.
81
builtin/cat-file.c
+19 -7
@@ -14,6 +14,7 @@ struct batch_options {
14 int enabled;
15 int follow_symlinks;
16 int print_contents;
17 + int buffer_output;
18 const char *format;
19 };
20
@@ -211,14 +212,25 @@ static size_t expand_format(struct strbuf *sb, const char *start, void *data)
212 return end - start + 1;
213 }
214
214 -static void print_object_or_die(int fd, struct expand_data *data)
215 +static void batch_write(struct batch_options *opt, const void *data, int len)
216 +{
217 + if (opt->buffer_output) {
218 + if (fwrite(data, 1, len, stdout) != len)
219 + die_errno("unable to write to stdout");
220 + } else
221 + write_or_die(1, data, len);
222 +}
223 +
224 +static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
225 {
226 const unsigned char *sha1 = data->sha1;
227
228 assert(data->info.typep);
229
230 if (data->type == OBJ_BLOB) {
221 - if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
231 + if (opt->buffer_output)
232 + fflush(stdout);
233 + if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
234 die("unable to stream %s to stdout", sha1_to_hex(sha1));
235 }
236 else {
@@ -234,12 +246,11 @@ static void print_object_or_die(int fd, struct expand_data *data)
246 if (data->info.sizep && size != data->size)
247 die("object %s changed size!?", sha1_to_hex(sha1));
248
237 - write_or_die(fd, contents, size);
249 + batch_write(opt, contents, size);
250 free(contents);
251 }
252 }
253
242 -
254 static int batch_one_object(const char *obj_name, struct batch_options *opt,
255 struct expand_data *data)
256 {
@@ -294,12 +305,12 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
305
306 strbuf_expand(&buf, opt->format, expand_format, data);
307 strbuf_addch(&buf, '\n');
297 - write_or_die(1, buf.buf, buf.len);
308 + batch_write(opt, buf.buf, buf.len);
309 strbuf_release(&buf);
310
311 if (opt->print_contents) {
301 - print_object_or_die(1, data);
302 - write_or_die(1, "\n", 1);
312 + print_object_or_die(opt, data);
313 + batch_write(opt, "\n", 1);
314 }
315 return 0;
316 }
@@ -415,6 +426,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
426 N_("for blob objects, run textconv on object's content"), 'c'),
427 OPT_BOOL(0, "allow-unknown-type", &unknown_type,
428 N_("allow -s and -t to work with broken/corrupt objects")),
429 + OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
430 { OPTION_CALLBACK, 0, "batch", &batch, "format",
431 N_("show info and content of objects fed from the standard input"),
432 PARSE_OPT_OPTARG, batch_option_callback },