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

[PATCH] Remove preemptive allocations.

From
Pierre Habouzit <madcoder@debian.org>
Date
Sep 16, 2007, 08:19 UTC
Message-ID
<20070916172233.A60E42D43A4@madism.org>
In-Reply-To
<20070916172134.GA26457@artemis.corp>

Careful profiling shows that we spend more time guessing what pattern allocation will have, whereas we can delay it only at the point where add_rfc2047 will be used and don't allocate huge memory area for the many cases where it's not.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 commit.c |   35 +++++------------------------------
 1 files changed, 5 insertions(+), 30 deletions(-)
diff --git a/commit.c b/commit.c
index 13af933..85889f9 100644
--- a/commit.c
+++ b/commit.c
@@ -501,6 +501,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
 	return;
 
 needquote:
+	strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
 	strbuf_addf(sb, "=?%s?q?", encoding);
 	for (i = last = 0; i < len; i++) {
 		unsigned ch = line[i] & 0xFF;
@@ -520,14 +521,6 @@ needquote:
 	strbuf_addstr(sb, "?=");
 }
 
-static unsigned long bound_rfc2047(unsigned long len, const char *encoding)
-{
-	/* upper bound of q encoded string of length 'len' */
-	unsigned long elen = strlen(encoding);
-
-	return len * 3 + elen + 100;
-}
-
 static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
 			 const char *line, enum date_mode dmode,
 			 const char *encoding)
@@ -560,8 +553,7 @@ static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb
 		add_rfc2047(sb, line, display_name_length, encoding);
 		strbuf_add(sb, name_tail, namelen - display_name_length);
 		strbuf_addch(sb, '\n');
-	}
-	else {
+	} else {
 		strbuf_addf(sb, "%s: %.*s%.*s\n", what,
 			      (fmt == CMIT_FMT_FULLER) ? 4 : 0,
 			      filler, namelen, line);
@@ -955,19 +947,12 @@ static void pp_header(enum cmit_fmt fmt,
 		 * FULLER shows both authors and dates.
 		 */
 		if (!memcmp(line, "author ", 7)) {
-			unsigned long len = linelen;
-			if (fmt == CMIT_FMT_EMAIL)
-				len = bound_rfc2047(linelen, encoding);
-			strbuf_grow(sb, len + 80);
+			strbuf_grow(sb, linelen + 80);
 			add_user_info("Author", fmt, sb, line + 7, dmode, encoding);
 		}
-
 		if (!memcmp(line, "committer ", 10) &&
 		    (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
-			unsigned long len = linelen;
-			if (fmt == CMIT_FMT_EMAIL)
-				len = bound_rfc2047(linelen, encoding);
-			strbuf_grow(sb, len + 80);
+			strbuf_grow(sb, linelen + 80);
 			add_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
 		}
 	}
@@ -982,7 +967,6 @@ static void pp_title_line(enum cmit_fmt fmt,
 			  int plain_non_ascii)
 {
 	struct strbuf title;
-	unsigned long len;
 
 	strbuf_init(&title, 80);
 
@@ -1004,16 +988,7 @@ static void pp_title_line(enum cmit_fmt fmt,
 		strbuf_add(&title, line, linelen);
 	}
 
-	/* Enough slop for the MIME header and rfc2047 */
-	len = bound_rfc2047(title.len, encoding) + 1000;
-	if (subject)
-		len += strlen(subject);
-	if (after_subject)
-		len += strlen(after_subject);
-	if (encoding)
-		len += strlen(encoding);
-
-	strbuf_grow(sb, title.len + len);
+	strbuf_grow(sb, title.len + 1024);
 	if (subject) {
 		strbuf_addstr(sb, subject);
 		add_rfc2047(sb, title.buf, title.len, encoding);
-- 
1.5.3.1
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 16 of 21 in “[RFC] strbuf's in builtin-apply”
  1. Pierre HabouzitSep 15, 2007
  2. New strbuf APIs: splice and embed.Pierre Habouzit, Sep 15, 2007
  3. Junio C HamanoSep 16, 2007
  4. Pierre HabouzitSep 16, 2007
  5. builtin-apply: use strbuf's instead of buffer_desc's.Pierre Habouzit, Sep 15, 2007
  6. Junio C HamanoSep 16, 2007
  7. Pierre HabouzitSep 16, 2007
  8. Pierre HabouzitSep 15, 2007
  9. Pierre HabouzitSep 16, 2007
  10. New strbuf APIs: splice and attach.Pierre Habouzit, Sep 15, 2007
  11. Florian WeimerSep 16, 2007
  12. Pierre HabouzitSep 16, 2007
  13. Florian WeimerSep 17, 2007
  14. Now that cache.h needs strbuf.h, remove useless includes.Pierre Habouzit, Sep 15, 2007
  15. Refactor replace_encoding_header.Pierre Habouzit, Sep 15, 2007
  16. Remove preemptive allocations.Pierre Habouzit, Sep 16, 2007
  17. Rewrite convert_to_{git,working_tree} to use strbuf's.Pierre Habouzit, Sep 16, 2007
  18. Linus TorvaldsSep 16, 2007
  19. builtin-apply: use strbuf's instead of buffer_desc's.Pierre Habouzit, Sep 16, 2007
  20. Pierre HabouzitSep 16, 2007
  21. Junio C HamanoSep 16, 2007

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.