Use case: eliminate hidden allocations in buildPath

inout inout at gmail.com
Thu Dec 5 09:30:36 PST 2013


On Wednesday, 4 December 2013 at 23:14:48 UTC, Andrei 
Alexandrescu wrote:
> Hello,
>
>
> Walter and I were talking about eliminating the surreptitious 
> allocations in buildPath:
>
> http://dlang.org/phobos/std_path.html#.buildPath
>
> We'd need to keep the existing version working, so we're 
> looking at adding one or more new overloads. We're looking at 
> giving the user the option to control any needed memory 
> allocation (or even arrange things such that there's no memory 
> allocated at all).
>
> It's a generous design space, so although we have a couple of 
> ideas let's hear others first.
>
>
> Thanks,
>
> Andrei

My approach in cases like that used to be to pass an optional 
char[] buffer = null as last argument (it doesn't work as nice in 
variadic functions though):

// constructs the result in a buffer if one is passed until there 
is no more space left, in which case it reallocates the buffer
char[] buildPath(const(char)[] path1, const(char)[] path2, char[] 
buffer = null) {
   Appender!(char) appender = useBuffer(buffer);
   ...
}


More information about the Digitalmars-d mailing list