Skip to content

Commit 889860e

Browse files
committed
Merge branch 'jc/cat-file-batch-default-format-optim'
Optimize away strbuf_expand() call with a hardcoded formatting logic specific for the default format in the --batch and --batch-check options of "git cat-file". * jc/cat-file-batch-default-format-optim: cat-file: skip expanding default format
2 parents 8351033 + eb54a33 commit 889860e

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

builtin/cat-file.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
360360
}
361361
}
362362

363+
static void print_default_format(struct strbuf *scratch, struct expand_data *data)
364+
{
365+
strbuf_addf(scratch, "%s %s %"PRIuMAX"\n", oid_to_hex(&data->oid),
366+
type_name(data->type),
367+
(uintmax_t)data->size);
368+
}
369+
363370
/*
364371
* If "pack" is non-NULL, then "offset" is the byte offset within the pack from
365372
* which the object may be accessed (though note that we may also rely on
@@ -391,8 +398,14 @@ static void batch_object_write(const char *obj_name,
391398
}
392399

393400
strbuf_reset(scratch);
394-
strbuf_expand(scratch, opt->format, expand_format, data);
395-
strbuf_addch(scratch, '\n');
401+
402+
if (!opt->format) {
403+
print_default_format(scratch, data);
404+
} else {
405+
strbuf_expand(scratch, opt->format, expand_format, data);
406+
strbuf_addch(scratch, '\n');
407+
}
408+
396409
batch_write(opt, scratch->buf, scratch->len);
397410

398411
if (opt->batch_mode == BATCH_MODE_CONTENTS) {
@@ -646,6 +659,8 @@ static void batch_objects_command(struct batch_options *opt,
646659
strbuf_release(&input);
647660
}
648661

662+
#define DEFAULT_FORMAT "%(objectname) %(objecttype) %(objectsize)"
663+
649664
static int batch_objects(struct batch_options *opt)
650665
{
651666
struct strbuf input = STRBUF_INIT;
@@ -654,22 +669,24 @@ static int batch_objects(struct batch_options *opt)
654669
int save_warning;
655670
int retval = 0;
656671

657-
if (!opt->format)
658-
opt->format = "%(objectname) %(objecttype) %(objectsize)";
659-
660672
/*
661673
* Expand once with our special mark_query flag, which will prime the
662674
* object_info to be handed to oid_object_info_extended for each
663675
* object.
664676
*/
665677
memset(&data, 0, sizeof(data));
666678
data.mark_query = 1;
667-
strbuf_expand(&output, opt->format, expand_format, &data);
679+
strbuf_expand(&output,
680+
opt->format ? opt->format : DEFAULT_FORMAT,
681+
expand_format,
682+
&data);
668683
data.mark_query = 0;
669684
strbuf_release(&output);
670685
if (opt->transform_mode)
671686
data.split_on_whitespace = 1;
672687

688+
if (opt->format && !strcmp(opt->format, DEFAULT_FORMAT))
689+
opt->format = NULL;
673690
/*
674691
* If we are printing out the object, then always fill in the type,
675692
* since we will want to decide whether or not to stream.

t/perf/p1006-cat-file.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
test_description='Tests listing object info performance'
4+
. ./perf-lib.sh
5+
6+
test_perf_large_repo
7+
8+
test_perf 'cat-file --batch-check' '
9+
git cat-file --batch-all-objects --batch-check
10+
'
11+
12+
test_done

0 commit comments

Comments
 (0)