git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jim Hill <gjthill@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH] strbuf_read: skip unnecessary strbuf_grow at eof
Date: Sun, 31 May 2015 11:16:45 -0700	[thread overview]
Message-ID: <1433096205-14516-1-git-send-email-gjthill@gmail.com> (raw)

Make strbuf_read not try to do read_in_full's job too.  If xread returns
less than was requested it can be either eof or an interrupted read.  If
read_in_full returns less than was requested, it's eof. Use read_in_full
to detect eof and not iterate when eof has been seen.

Signed-off-by: Jim Hill <gjthill@gmail.com>
---
 strbuf.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 88cafd4..c70733e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -364,19 +364,19 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 
 	strbuf_grow(sb, hint ? hint : 8192);
 	for (;;) {
-		ssize_t cnt;
+		ssize_t want = sb->alloc - sb->len - 1;
+		ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
 
-		cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
-		if (cnt < 0) {
+		if (got < 0) {
 			if (oldalloc == 0)
 				strbuf_release(sb);
 			else
 				strbuf_setlen(sb, oldlen);
 			return -1;
 		}
-		if (!cnt)
+		sb->len += got;
+		if (got < want)
 			break;
-		sb->len += cnt;
 		strbuf_grow(sb, 8192);
 	}
 
-- 
2.4.1.4.gfc728c2

             reply	other threads:[~2015-05-31 18:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-31 18:16 Jim Hill [this message]
2015-06-01 10:59 ` [PATCH] strbuf_read: skip unnecessary strbuf_grow at eof Jeff King
2015-06-01 16:23   ` 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=1433096205-14516-1-git-send-email-gjthill@gmail.com \
    --to=gjthill@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).