View mode: basic / threaded / horizontal-split · Log in · Help
March 16, 2012
Interesting Memory Optimization
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.
March 16, 2012
Re: Interesting Memory Optimization
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.
March 16, 2012
Re: Interesting Memory Optimization
On 16-03-2012 03:31, 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.

Right. Most compilers do it.

-- 
- Alex
March 16, 2012
Re: Interesting Memory Optimization
On 03/15/2012 10:35 PM, Alex Rønne Petersen wrote:
> On 16-03-2012 03:31, Xinok wrote:
>> I'm pretty sure this is called string pooling.
>
> Right. Most compilers do it.
>
Cool.  You learn something every day.
March 16, 2012
Re: Interesting Memory Optimization
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.
March 16, 2012
Re: Interesting Memory Optimization
On 16-03-2012 12:32, Peter Alexander wrote:
> 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.

I don't see any reason why c couldn't point to element number 3 of b, 
and have its length set to 3...

-- 
- Alex
March 16, 2012
Re: Interesting Memory Optimization
On Mar 16, 2012 7:45 AM, "Alex Rønne Petersen" <xtzgzorex@gmail.com> wrote
>
> I don't see any reason why c couldn't point to element number 3 of b, and
have its length set to 3...
>
> --
> - Alex

And the previous examples were language agnostic.  In D and other languages
where the length of a string is stored we can nest strings anywhere inside
other strings.

const char[] a = "foofoo";
const char[] b = "oof";

Those can't be nested in null terminated strings, bit they can where
strings have an explicit length.
March 16, 2012
Re: Interesting Memory Optimization
On Fri, Mar 16, 2012 at 08:24:34AM -0400, Kevin Cox wrote:
[...]
> And the previous examples were language agnostic.  In D and other
> languages where the length of a string is stored we can nest strings
> anywhere inside other strings.
> 
> const char[] a = "foofoo";
> const char[] b = "oof";
> 
> Those can't be nested in null terminated strings, bit they can where
> strings have an explicit length.

More to the point, does dmd perform this optimization currently?


T

-- 
Mediocrity has been pushed to extremes.
March 16, 2012
Re: Interesting Memory Optimization
On Friday, 16 March 2012 at 12:24:45 UTC, Kevin Cox wrote:
> On Mar 16, 2012 7:45 AM, "Alex Rønne Petersen" 
> <xtzgzorex@gmail.com> wrote
>>
>> I don't see any reason why c couldn't point to element number 
>> 3 of b, and
> have its length set to 3...
>>
>> --
>> - Alex
>
> And the previous examples were language agnostic.  In D and 
> other languages
> where the length of a string is stored we can nest strings 
> anywhere inside
> other strings.
>
> const char[] a = "foofoo";
> const char[] b = "oof";
>
> Those can't be nested in null terminated strings, bit they can 
> where
> strings have an explicit length.

All compile-time generated strings (like literals) in D are 
(guaranteed to be?) null-terminated as well.
March 16, 2012
Re: Interesting Memory Optimization
On 03/16/2012 03:28 PM, H. S. Teoh wrote:
> On Fri, Mar 16, 2012 at 08:24:34AM -0400, Kevin Cox wrote:
> [...]
>> And the previous examples were language agnostic.  In D and other
>> languages where the length of a string is stored we can nest strings
>> anywhere inside other strings.
>>
>> const char[] a = "foofoo";
>> const char[] b = "oof";
>>
>> Those can't be nested in null terminated strings, bit they can where
>> strings have an explicit length.
>
> More to the point, does dmd perform this optimization currently?
>
>
> T
>

No.

immutable string a = "123";
immutable string b = a;

void main(){writeln(a.ptr is b.ptr);} // "false"
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home