From: ZheNing Hu <adlternative@xxxxxxxxx> Because finnal_buf will be cleared before every time we call format_ref_array_item(). So we actually add content to an empty strbuf, We can add the object's data directly to this finnal_buffer instead of adding objects' data to state.stack->output first, then copying to finnal_buf. So add a can_reuse_final_buffer flag to struct ref_format to check if we are use %(align), %(end), %(if), %(then), %(else). if not, we can reuse the buf of finnal_buf. This can bring 2% performance improvement for git cat-file --batch. Mentored-by: Christian Couder <christian.couder@xxxxxxxxx> Mentored-by: Hariom Verma <hariom18599@xxxxxxxxx> Signed-off-by: ZheNing Hu <adlternative@xxxxxxxxx> --- ref-filter.c | 36 +++++++++++++++++++++++++++++------- ref-filter.h | 3 ++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 6fbf38f1375..4d7aa60da0f 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1083,6 +1083,13 @@ int verify_ref_format(struct ref_format *format) if (reject_atom(format->cat_file_mode, used_atom[at].atom_type)) die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2); + if (used_atom[at].atom_type == ATOM_ALIGN || + used_atom[at].atom_type == ATOM_END || + used_atom[at].atom_type == ATOM_IF || + used_atom[at].atom_type == ATOM_THEN || + used_atom[at].atom_type == ATOM_ELSE) + format->can_reuse_final_buffer = 0; + if ((format->quote_style == QUOTE_PYTHON || format->quote_style == QUOTE_SHELL || format->quote_style == QUOTE_TCL) && @@ -2710,7 +2717,14 @@ int format_ref_array_item(struct ref_array_item *info, int ret; state.quote_style = format->quote_style; - push_stack_element(&state.stack); + if (format->can_reuse_final_buffer) { + struct ref_formatting_stack *s = xmalloc(sizeof(struct ref_formatting_stack)); + s->output = *final_buf; + s->prev = state.stack; + state.stack = s; + } else { + push_stack_element(&state.stack); + } info->can_skip_parse_buffer = format->can_skip_parse_buffer; cp = format->format; @@ -2722,9 +2736,10 @@ int format_ref_array_item(struct ref_array_item *info, if (cp < e->beg - 2) append_literal(cp, e->beg - 2, &state); - if ((ret = get_ref_atom_value(info, e->at, &atomv, error_buf)) || - atomv->handler(atomv, &state, error_buf)) { - pop_stack_element(&state.stack); + if (((ret = get_ref_atom_value(info, e->at, &atomv, error_buf)) || + atomv->handler(atomv, &state, error_buf))) { + if (!format->can_reuse_final_buffer) + pop_stack_element(&state.stack); return ret ? ret : -1; } cp = e->end + 1; @@ -2739,16 +2754,23 @@ int format_ref_array_item(struct ref_array_item *info, struct atom_value resetv = ATOM_VALUE_INIT; resetv.s = GIT_COLOR_RESET; if (append_atom(&resetv, &state, error_buf)) { - pop_stack_element(&state.stack); + if (!format->can_reuse_final_buffer) + pop_stack_element(&state.stack); return -1; } } if (state.stack->prev) { + assert(!format->can_reuse_final_buffer); pop_stack_element(&state.stack); return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing")); } - strbuf_addbuf(final_buf, &state.stack->output); - pop_stack_element(&state.stack); + if(format->can_reuse_final_buffer) { + *final_buf = state.stack->output; + free(state.stack); + } else { + strbuf_addbuf(final_buf, &state.stack->output); + pop_stack_element(&state.stack); + } return 0; } diff --git a/ref-filter.h b/ref-filter.h index 2b871f9ed4f..bf1d2fa18c6 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -94,10 +94,11 @@ struct ref_format { int can_skip_parse_buffer; /* Internal state to ref-filter */ int need_color_reset_at_eol; + int can_reuse_final_buffer; struct list_head parsed_atom_head; }; -#define REF_FORMAT_INIT { .use_color = -1, .can_skip_parse_buffer = 1 } +#define REF_FORMAT_INIT { .use_color = -1, .can_skip_parse_buffer = 1, .can_reuse_final_buffer = 1 } /* Macros for checking --merged and --no-merged options */ #define _OPT_MERGED_NO_MERGED(option, filter, h) \ -- gitgitgadget