git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] strbuf.c: optimize program logic

From
阿G阿德烈 via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 26, 2021, 05:06 UTC
Message-ID
<pull.846.git.1611637582625.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

the usage in strbuf.h tell us"Alloc is somehow a "private" member that should not be messed with. use `strbuf_avail()`instead."

I notice that `strbuf_read` and `strbuf_read_once` may use "sb->alloc - sb->len - 1" instead of using `strbuf_avail()`,so I change it,in the same time, I change `sb->len+= got` to `strbuf_setlen()`,it may better to use existing api.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
    strbuf.c: optimize program logic
    
    use strbuf_avail() is better than use Exposed .len and .alloc use
    strbuf_setlen() is better than use Exposed .len,so i change them in
    "strbuf.c".
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-846%2Fadlternative%2Fstrbuf_read_optimization-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-846/adlternative/strbuf_read_optimization-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/846
 strbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index e3397cc4c72..76f560a28d0 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -517,7 +517,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 
 	strbuf_grow(sb, hint ? hint : 8192);
 	for (;;) {
-		ssize_t want = sb->alloc - sb->len - 1;
+		ssize_t want = strbuf_avail(sb);
 		ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
 
 		if (got < 0) {
@@ -527,7 +527,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 				strbuf_setlen(sb, oldlen);
 			return -1;
 		}
-		sb->len += got;
+		strbuf_setlen(sb, sb->len + got);
 		if (got < want)
 			break;
 		strbuf_grow(sb, 8192);
@@ -543,7 +543,7 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
 	ssize_t cnt;
 
 	strbuf_grow(sb, hint ? hint : 8192);
-	cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
+	cnt = xread(fd, sb->buf + sb->len, strbuf_avail(sb));
 	if (cnt > 0)
 		strbuf_setlen(sb, sb->len + cnt);
 	else if (oldalloc == 0)

base-commit: 6d3ef5b467eccd2769f1aa1c555d317d3c8dc707
-- 
gitgitgadget
Next: Junio C Hamano
Message 1 of 8 in “strbuf.c: optimize program logic”
  1. strbuf.c: optimize program logic阿德烈 via GitGitGadget, Jan 26, 2021
  2. Junio C HamanoJan 26, 2021
  3. 胡哲宁Jan 26, 2021
  4. Junio C HamanoJan 26, 2021
  5. Jeff KingJan 26, 2021
  6. Junio C HamanoJan 26, 2021
  7. 胡哲宁Jan 29, 2021
  8. Jeff KingJan 30, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.