Re: [RFC] Time to talk about StringNG merge again?

From: Kinkie <gkinkie_at_gmail.com>
Date: Sat, 27 Jul 2013 21:03:33 +0200

On Sat, Jul 27, 2013 at 8:31 PM, Alex Rousskov
<rousskov_at_measurement-factory.com> wrote:
> On 07/27/2013 12:00 PM, Kinkie wrote:
>>> > 1a. Reserve total buffer capacity. Ensure exclusive buffer ownership.
>>> >
>>> > 1b. Reserve buffer space. Ensure exclusive buffer ownership.
>>> >
>>> > 2. Reserve N space bytes for the caller to append to. No guarantees
>>> > regarding buffer ownership are provided.
>
>> I like Amos's suggestion more (two methods for the append-friendly
>> optimization, and one for accessing the internals).
>
>
> There should be no need for two methods for append-friendly optimization
> because append only cares about the space size, not total size. This is
> method #2 in the above list.
>
> Methods #1a and #1b ensure exclusive ownership. They do not return a
> pointer. They must not contain that append-friendly optimization!
>
> AFAICT, Amos explanation of what I was getting at is correct. His email
> did not discuss the optimization trick which is one more reason why we
> want rawSpace(), but I think we both suggest the same set of three methods:
>
> // 1a.
> void reserveCapacity(size_type minCap) {
> cow(minCap);
> }
>
> // 1b.
> void reserveSpace(size_type minSpace) {
> // check that the sum below does not exceed size_type
> Must(size() <= size_type's limit - minSpace);
> reserveCapacity(size() + minSpace);
> }
>
> // 2.
> char *rawSpace(size_type minSpace) {
> ... your optimization goes here ...
> return pointer to space;
> }
>
> Needless to say, it is critical to document these correctly and clearly
> so that we do not have to come back to this discussion again.

I don't understand how this helps with the append() use-case.
The most likely useage pattern would be 2, and that forces a
potentially unneeded cow()..

IMO the three methods would be:

void reserveCapacity(size_type minCap) {
  if (needed())
    cow();
}

void reserveSpace(size_type minSpace) {
  reserveCapacity(minSpace+length());
}

char *rawSpace(size_type minSpace) {
  cow(minSpace+length());
  return *freespace;
}

-- 
    /kinkie
Received on Sat Jul 27 2013 - 19:03:42 MDT

This archive was generated by hypermail 2.2.0 : Sun Jul 28 2013 - 12:00:06 MDT