From: Siddharth Shrimali <r.siddharth.shrimali@gmail•com>
To: git@vger•kernel.org
Cc: gitster@pobox•com, ps@pks•im, jonatan@jontes•page,
r.siddharth.shrimali@gmail•com
Subject: [PATCH] builtin/help.c: move strbuf out of help loops
Date: Tue, 10 Mar 2026 12:33:28 +0530 [thread overview]
Message-ID: <20260310070328.29836-1-r.siddharth.shrimali@gmail.com> (raw)
In list_config_help(), a strbuf was being initialized and released
inside two separate loops. This caused unnecessary memory allocation
and deallocation on every iteration.
Move the strbuf declaration to the top of the function and use
strbuf_reset() inside the loops to reuse the same buffer. Similarly
release() the buffer at the end of the function to free the memory.
This improves performance by avoiding repeated heap pressure by reducing
the number of allocations.
This also fixes a minor memory leak when the SHOW_CONFIG_HUMAN case
triggers a continue.
Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail•com>
---
builtin/help.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/builtin/help.c b/builtin/help.c
index 86a3d03a9b..07398b430e 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -134,10 +134,10 @@ static void list_config_help(enum show_config_type type)
struct string_list keys = STRING_LIST_INIT_DUP;
struct string_list keys_uniq = STRING_LIST_INIT_DUP;
struct string_list_item *item;
+ struct strbuf sb = STRBUF_INIT;
for (p = config_name_list; *p; p++) {
const char *var = *p;
- struct strbuf sb = STRBUF_INIT;
for (e = slot_expansions; e->prefix; e++) {
@@ -149,7 +149,6 @@ static void list_config_help(enum show_config_type type)
break;
}
}
- strbuf_release(&sb);
if (!e->prefix)
string_list_append(&keys, var);
}
@@ -161,10 +160,10 @@ static void list_config_help(enum show_config_type type)
string_list_sort(&keys);
for (size_t i = 0; i < keys.nr; i++) {
+ strbuf_reset(&sb);
const char *var = keys.items[i].string;
const char *wildcard, *tag, *cut;
const char *dot = NULL;
- struct strbuf sb = STRBUF_INIT;
switch (type) {
case SHOW_CONFIG_HUMAN:
@@ -195,13 +194,13 @@ static void list_config_help(enum show_config_type type)
strbuf_add(&sb, var, cut - var);
string_list_append(&keys_uniq, sb.buf);
- strbuf_release(&sb);
}
string_list_clear(&keys, 0);
string_list_remove_duplicates(&keys_uniq, 0);
for_each_string_list_item(item, &keys_uniq)
puts(item->string);
+ strbuf_release(&sb);
string_list_clear(&keys_uniq, 0);
}
--
2.51.2
next reply other threads:[~2026-03-10 7:03 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 7:03 Siddharth Shrimali [this message]
2026-03-10 12:41 ` [PATCH] builtin/help.c: move strbuf out of help loops Patrick Steinhardt
2026-03-10 16:00 ` [PATCH v2] " Siddharth Shrimali
2026-03-10 20:33 ` Junio C Hamano
2026-03-11 18:13 ` Siddharth Shrimali
2026-03-11 19:30 ` Amisha Chhajed
2026-03-11 19:48 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260310070328.29836-1-r.siddharth.shrimali@gmail.com \
--to=r.siddharth.shrimali@gmail$(echo .)com \
--cc=git@vger$(echo .)kernel.org \
--cc=gitster@pobox$(echo .)com \
--cc=jonatan@jontes$(echo .)page \
--cc=ps@pks$(echo .)im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox