On Sun, Aug 25, 2019 at 04:21:54PM +0200, René Scharfe wrote:

> > You could xstrndup(command_buf.buf, command_buf.len), which would avoid
> > a hidden strlen.
> 
> xstrndup() also searches for NUL, albeit with memchr(3).  xmemdupz()
> would copy without checking.
> 
> I suspect the simplicity of xstrdup() outweighs the benefits of the
> alternatives, but didn't do any measurements..

Yep. I actually started to write xmemdupz() originally then decided it
was unnecessarily verbose and a premature optimization.

I wondered after this exchange whether something like:

  char *strbuf_dup(const struct strbuf *sb)
  {
        return xmemdupz(sb->buf, sb->len);
  }

would be a useful general helper. Grepping around it doesn't seem like
there are a lot of candidates.

If we really wanted to micro-optimize, we could have cmd_hist store
strbufs, and then we could reuse the same buffers over and over without
re-allocating. And use strbuf_addbuf(&cmd_hist.buf, &command_buf). :)

-Peff

Reply via email to