Skip to content

Commit c108c8c

Browse files
committed
Merge branch 'zh/format-ref-array-optim'
"git (branch|tag) --format=..." has been micro-optimized. * zh/format-ref-array-optim: ref-filter: reuse output buffer ref-filter: get rid of show_ref_array_item
2 parents bb2feec + 844c3f0 commit c108c8c

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

builtin/branch.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
411411
{
412412
int i;
413413
struct ref_array array;
414+
struct strbuf out = STRBUF_INIT;
415+
struct strbuf err = STRBUF_INIT;
414416
int maxwidth = 0;
415417
const char *remote_prefix = "";
416418
char *to_free = NULL;
@@ -440,8 +442,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
440442
ref_array_sort(sorting, &array);
441443

442444
for (i = 0; i < array.nr; i++) {
443-
struct strbuf out = STRBUF_INIT;
444-
struct strbuf err = STRBUF_INIT;
445+
strbuf_reset(&err);
446+
strbuf_reset(&out);
445447
if (format_ref_array_item(array.items[i], format, &out, &err))
446448
die("%s", err.buf);
447449
if (column_active(colopts)) {
@@ -452,10 +454,10 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
452454
fwrite(out.buf, 1, out.len, stdout);
453455
putchar('\n');
454456
}
455-
strbuf_release(&err);
456-
strbuf_release(&out);
457457
}
458458

459+
strbuf_release(&err);
460+
strbuf_release(&out);
459461
ref_array_clear(&array);
460462
free(to_free);
461463
}

builtin/for-each-ref.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
2222
struct ref_array array;
2323
struct ref_filter filter;
2424
struct ref_format format = REF_FORMAT_INIT;
25+
struct strbuf output = STRBUF_INIT;
26+
struct strbuf err = STRBUF_INIT;
2527

2628
struct option opts[] = {
2729
OPT_BIT('s', "shell", &format.quote_style,
@@ -80,8 +82,17 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
8082

8183
if (!maxcount || array.nr < maxcount)
8284
maxcount = array.nr;
83-
for (i = 0; i < maxcount; i++)
84-
show_ref_array_item(array.items[i], &format);
85+
for (i = 0; i < maxcount; i++) {
86+
strbuf_reset(&err);
87+
strbuf_reset(&output);
88+
if (format_ref_array_item(array.items[i], &format, &output, &err))
89+
die("%s", err.buf);
90+
fwrite(output.buf, 1, output.len, stdout);
91+
putchar('\n');
92+
}
93+
94+
strbuf_release(&err);
95+
strbuf_release(&output);
8596
ref_array_clear(&array);
8697
return 0;
8798
}

builtin/tag.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
3939
struct ref_format *format)
4040
{
4141
struct ref_array array;
42+
struct strbuf output = STRBUF_INIT;
43+
struct strbuf err = STRBUF_INIT;
4244
char *to_free = NULL;
4345
int i;
4446

@@ -63,8 +65,17 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
6365
filter_refs(&array, filter, FILTER_REFS_TAGS);
6466
ref_array_sort(sorting, &array);
6567

66-
for (i = 0; i < array.nr; i++)
67-
show_ref_array_item(array.items[i], format);
68+
for (i = 0; i < array.nr; i++) {
69+
strbuf_reset(&output);
70+
strbuf_reset(&err);
71+
if (format_ref_array_item(array.items[i], format, &output, &err))
72+
die("%s", err.buf);
73+
fwrite(output.buf, 1, output.len, stdout);
74+
putchar('\n');
75+
}
76+
77+
strbuf_release(&err);
78+
strbuf_release(&output);
6879
ref_array_clear(&array);
6980
free(to_free);
7081

ref-filter.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,27 +2435,22 @@ int format_ref_array_item(struct ref_array_item *info,
24352435
return 0;
24362436
}
24372437

2438-
void show_ref_array_item(struct ref_array_item *info,
2439-
const struct ref_format *format)
2440-
{
2441-
struct strbuf final_buf = STRBUF_INIT;
2442-
struct strbuf error_buf = STRBUF_INIT;
2443-
2444-
if (format_ref_array_item(info, format, &final_buf, &error_buf))
2445-
die("%s", error_buf.buf);
2446-
fwrite(final_buf.buf, 1, final_buf.len, stdout);
2447-
strbuf_release(&error_buf);
2448-
strbuf_release(&final_buf);
2449-
putchar('\n');
2450-
}
2451-
24522438
void pretty_print_ref(const char *name, const struct object_id *oid,
24532439
const struct ref_format *format)
24542440
{
24552441
struct ref_array_item *ref_item;
2442+
struct strbuf output = STRBUF_INIT;
2443+
struct strbuf err = STRBUF_INIT;
2444+
24562445
ref_item = new_ref_array_item(name, oid);
24572446
ref_item->kind = ref_kind_from_refname(name);
2458-
show_ref_array_item(ref_item, format);
2447+
if (format_ref_array_item(ref_item, format, &output, &err))
2448+
die("%s", err.buf);
2449+
fwrite(output.buf, 1, output.len, stdout);
2450+
putchar('\n');
2451+
2452+
strbuf_release(&err);
2453+
strbuf_release(&output);
24592454
free_array_item(ref_item);
24602455
}
24612456

ref-filter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ int format_ref_array_item(struct ref_array_item *info,
119119
const struct ref_format *format,
120120
struct strbuf *final_buf,
121121
struct strbuf *error_buf);
122-
/* Print the ref using the given format and quote_style */
123-
void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
124122
/* Parse a single sort specifier and add it to the list */
125123
void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
126124
/* Callback function for parsing the sort option */

0 commit comments

Comments
 (0)