*.c *_init(): define in terms of corresponding *_INIT macro

Change the common patter in the codebase of duplicating the
initialization logic between an *_INIT macro and a
corresponding *_init() function to use the macro as the canonical
source of truth.

Now we no longer need to keep the function up-to-date with the macro
version. This implements a suggestion by Jeff King who found that
under -O2 [1] modern compilers will init new version in place without
the extra copy[1]. The performance of a single *_init() won't matter
in most cases, but even if it does we're going to be producing
efficient machine code to perform these operations.

1. https://lore.kernel.org/git/YNyrDxUO1PlGJvCn@coredump.intra.peff.net/

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Ævar Arnfjörð Bjarmason
2021-07-01 12:51:26 +02:00
committed by Junio C Hamano
parent 3d97ea479f
commit 5726a6b401
6 changed files with 12 additions and 15 deletions

View File

@ -3,10 +3,8 @@
void jw_init(struct json_writer *jw)
{
strbuf_init(&jw->json, 0);
strbuf_init(&jw->open_stack, 0);
jw->need_comma = 0;
jw->pretty = 0;
struct json_writer blank = JSON_WRITER_INIT;
memcpy(jw, &blank, sizeof(*jw));;
}
void jw_release(struct json_writer *jw)