From 5726a6b4012cd41701927a6637b9f2070e7760ee Mon Sep 17 00:00:00 2001 From: Ævar Arnfjörð Bjarmason Date: Thu, 1 Jul 2021 12:51:26 +0200 Subject: *.c *_init(): define in terms of corresponding *_INIT macro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- strvec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'strvec.c') diff --git a/strvec.c b/strvec.c index 21dce0a7a4..61a76ce6cb 100644 --- a/strvec.c +++ b/strvec.c @@ -6,9 +6,8 @@ const char *empty_strvec[] = { NULL }; void strvec_init(struct strvec *array) { - array->v = empty_strvec; - array->nr = 0; - array->alloc = 0; + struct strvec blank = STRVEC_INIT; + memcpy(array, &blank, sizeof(*array)); } static void strvec_push_nodup(struct strvec *array, const char *value) -- cgit v1.2.3