Use case: eliminate hidden allocations in buildPath

Michel Fortin michel.fortin at michelf.ca
Wed Dec 4 16:13:00 PST 2013


On 2013-12-04 23:14:48 +0000, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> 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.

Allow an allocator as the first argument. Then pass an allocator that 
uses preallocated memory (or any other strategy that does not really 
need to allocate). While technically the allocator still "allocates" 
memory, since you control the allocator it does it the you can redefine 
"allocate" to not allocate.

Here's a funny thought: allow plain arrays to be *typed* allocators 
through UFCS, just like arrays are ranges. If you have an array of 
chars, then "allocating" from it will simply return a slice and "bump 
the pointer" by becoming the remaining unused slice. The big problem 
with buildPath is that it won't work with overloading because your 
allocator has the same type as the other parameters. You'll need to 
wrap it in some kind of allocator shell. :-/

-- 
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca



More information about the Digitalmars-d mailing list