Small Buffer Optimization for string and friends

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Apr 8 11:30:49 PDT 2012


On 4/8/12 1:26 PM, Andrei Alexandrescu wrote:
> On 4/8/12 1:16 PM, Michel Fortin wrote:
>> On 2012-04-08 17:14:37 +0000, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> said:
>>> As I mentioned, the first call to .ptr changes representation, thus
>>> making the allocation that the optimization had saved. Things are not
>>> worse off than before.
>>
>> This only works if the code has write access to that variable.
>>
>> Also, if the variable is a temporary copy such as a by-value function
>> parameter, only the representation of that temporary copy will be
>> affected. Every time you call a function which calls .ptr a new copy
>> will be allocated on the heap. If such a function call is part of a
>> loop, it'll degrade performance noticeably.
>
> I think there's a bit of a confusion. Could you please clarify with an
> example?

I think I understand:

string s;
foreach (lots)
   fun(s);

void fun(string s)
{
     ... s.ptr ...
}

I agree in this case there would be one allocation per call.


Andrei


More information about the Digitalmars-d mailing list