strbuf: avoid static variables in strbuf_add_commented_lines()
In strbuf_add_commented_lines(), we have to convert the single-byte comment_line_char into a string to pass to add_lines(). We cache the created string using a static-local variable. But this makes the function non-reentrant, and it's doubtful that this provides any real performance benefit given that we know the string always contains a single character. So let's just create it from scratch each time, and to give the compiler the maximal opportunity to make it fast we'll ditch the over-complicated xsnprintf() and just assign directly into the array. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 12, 2024 at 05:17 UTC
3b45450db65b97f010420229457c529f69bb2168
1 file changed
+3
-3
strbuf.c
+3
-3
@@ -361,10 +361,10 @@ static void add_lines(struct strbuf *out,
361
void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
362
size_t size, char comment_line_char)
363
{
364
- static char prefix[2];
364
+ char prefix[2];
365
366
- if (prefix[0] != comment_line_char)
367
- xsnprintf(prefix, sizeof(prefix), "%c", comment_line_char);
366
+ prefix[0] = comment_line_char;
367
+ prefix[1] = '\0';
368
add_lines(out, prefix, buf, size, 1);
369
}
370