On 07/27/2013 01:03 PM, Kinkie wrote:
> On Sat, Jul 27, 2013 at 8:31 PM, Alex Rousskov 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.

>> // 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;
>> }

> I don't understand how this helps with the append() use-case.

append() should call rawSpace(). rawSpace() is optimized.


> The most likely useage pattern would be 2, and that forces a
> potentially unneeded cow()..

rawSpace() does not force that because your optimization should be
placed there.


> IMO the three methods would be:
> 
> void reserveCapacity(size_type minCap) {
>   if (needed())
>     cow();
> }

I assume that cow() already implements the "if needed" part.


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

Yes, plus the size_type overflow check.


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

No, this needs your no-cow optimization instead. That optimization is
currently in SBuf::reserveSpace() but should be in rawSpace().


HTH,

Alex.

Reply via email to