Skip to content

Commit 3b45450

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent db7f930 commit 3b45450

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

strbuf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,10 @@ static void add_lines(struct strbuf *out,
361361
void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
362362
size_t size, char comment_line_char)
363363
{
364-
static char prefix[2];
364+
char prefix[2];
365365

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';
368368
add_lines(out, prefix, buf, size, 1);
369369
}
370370

0 commit comments

Comments
 (0)