www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Appender at CTFE?

reply "Nick Sabalausky" <a a.a> writes:
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?
Aug 21 2015
parent reply "cym13" <cpicard openmailbox.org> writes:
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.
Aug 21 2015
next sibling parent "Nick Sabalausky" <a a.a> writes:
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.
Aug 21 2015
prev sibling parent "BBasile" <bb.temp gmx.com> writes:
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 ?
Aug 22 2015