Skip to content

Commit f80c153

Browse files
peffgitster
authored andcommitted
strbuf_getwholeline: avoid calling strbuf_grow
As with the recent speedup to strbuf_addch, we can avoid calling strbuf_grow() in a tight loop of single-character adds by instead checking strbuf_avail. Note that we would instead call strbuf_addch directly here, but it does more work than necessary: it will NUL-terminate the result for each character read. Instead, in this loop we read the characters one by one and then add the terminator manually at the end. Running "git rev-parse refs/heads/does-not-exist" on a repo with an extremely large (1.6GB) packed-refs file went from (best-of-5): real 0m10.948s user 0m10.548s sys 0m0.412s to: real 0m8.601s user 0m8.084s sys 0m0.524s for a wall-clock speedup of 21%. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fec501d commit f80c153

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

strbuf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
445445
strbuf_reset(sb);
446446
flockfile(fp);
447447
while ((ch = getc_unlocked(fp)) != EOF) {
448-
strbuf_grow(sb, 1);
448+
if (!strbuf_avail(sb))
449+
strbuf_grow(sb, 1);
449450
sb->buf[sb->len++] = ch;
450451
if (ch == term)
451452
break;

0 commit comments

Comments
 (0)