101 lines
2.9 KiB
Diff
101 lines
2.9 KiB
Diff
From 5726a6b4012cd41701927a6637b9f2070e7760ee Mon Sep 17 00:00:00 2001
|
|
From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?=
|
|
<avarab@gmail.com>
|
|
Date: Thu, 1 Jul 2021 12:51:26 +0200
|
|
Subject: [PATCH] *.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 <avarab@gmail.com>
|
|
Signed-off-by: Junio C Hamano <gitster@pobox.com>
|
|
---
|
|
credential.c | 4 ++--
|
|
json-writer.c | 6 ++----
|
|
run-command.c | 5 ++---
|
|
strbuf.c | 4 ++--
|
|
strmap.c | 3 ++-
|
|
strvec.c | 5 ++---
|
|
6 files changed, 12 insertions(+), 15 deletions(-)
|
|
|
|
diff --git a/credential.c b/credential.c
|
|
index e5202fbef2..3c05c7c669 100644
|
|
--- a/credential.c
|
|
+++ b/credential.c
|
|
@@ -10,8 +10,8 @@
|
|
|
|
void credential_init(struct credential *c)
|
|
{
|
|
- memset(c, 0, sizeof(*c));
|
|
- c->helpers.strdup_strings = 1;
|
|
+ struct credential blank = CREDENTIAL_INIT;
|
|
+ memcpy(c, &blank, sizeof(*c));
|
|
}
|
|
|
|
void credential_clear(struct credential *c)
|
|
diff --git a/json-writer.c b/json-writer.c
|
|
index aadb9dbddc..f1cfd8fa8c 100644
|
|
--- a/json-writer.c
|
|
+++ b/json-writer.c
|
|
@@ -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)
|
|
diff --git a/run-command.c b/run-command.c
|
|
index be6bc128cd..8750df16d8 100644
|
|
--- a/run-command.c
|
|
+++ b/run-command.c
|
|
@@ -11,9 +11,8 @@
|
|
|
|
void child_process_init(struct child_process *child)
|
|
{
|
|
- memset(child, 0, sizeof(*child));
|
|
- argv_array_init(&child->args);
|
|
- argv_array_init(&child->env_array);
|
|
+ struct child_process blank = CHILD_PROCESS_INIT;
|
|
+ memcpy(child, &blank, sizeof(*child));
|
|
}
|
|
|
|
void child_process_clear(struct child_process *child)
|
|
diff --git a/strbuf.c b/strbuf.c
|
|
index 4df30b4549..c8a5789694 100644
|
|
--- a/strbuf.c
|
|
+++ b/strbuf.c
|
|
@@ -52,8 +52,8 @@ char strbuf_slopbuf[1];
|
|
|
|
void strbuf_init(struct strbuf *sb, size_t hint)
|
|
{
|
|
- sb->alloc = sb->len = 0;
|
|
- sb->buf = strbuf_slopbuf;
|
|
+ struct strbuf blank = STRBUF_INIT;
|
|
+ memcpy(sb, &blank, sizeof(*sb));
|
|
if (hint)
|
|
strbuf_grow(sb, hint);
|
|
}
|
|
--
|
|
2.33.0
|
|
|