Skip to content

Commit 3ebbd00

Browse files
jthillgitster
authored andcommitted
strbuf_read(): skip unnecessary strbuf_grow() at eof
The loop in strbuf_read() uses xread() repeatedly while extending the strbuf until the call returns zero. If the buffer is sufficiently large to begin with, this results in xread() returning the remainder of the file to the end (returning non-zero), the loop extending the strbuf, and then making another call to xread() to have it return zero. By using read_in_full(), we can tell when the read reached the end of file: when it returns less than was requested, it's eof. This way we can avoid an extra iteration that allocates an extra 8kB that is never used. Signed-off-by: Jim Hill <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 282616c commit 3ebbd00

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

strbuf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,19 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
348348

349349
strbuf_grow(sb, hint ? hint : 8192);
350350
for (;;) {
351-
ssize_t cnt;
351+
ssize_t want = sb->alloc - sb->len - 1;
352+
ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
352353

353-
cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
354-
if (cnt < 0) {
354+
if (got < 0) {
355355
if (oldalloc == 0)
356356
strbuf_release(sb);
357357
else
358358
strbuf_setlen(sb, oldlen);
359359
return -1;
360360
}
361-
if (!cnt)
361+
sb->len += got;
362+
if (got < want)
362363
break;
363-
sb->len += cnt;
364364
strbuf_grow(sb, 8192);
365365
}
366366

0 commit comments

Comments
 (0)