Subject: Re: [RFC] cocci: .buf in a strbuf object can never be NULL

From: Jeff King <[email-masked]>
Date: Fri, 20 Mar 2026 01:57:09 -0400

On Thu, Mar 19, 2026 at 10:45:04PM -0700, Junio C Hamano wrote:

> I may have phrased the idea very poorly.  In short, the core of the
> idea is that we do not have to use the original content of the
> strbuf at all.  I.e., "buf" does not have to be anything related to
> sb->buf.

This seems like a non-starter to me, though, as it means getdelim() will
always allocate a fresh buffer, even though we had a buffer it could
have used. I.e., here:

> +	buf = NULL;
> +	alloc = 0;
>  	errno = 0;
> -	r = getdelim(&sb->buf, &sb->alloc, term, fp);
> +	r = getdelim(&buf, &alloc, term, fp);

we will always get a new allocation. And so looping over
strbuf_getline() will incur one allocation per call, rather than using
the same buffer over and over.

I haven't measured to see what the exact cost is, but I know that
looping over a strbuf (with a reset in the loop, or the implied reset
from a getline call) is a common optimization trick that does have a
measurable improvement for some cases.

-Peff