Filesize of D binaries?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sat Jan 6 05:35:13 PST 2007


Mike Simons wrote:
> Someone recently pointed out to me that a simple hello world sample that only
> imports std.stdio and prints a single line to console compiles to a rather
> hefty 250k! The program was 7 lines long! The compiled C equivalent is 7k.
> 
> I also tried compiling one of the samples from the site which was rouhghly 150
> lines and that ended up being 400k.
> 
> I'm using gdc on linux.
> Is there something I'm missing (i.e. is the compiler chucking in stuff I don't
> need?) or is this for real?
> Is it just an issue with immature compilers?
> 
> I know D has to stick RTTI and GC stuffs in my binary, but what else is going
> in there?

writef an friends call a function to handle the formatting. Since 
they're variadic functions, that function needs to be able to handle any 
type. So it contains code for every primitive type, structs, classes, 
etc. The compiler can't (currently) determine which of those are unused 
so it includes all of that code, plus any code they in turn need, and so on.

C's printf() should have the same problem, except C has less types so 
the superfluous code is smaller, and it doesn't include typeinfo.
C++'s iostreams can be written to include only the code that's strictly 
necessary.

Something similar to C++'s iostreams could be written in D, either using 
similar syntax (lots of small function calls on a line, optionally using 
operator overloading) or using writef-like syntax (using variadic 
templates and static ifs).
writef and friends predate variadic templates, so that's why those 
aren't used. I'm not sure if they also predate operator overloading[1], 
but it could be.

I think Mango[2] has an output mechanism that works like iostreams.


[1]: They were both present when I started using D
[2]: http://dsource.org/projects/mango



More information about the Digitalmars-d mailing list