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

[PATCH 1/3] strbuf.[ch]: add STRBUF_HINT_SIZE macro = 8192

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 7, 2021, 10:38 UTC
Message-ID
<patch-1.3-f5a6c4a2720-20210707T103712Z-avarab@gmail.com>
In-Reply-To
<cover-0.3-00000000000-20210707T103712Z-avarab@gmail.com>

In b449f4cfc97 (Rework strbuf API and semantics., 2007-09-06) the first hardcoding of 8192 appeared in strbuf.[ch], then in f1696ee398e (Strbuf API extensions and fixes., 2007-09-10) another one was added, and in b4e04fb66e8 (strbuf: add strbuf_read_once to read without blocking, 2015-12-15) a third.

Let's factor that out into a STRBUF_HINT_SIZE macro, and add a strbuf_hint() helper macro for "hint ? hint : STRBUF_HINT_SIZE".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 strbuf.c |  6 +++---
 strbuf.h | 11 +++++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 4df30b45494..7e9f5fdc4de 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -517,7 +517,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 	size_t oldlen = sb->len;
 	size_t oldalloc = sb->alloc;
 
-	strbuf_grow(sb, hint ? hint : 8192);
+	strbuf_grow(sb, strbuf_hint(hint));
 	for (;;) {
 		ssize_t want = sb->alloc - sb->len - 1;
 		ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
@@ -532,7 +532,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 		sb->len += got;
 		if (got < want)
 			break;
-		strbuf_grow(sb, 8192);
+		strbuf_grow(sb, STRBUF_HINT_SIZE);
 	}
 
 	sb->buf[sb->len] = '\0';
@@ -544,7 +544,7 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
 	size_t oldalloc = sb->alloc;
 	ssize_t cnt;
 
-	strbuf_grow(sb, hint ? hint : 8192);
+	strbuf_grow(sb, strbuf_hint(hint));
 	cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
 	if (cnt > 0)
 		strbuf_setlen(sb, sb->len + cnt);
diff --git a/strbuf.h b/strbuf.h
index 223ee2094af..ca3c47966a0 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -72,6 +72,17 @@ struct strbuf {
 extern char strbuf_slopbuf[];
 #define STRBUF_INIT  { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
 
+/**
+ * Various functions take a `size_t hint` to give a hint about the
+ * file size, to avoid reallocs. This is the default hint size when
+ * `0` is given. 
+ *
+ * The strbuf_hint() convenience macro is used internally in the
+ * API. DO NOT USE any expression with side-effect for 'size'.
+ */
+#define STRBUF_HINT_SIZE 8192
+#define strbuf_hint(size) ((size) ? (size) : STRBUF_HINT_SIZE)
+
 /*
  * Predeclare this here, since cache.h includes this file before it defines the
  * struct.
-- 
2.32.0.636.g43e71d69cff
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 2 of 12 in “strbuf.[ch]: add STRBUF_HINT_SIZE, don't hardcode 8192”
  1. 0/3 strbuf.[ch]: add STRBUF_HINT_SIZE, don't hardcode 8192Ævar Arnfjörð Bjarmason, Jul 7, 2021
  2. 1/3 strbuf.[ch]: add STRBUF_HINT_SIZE macro = 8192Ævar Arnfjörð Bjarmason, Jul 7, 2021
  3. Junio C HamanoJul 7, 2021
  4. 2/3 strbuf.h API users: don't hardcode 8192, use STRBUF_HINT_SIZEÆvar Arnfjörð Bjarmason, Jul 7, 2021
  5. Eric SunshineJul 7, 2021
  6. Junio C HamanoJul 7, 2021
  7. Junio C HamanoJul 7, 2021
  8. Randall S. BeckerJul 7, 2021
  9. Jeff KingJul 12, 2021
  10. 3/3 strbuf.[ch]: make strbuf_fread() take hint, not sizeÆvar Arnfjörð Bjarmason, Jul 7, 2021
  11. Han-Wen NienhuysJul 7, 2021
  12. Junio C HamanoJul 7, 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.