compile-time function evaluation consuming too much memory

Christian Kamm kamm.incasoftware at shift-at-left-and-remove-this.de
Fri Jun 29 13:40:47 PDT 2007


> The memory performance of ctfe can definatly be improved,

Do you think this warrants being entered as a bug?

> Yes I know the above was just a sample code to
> demonstrate the issue but quite frequently that issue can be evaded by
> using another algoritm:

Yes, I use the same for tuples. First I was splitting strings directly into
tuples in order to statically foreach over them. However, storing the
tuples seemed to take a lot more memory than an array of strings, so
nowadays I just have

const char[][] splitstring = split(str, ' ');
foreach(i, unused; MakeIterTuple!(splitstring.length))
{ ... splitstring[i] ... }

where MakeIterTuple is

template MakeIterTuple(uint size)
{
  static if(size == 0)
    alias Tuple!() MakeIterTuple;
  else static if(size % 2 == 0)
    alias Tuple!(MakeIterTuple!(size/2), MakeIterTuple!(size/2))
      MakeIterTuple;
  else
    alias Tuple!(void, MakeIterTuple!((size-1)/2), 
      MakeIterTuple!((size-1)/2)) MakeIterTuple;
}

And that works well. What's currently my greatest memory hog is the split
function above. It's basically implemented like this

char[][] split(char[] str, char by)
{
  char[][] result;
  size_t c = 0;
  int pos;

  while((pos = find(str[c..$], by)) != -1)
  {
    result ~= str[c..c+pos];
    c += pos+1;
  }

  result ~= str[c..$];

  return result;
}

and I can't seem to find a way to make it more memory efficient. I've
thought about finding the positions and lengths of the strings to be taken
and then using a mixin to create one large array literal, but can't get
around building the string somewhere.

Cheers,
Christian


More information about the Digitalmars-d-bugs mailing list