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

[PATCH v2] trailer: change strbuf in-place in unfold_value()

From
René Scharfe <l.s.r@web.de>
Date
May 15, 2026, 07:33 UTC
Message-ID
<816be07e-2cd6-48fe-ae93-57fa0f2543ed@web.de>
In-Reply-To
<9629b0c1-b28f-4cd2-8d59-67d909ca9052@web.de>

Avoid an allocation by doing s/\n\s*/ /g (replacing NL and any following whitespace with a SP) right in the strbuf instead of copying the result to a temporary one and swapping them in the end. We can safely do that because the replacement is never longer than the original string.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
Formatted with --function-context for easier review.
Changes since v1:
- Removed always-true comparison.
 trailer.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/trailer.c b/trailer.c
index 470f86a4a2..6d8ec7fa8d 100644
--- a/trailer.c
+++ b/trailer.c
@@ -988,29 +988,24 @@ static int ends_with_blank_line(const char *buf, size_t len)
 
 static void unfold_value(struct strbuf *val)
 {
-	struct strbuf out = STRBUF_INIT;
 	size_t i;
+	size_t pos = 0;
 
-	strbuf_grow(&out, val->len);
 	i = 0;
 	while (i < val->len) {
 		char c = val->buf[i++];
 		if (c == '\n') {
 			/* Collapse continuation down to a single space. */
 			while (i < val->len && isspace(val->buf[i]))
 				i++;
-			strbuf_addch(&out, ' ');
-		} else {
-			strbuf_addch(&out, c);
+			c = ' ';
 		}
+		val->buf[pos++] = c;
 	}
+	strbuf_setlen(val, pos);
 
 	/* Empty lines may have left us with whitespace cruft at the edges */
-	strbuf_trim(&out);
-
-	/* output goes back to val as if we modified it in-place */
-	strbuf_swap(&out, val);
-	strbuf_release(&out);
+	strbuf_trim(val);
 }
 
 static struct trailer_block *trailer_block_new(void)

Interdiff against v1:
  diff --git a/trailer.c b/trailer.c
  index b89fa12fe7..6d8ec7fa8d 100644
  --- a/trailer.c
  +++ b/trailer.c
  @@ -989,22 +989,21 @@ static int ends_with_blank_line(const char *buf, size_t len)
   static void unfold_value(struct strbuf *val)
   {
   	size_t i;
   	size_t pos = 0;
   
   	i = 0;
   	while (i < val->len) {
   		char c = val->buf[i++];
   		if (c == '\n') {
   			/* Collapse continuation down to a single space. */
   			while (i < val->len && isspace(val->buf[i]))
   				i++;
  -			val->buf[pos++] = ' ';
  -		} else if (pos != i) {
  -			val->buf[pos++] = c;
  +			c = ' ';
   		}
  +		val->buf[pos++] = c;
   	}
   	strbuf_setlen(val, pos);
   
   	/* Empty lines may have left us with whitespace cruft at the edges */
   	strbuf_trim(val);
   }
-- 
2.54.0
Previous: Jeff King
Message 6 of 6 in “trailer: change strbuf in-place in unfold_value()”
  1. trailer: change strbuf in-place in unfold_value()René Scharfe, May 14, 2026
  2. Ramsay JonesMay 14, 2026
  3. Jeff KingMay 15, 2026
  4. René ScharfeMay 15, 2026
  5. Jeff KingMay 15, 2026
  6. trailer: change strbuf in-place in unfold_value()René Scharfe, May 15, 2026

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.