From: ZheNing Hu <adlternative@xxxxxxxxx> Reuse the `err` buffer in batch_object_write(), as the buffer `scratch` does. This will reduce the overhead of multiple allocations of memory of the err buffer. Mentored-by: Christian Couder <christian.couder@xxxxxxxxx> Mentored-by: Hariom Verma <hariom18599@xxxxxxxxx> Signed-off-by: ZheNing Hu <adlternative@xxxxxxxxx> --- builtin/cat-file.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index cbedb8e1c471..b5204493bd56 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -212,32 +212,33 @@ static void batch_write(struct batch_options *opt, const void *data, int len) static void batch_object_write(const char *obj_name, struct strbuf *scratch, + struct strbuf *err, struct batch_options *opt, struct expand_data *data) { int ret = 0; - struct strbuf err = STRBUF_INIT; struct ref_array_item item = { data->oid, data->rest }; strbuf_reset(scratch); + strbuf_reset(err); - ret = format_ref_array_item(&item, &opt->format, scratch, &err); + ret = format_ref_array_item(&item, &opt->format, scratch, err); if (!ret) { strbuf_addch(scratch, '\n'); batch_write(opt, scratch->buf, scratch->len); } else if (ret < 0) { - die("%s\n", err.buf); + die("%s\n", err->buf); } else { /* when ret > 0 , don't call die and print the err to stdout*/ - printf("%s\n", err.buf); + printf("%s\n", err->buf); fflush(stdout); } free_ref_array_item_value(&item); - strbuf_release(&err); } static void batch_one_object(const char *obj_name, struct strbuf *scratch, + struct strbuf *err, struct batch_options *opt, struct expand_data *data) { @@ -291,7 +292,7 @@ static void batch_one_object(const char *obj_name, return; } - batch_object_write(obj_name, scratch, opt, data); + batch_object_write(obj_name, scratch, err, opt, data); } struct object_cb_data { @@ -299,13 +300,14 @@ struct object_cb_data { struct expand_data *expand; struct oidset *seen; struct strbuf *scratch; + struct strbuf *err; }; static int batch_object_cb(const struct object_id *oid, void *vdata) { struct object_cb_data *data = vdata; oidcpy(&data->expand->oid, oid); - batch_object_write(NULL, data->scratch, data->opt, data->expand); + batch_object_write(NULL, data->scratch, data->err, data->opt, data->expand); return 0; } @@ -361,6 +363,7 @@ static int batch_objects(struct batch_options *opt, const struct option *options { struct strbuf input = STRBUF_INIT; struct strbuf output = STRBUF_INIT; + struct strbuf err = STRBUF_INIT; struct strbuf format = STRBUF_INIT; struct expand_data data; int save_warning; @@ -389,6 +392,7 @@ static int batch_objects(struct batch_options *opt, const struct option *options cb.opt = opt; cb.expand = &data; cb.scratch = &output; + cb.err = &err; if (opt->unordered) { struct oidset seen = OIDSET_INIT; @@ -413,6 +417,7 @@ static int batch_objects(struct batch_options *opt, const struct option *options strbuf_release(&format); strbuf_release(&output); + strbuf_release(&err); return 0; } @@ -441,12 +446,13 @@ static int batch_objects(struct batch_options *opt, const struct option *options data.rest = p; } - batch_one_object(input.buf, &output, opt, &data); + batch_one_object(input.buf, &output, &err, opt, &data); } strbuf_release(&format); strbuf_release(&input); strbuf_release(&output); + strbuf_release(&err); warn_on_object_refname_ambiguity = save_warning; return retval; } -- gitgitgadget