Interesting Memory Optimization

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 19 04:44:00 PDT 2012


On Thu, 15 Mar 2012 22:16:18 -0400, Kevin <kevincox.ca at gmail.com> 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).

I have done this manually in the past.  In an application that ran on a  
8-bit micro with 256 bytes of RAM and 4K code space, I ran out of space  
and was able to save quite a bit by making one large string array with all  
the data, and then use pointer/length combinations out of that string  
array.

It seems like the compiler could do some work, but what about CTFE?  I  
think this would be a cool project.  But I'm not sure if CTFE can save  
state for later...

string poolString(string s)
{
    // look for s in existing pool, if found, return
    // otherwise, add to pool.
}

-Steve


More information about the Digitalmars-d mailing list