Thread overview
Appender at CTFE?
Aug 21, 2015
Nick Sabalausky
Aug 21, 2015
cym13
Aug 21, 2015
Nick Sabalausky
Aug 22, 2015
BBasile
August 21, 2015
Not at a pc, so can't test right now, but does Appender work at compile time? If not, does ~= still blow up CTFE memory usage like it used to? Any other best practice / trick for building strings in CTFE?
August 21, 2015
On Friday, 21 August 2015 at 22:39:29 UTC, Nick Sabalausky wrote:
> Not at a pc, so can't test right now, but does Appender work at compile time? If not, does ~= still blow up CTFE memory usage like it used to? Any other best practice / trick for building strings in CTFE?

I did two experiments:

    import std.conv;
    import std.stdio;
    import std.array;
    import std.range;
    import std.algorithm;

    string f(T)(T strings) {
        auto a = appender("");
        foreach (s ; strings)
            a.put(s);
        return a.data;
    }

    string g(T)(T strings) {
        auto a = "";
        foreach (s ; strings)
            a ~= s;
        return a;
    }

    void main(string[] args) {
        enum a = iota(10000).map!(to!string).f;
      //enum a = iota(10000).map!(to!string).g;
        a.writeln;
    }

Each make use of CTFE but the f (appender) variant blew my RAM (old computer)
while the g (string) variant was nothing comparable in term of memory
consumption and almost instantaneous. It looks like string concatenation is
the best way to go, but this example is rather limited so theoretical
confirmation would be great.

August 21, 2015
Thanks!

I wouldn't have expected that about the 	memory. But I wonder how much of that memory usage with Appender was just all the template instantiations. I'll have to look into that when I get back.
August 22, 2015
On Friday, 21 August 2015 at 23:51:16 UTC, cym13 wrote:
> On Friday, 21 August 2015 at 22:39:29 UTC, Nick Sabalausky wrote:
>> Not at a pc, so can't test right now, but does Appender work at compile time? If not, does ~= still blow up CTFE memory usage like it used to? Any other best practice / trick for building strings in CTFE?
>
> I did two experiments:
> 
> [...]
>
> Each make use of CTFE but the f (appender) variant blew my RAM (old computer)

Excepted any error from my part, shouldn't you call '.reserve' in order to make the appender efficient ?