Small Buffer Optimization for string and friends

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Apr 8 07:53:16 PDT 2012


On 4/8/12 4:55 AM, Vladimir Panteleev wrote:
> On Sunday, 8 April 2012 at 09:46:28 UTC, Vladimir Panteleev wrote:
>> On Sunday, 8 April 2012 at 05:56:36 UTC, Andrei Alexandrescu wrote:
>>> Walter and I discussed today about using the small string
>>> optimization in string and other arrays of immutable small objects.
>>>
>>> On 64 bit machines, string occupies 16 bytes. We could use the first
>>> byte as discriminator, which means that all strings under 16 chars
>>> need no memory allocation at all.
>>
>> Don't use the first byte. Use the last byte.
>>
>> The last byte is the highest-order byte of the length. Limiting arrays
>> to 18.37 exabytes, as opposed to 18.45 exabytes, is a much nicer
>> limitation than making assumptions about the memory layout.
>
> Erm... never mind, I thought the pointer was the first field.

Me too, actually.

> Even so, how would you use the lowest-order byte of the length as a
> discriminator? Unless you allocated bytes 2-8 for the length (which
> would be an unaligned read, or a shift every time...)

It's amazing how fast shift works :o).

> "making assumptions about the memory layout" now seems like the only
> solution.
>
> Also, what will be .ptr for such arrays? It can't point inside the
> string, because the type is immutable and the array is on the stack.
> Duplication can be expensive as well.

Once anyone asks for .ptr a conservative copy will be made.


Andrei


More information about the Digitalmars-d mailing list