Why is sformat and formattedWrite (appender) allocating GC mem here?

ag0aep6g anonymous at example.com
Sat Aug 31 21:12:32 UTC 2019


On 31.08.19 14:07, cc wrote:
> I'm guessing it's allocating a string first to write the contents 
> of the array and then inserting that string into the buffer I supplied.  
> Is there no way to have it skip this step and just write each element 
> (plus the joining punctuation, etc) directly into the buffer?

`formatElement` does something like that. It writes the string into a 
temporary buffer while looking for invalid Unicode. When it finds some, 
the temporary is discarded and the whole string is formatted 
differently. When the string is a-ok, the data is copied over to the 
actual destination.

I'm not sure if that's the best approach, though. The temporary buffer 
and the string copy are costly.

There is also a closure being allocated for no reason in `sformat` 
itself. The compiler isn't smart enough to see that it's not really needed.

I've made a pull request to get rid of those allocations:
https://github.com/dlang/phobos/pull/7163


More information about the Digitalmars-d-learn mailing list