object-name: re-use "struct strbuf" in show_ambiguous_object()
Reduce the allocations done by show_ambiguous_object() by moving the "desc" strbuf into the "struct ambiguous_output" introduced in the preceding commit. This doesn't matter for optimization purposes, but since we're accumulating a "struct strbuf advice" anyway let's follow that pattern and add a "struct strbuf sb", we can then strbuf_reset() it rather than calling strbuf_release() for each call to show_ambiguous_object(). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Jan 27, 2022 at 06:26 UTC
3a73c1dfafcc53831a252fc3aededeb59be476f1
1 file changed
+13
-10
object-name.c
+13
-10
@@ -354,6 +354,7 @@ static int init_object_disambiguation(struct repository *r,
354
struct ambiguous_output {
355
const struct disambiguate_state *ds;
356
struct strbuf advice;
357
+ struct strbuf sb;
358
};
359
360
static int show_ambiguous_object(const struct object_id *oid, void *data)
@@ -361,7 +362,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
362
struct ambiguous_output *state = data;
363
const struct disambiguate_state *ds = state->ds;
364
struct strbuf *advice = &state->advice;
364
- struct strbuf desc = STRBUF_INIT;
365
+ struct strbuf *sb = &state->sb;
366
int type;
367
const char *hash;
368
@@ -377,7 +378,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
378
* output shown when we cannot look up or parse the
379
* object in question. E.g. "deadbeef [bad object]".
380
*/
380
- strbuf_addf(&desc, _("%s [bad object]"), hash);
381
+ strbuf_addf(sb, _("%s [bad object]"), hash);
382
goto out;
383
}
384
@@ -402,8 +403,8 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
403
*
404
* "deadbeef commit 2021-01-01 - Some Commit Message"
405
*/
405
- strbuf_addf(&desc, _("%s commit %s - %s"),
406
- hash, date.buf, msg.buf);
406
+ strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf,
407
+ msg.buf);
408
409
strbuf_release(&date);
410
strbuf_release(&msg);
@@ -423,7 +424,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
424
* The third argument is the "tag" string
425
* from object.c.
426
*/
426
- strbuf_addf(&desc, _("%s tag %s - %s"), hash,
427
+ strbuf_addf(sb, _("%s tag %s - %s"), hash,
428
show_date(tag->date, 0, DATE_MODE(SHORT)),
429
tag->tag);
430
} else {
@@ -434,7 +435,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
435
*
436
* "deadbeef [bad tag, could not parse it]"
437
*/
437
- strbuf_addf(&desc, _("%s [bad tag, could not parse it]"),
438
+ strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
439
hash);
440
}
441
} else if (type == OBJ_TREE) {
@@ -442,13 +443,13 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
443
* TRANSLATORS: This is a line of ambiguous <type>
444
* object output. E.g. "deadbeef tree".
445
*/
445
- strbuf_addf(&desc, _("%s tree"), hash);
446
+ strbuf_addf(sb, _("%s tree"), hash);
447
} else if (type == OBJ_BLOB) {
448
/*
449
* TRANSLATORS: This is a line of ambiguous <type>
450
* object output. E.g. "deadbeef blob".
451
*/
451
- strbuf_addf(&desc, _("%s blob"), hash);
452
+ strbuf_addf(sb, _("%s blob"), hash);
453
}
454
455
@@ -459,9 +460,9 @@ out:
460
* you'll probably want to swap the "%s" and leading " " space
461
* around.
462
*/
462
- strbuf_addf(advice, _(" %s\n"), desc.buf);
463
+ strbuf_addf(advice, _(" %s\n"), sb->buf);
464
464
- strbuf_release(&desc);
465
+ strbuf_reset(sb);
466
return 0;
467
}
468
@@ -560,6 +561,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
561
struct oid_array collect = OID_ARRAY_INIT;
562
struct ambiguous_output out = {
563
.ds = &ds,
564
+ .sb = STRBUF_INIT,
565
.advice = STRBUF_INIT,
566
};
567
@@ -589,6 +591,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
591
592
oid_array_clear(&collect);
593
strbuf_release(&out.advice);
594
+ strbuf_release(&out.sb);
595
}
596
597
return status;