Skip to content

Commit 3a73c1d

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d2ef3cb commit 3a73c1d

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

object-name.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,15 @@ static int init_object_disambiguation(struct repository *r,
354354
struct ambiguous_output {
355355
const struct disambiguate_state *ds;
356356
struct strbuf advice;
357+
struct strbuf sb;
357358
};
358359

359360
static int show_ambiguous_object(const struct object_id *oid, void *data)
360361
{
361362
struct ambiguous_output *state = data;
362363
const struct disambiguate_state *ds = state->ds;
363364
struct strbuf *advice = &state->advice;
364-
struct strbuf desc = STRBUF_INIT;
365+
struct strbuf *sb = &state->sb;
365366
int type;
366367
const char *hash;
367368

@@ -377,7 +378,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
377378
* output shown when we cannot look up or parse the
378379
* object in question. E.g. "deadbeef [bad object]".
379380
*/
380-
strbuf_addf(&desc, _("%s [bad object]"), hash);
381+
strbuf_addf(sb, _("%s [bad object]"), hash);
381382
goto out;
382383
}
383384

@@ -402,8 +403,8 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
402403
*
403404
* "deadbeef commit 2021-01-01 - Some Commit Message"
404405
*/
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);
407408

408409
strbuf_release(&date);
409410
strbuf_release(&msg);
@@ -423,7 +424,7 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
423424
* The third argument is the "tag" string
424425
* from object.c.
425426
*/
426-
strbuf_addf(&desc, _("%s tag %s - %s"), hash,
427+
strbuf_addf(sb, _("%s tag %s - %s"), hash,
427428
show_date(tag->date, 0, DATE_MODE(SHORT)),
428429
tag->tag);
429430
} else {
@@ -434,21 +435,21 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
434435
*
435436
* "deadbeef [bad tag, could not parse it]"
436437
*/
437-
strbuf_addf(&desc, _("%s [bad tag, could not parse it]"),
438+
strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
438439
hash);
439440
}
440441
} else if (type == OBJ_TREE) {
441442
/*
442443
* TRANSLATORS: This is a line of ambiguous <type>
443444
* object output. E.g. "deadbeef tree".
444445
*/
445-
strbuf_addf(&desc, _("%s tree"), hash);
446+
strbuf_addf(sb, _("%s tree"), hash);
446447
} else if (type == OBJ_BLOB) {
447448
/*
448449
* TRANSLATORS: This is a line of ambiguous <type>
449450
* object output. E.g. "deadbeef blob".
450451
*/
451-
strbuf_addf(&desc, _("%s blob"), hash);
452+
strbuf_addf(sb, _("%s blob"), hash);
452453
}
453454

454455

@@ -459,9 +460,9 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
459460
* you'll probably want to swap the "%s" and leading " " space
460461
* around.
461462
*/
462-
strbuf_addf(advice, _(" %s\n"), desc.buf);
463+
strbuf_addf(advice, _(" %s\n"), sb->buf);
463464

464-
strbuf_release(&desc);
465+
strbuf_reset(sb);
465466
return 0;
466467
}
467468

@@ -560,6 +561,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
560561
struct oid_array collect = OID_ARRAY_INIT;
561562
struct ambiguous_output out = {
562563
.ds = &ds,
564+
.sb = STRBUF_INIT,
563565
.advice = STRBUF_INIT,
564566
};
565567

@@ -589,6 +591,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
589591

590592
oid_array_clear(&collect);
591593
strbuf_release(&out.advice);
594+
strbuf_release(&out.sb);
592595
}
593596

594597
return status;

0 commit comments

Comments
 (0)