What are best practices around toString?

christian.koestlin christian.koestlin at gmail.com
Thu Oct 6 08:51:14 UTC 2022


On Saturday, 1 October 2022 at 17:50:54 UTC, tsbockman wrote:
> On Saturday, 1 October 2022 at 10:02:34 UTC, Salih Dincer wrote:
>> On Saturday, 1 October 2022 at 08:26:43 UTC, tsbockman wrote:
>>> `StringBuilder` is a utility shared across the entire project:
>>
>> Appender not good enough; at least in terms of allocating 
>> memory and accumulating a string?
>
> `Appender` is a legitimate option, but unless it is provided 
> with a good estimate of the final length at the beginning, it 
> will allocate several times for a longer string, and the final 
> buffer will be, on average, 50% larger than needed.
>
> Neither of these things is a major problem, but `StringBuilder` 
> is only a few lines of code to perfectly minimize allocation, 
> so why not?

Thanks a lot. One needs to go twice through the serialization, 
but perhaps thats better than reallocing memory.

Kind regards,
Christian


More information about the Digitalmars-d-learn mailing list