compile-time function evaluation consuming too much memory

Christian Kamm kamm.incasoftware at shift-at-left-and-remove-this.de
Thu Jun 28 08:27:41 PDT 2007


I have been experimenting with using ctfe and static foreach to generate
code from strings. While for small strings all is fine, larger strings make
DMD's memory usage go over the top. It came up when splitting a string into
an array of strings - something I'd think is relatively common. Simple
example that illustrates the problem:

char[] make_empty_string(int n)
{
  char[] result;

  for(int i = 0; i < n; ++i)
    result ~= " ";
  return result;
}

const char[] largestring1 = make_empty_string(10000);
const char[] largestring2 = make_empty_string(10000);
const char[] largestring3 = make_empty_string(10000);
...

Every call to make_empty_string(10000) will consume 50 MB of memory which it
seems will not be released until dmd terminates. That happens because every
intermediate value in 'result' is stored.

My question is if
- this is expected and actually intentional as ctfe merely extends constant
folding or if 
- this is a bug and dmd could just free the old memory on array
appends/assigns/... (would this ever be a problem?) or if
- D needs a compile-time garbage collector. ;)

Cheers,
Christian



More information about the Digitalmars-d-bugs mailing list