Git development
 help / color / mirror / Atom feed
From: Ramsay Jones <ramsay@ramsayjones.plus.com>
To: "René Scharfe" <l.s.r@web.de>, "Git List" <git@vger.kernel.org>
Cc: Jeff King <peff@peff.net>
Subject: Re: [PATCH] trailer: change strbuf in-place in unfold_value()
Date: Thu, 14 May 2026 22:30:37 +0100	[thread overview]
Message-ID: <a4da346d-3800-40ea-8828-970b15088bf3@ramsayjones.plus.com> (raw)
In-Reply-To: <9629b0c1-b28f-4cd2-8d59-67d909ca9052@web.de>



On 14/05/2026 7:40 pm, René Scharfe wrote:
> 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.
> Inspired by https://lore.kernel.org/git/20260513185408.GA147423@coredump.intra.peff.net/
> 
>  trailer.c | 16 ++++++----------
>  1 file changed, 6 insertions(+), 10 deletions(-)
> 
> diff --git a/trailer.c b/trailer.c
> index 470f86a4a2..b89fa12fe7 100644
> --- a/trailer.c
> +++ b/trailer.c
> @@ -988,29 +988,25 @@ 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);
> +			val->buf[pos++] = ' ';
> +		} else if (pos != i) {

Hmm, isn't 'pos' strictly (always) less than 'i' here? (note the post update
of 'i' when setting 'c' at the head of the loop).

> +			val->buf[pos++] = c;

So, this (non-newline-or-'trailing'-space char) is always copied.

Not that it matters much (depending on how long the first line is, I doubt
the difference is measurable :) ).

[Unless I'm not reading it correctly, of course - in which case, oops!]

ATB,
Ramsay Jones


>  		}
>  	}
> +	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)


  reply	other threads:[~2026-05-14 21:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-14 18:40 [PATCH] trailer: change strbuf in-place in unfold_value() René Scharfe
2026-05-14 21:30 ` Ramsay Jones [this message]
2026-05-15  4:44   ` Jeff King
2026-05-15  6:47   ` René Scharfe
2026-05-15  4:47 ` Jeff King
2026-05-15  7:33 ` [PATCH v2] " René Scharfe

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=a4da346d-3800-40ea-8828-970b15088bf3@ramsayjones.plus.com \
    --to=ramsay@ramsayjones.plus.com \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=peff@peff.net \
    /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