Interesting Memory Optimization

Peter Alexander peter.alexander.au at gmail.com
Fri Mar 16 04:32:36 PDT 2012


On Friday, 16 March 2012 at 02:31:47 UTC, Xinok wrote:
> On Friday, 16 March 2012 at 02:18:27 UTC, Kevin wrote:
>> This is in no way D specific but say you have two constant 
>> strings.
>>
>> const char[] a = "1234567890";
>> // and
>> const char[] b = "67890";
>>
>> You could lay out the memory inside of one another. IE: if 
>> a.ptr = 1 then b.ptr = 6.  I'm not sure if this has been done 
>> and I don't think it would apply very often but it would be 
>> kinda cool.
>>
>> I thought of this because I wanted to pre-generate 
>> hex-representations of some numbers I realized I could use 
>> half the memory if I nested them. (At least I think it would 
>> be half).
>>
>> Kevin.
>
> I'm pretty sure this is called string pooling.

My understanding is that string pooling just shares whole strings 
rather than combining suffixes.

e.g.
const char[] a = "fubar";
const char[] b = "fubar"; // shared
const char[] c = "bar"; // not shared at all

Combining suffixes is obviously possible, but I'm not sure that 
string pooling implies suffix pooling.


More information about the Digitalmars-d mailing list