Git development
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Ramsay Jones <ramsay@ramsayjones.plus.com>
Cc: "René Scharfe" <l.s.r@web.de>, "Git List" <git@vger.kernel.org>
Subject: Re: [PATCH] trailer: change strbuf in-place in unfold_value()
Date: Fri, 15 May 2026 00:44:47 -0400	[thread overview]
Message-ID: <20260515044447.GC83595@coredump.intra.peff.net> (raw)
In-Reply-To: <a4da346d-3800-40ea-8828-970b15088bf3@ramsayjones.plus.com>

On Thu, May 14, 2026 at 10:30:37PM +0100, Ramsay Jones wrote:

> >  	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!]

Yeah, I think you're right. If it were a for-loop which incremented "i"
at the end then the comparison could make sense. But even then, I think
usually in such modify-in-place loops we don't bother trying to skip
self-assignment (e.g., see remove_space() in builtin/patch-id.c). In
practice I don't know which is worse: the extra branch or a pointless
memory store.

-Peff

  reply	other threads:[~2026-05-15  4:44 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
2026-05-15  4:44   ` Jeff King [this message]
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=20260515044447.GC83595@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=ramsay@ramsayjones.plus.com \
    /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