From: Kristian Høgsberg Date: Tue, 31 Jul 2007 15:33:52 GMT Subject: Re: [PATCH 3/5] Add strbuf_printf() to do formatted printing to a strbuf. Message-ID: <1185896032.11086.57.camel@hinata.boston.redhat.com> In-Reply-To: On Tue, 2007-07-31 at 15:55 +0100, Johannes Schindelin wrote: > Hi, > > On Tue, 31 Jul 2007, Kristian H?gsberg wrote: > > > On Mon, 2007-07-30 at 21:36 -0700, Junio C Hamano wrote: > > > Kristian Høgsberg writes: > > > > > > > +static void inline strbuf_grow(struct strbuf *sb, size_t extra) > > > > +{ > > > > + while (sb->alloc < sb->len + extra) > > > > sb->alloc = sb->alloc * 3 / 2 + 16; > > > > + sb->buf = xrealloc(sb->buf, sb->alloc); > > > > +} > > > > > > Somehow this while () loop to compute the growth factor bothers > > > me but that is probably a minor detail. > > > > Think of it as a more efficient way of adding one character at a time :) > > And it's logarithmic in the number of extra bytes. By the way, I > > normally just double the size in these cases, which gives you amortized > > linear performance for adding to the buffer. What's behind the * 3 / 2 > > idea? > > But why not > > sb->alloc = alloc_nr(sb->len + extra); That should work, or even ALLOC_GROW, I guess. Didn't know about those macros. But still, why not just double the size? Kristian