Bloat with std.(string.)format?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 17 03:33:42 PDT 2015


On Thursday, 17 September 2015 at 09:54:07 UTC, Chris wrote:
> If I have code like this:
>
> auto builder = appender!string;
> builder ~= "Hello, World!";
> builder ~= "I'm here!";
> builder ~= "Now I'm there!";
>
> the object file grows by 10-11 lines with each call to `builder 
> ~=`. If I use this:
>
> builder ~= format("%s", "Hello, World!");
> builder ~= format("%s", "I'm here!");
> builder ~= format("%s", "Now I'm there!");
>
> The object file is more than twice as big and it grows by 20 
> lines with each call to `format`.
>
> If I use
>
> builder ~= format("%s %s %s", "Hello, World!", "I'm here!", 
> "Now I'm there!");
>
> the code bloat is even worse.
>
> There are many situation where a formatting string is 
> preferable to concatenation, however it adds _a lot_ of bloat. 
> Would a custom formatter be preferable to reduce code bloat or 
> should std/format.d be optimized? (Or both?)
>
> dmd 2.067.1
> -release -boundscheck=off -inline -O

Some initial bloat is expected, format is pretty big (although 
twice as big is a lot, unless your original code was quite 
small?). The extra bloat per call is likely due to inlining. I 
would hope that dmd would spot consecutive inlining of the same 
function and merge them, but perhaps it doesn't.

You could certainly make a less feature complete implementation 
of format that is smaller.

Have you tried with ldc or gdc. In particular, have you tried 
using ldc with --gc-sections on linux?


More information about the Digitalmars-d-learn mailing list