Re: [RFC 16/17] object_array_entry: copy name before storing in name field
- From
Jeff King <peff@peff.net>
- Date
- May 20, 2013, 16:44 UTC
- Message-ID
- <20130520164458.GA27788@sigill.intra.peff.net>
- In-Reply-To
- <519A365E.6020807@alum.mit.edu>
On Mon, May 20, 2013 at 04:42:38PM +0200, Michael Haggerty wrote:
Show 22 quoted lines
> >> * Many callers store the empty string ("") as the name; for example,
> >> most of the entries created during a run of rev-list have "" as
> >> their name. This means that lots of needless copies of "" are being
> >> made. I think that the best solution to this problem would be to
> >> store NULL rather than "" for such entries, but I haven't figured
> >> out all of the places where the name is used.
> >
> > Use strbufs?
> >
> > No allocation (except for the strbuf object itself) is needed for
> > empty strings, and string ownership and be transferred to and from it
> > to prevent extra copies.
>
> That would cost two extra size_t per object_array_entry. I have the
> feeling that this structure is used often enough that the extra overhead
> would be a disadvantage, but I'm not sure.
>
> The obvious alternative would be to teach users to deal with NULL and
> either add another constructor alternative that transfers string
> ownership or *always* transfer string ownership and change the callers
> to call xstrdup() if they don't already own the name string. I think I
> will try that approach first.You could use the same trick that strbuf does: instead of NULL, point to a well-known empty string literal. Readers do not have to care about this optimization at all; only writers need to recognize the well-known pointer value. And since we do not update in place but only eventually free, it really is just that anyone calling free() would do "if (name != well_known_empty_string)".
-Peff