Class size and performance

Sean Kelly sean at f4.ca
Sun Jan 20 17:41:29 PST 2008


Unknown W. Brackets wrote:
> For some code I'm writing, I have a parser that creates instances of a
> class.  For a test case I have, it creates 728,050 instances.  That's a
> lot, and takes a lot of time, but this is life.
> 
> I've got a few ideas on optimizing it, but while testing some of them
> out I noticed something interesting.  If I comment out a single, unused
> from commented out code, member... I get double the speed.
> 
> The number and types of members do not matter, just their size.
> 
> I wrote a reduced test case.  To compile it, use either of these two
> commands:
> 
> dmd -version=fast -O -inline -run weird.d
> dmd -version=slow -O -inline -run weird.d
> 
> The same effect happens with gdc.  It also happens with optimization and
> inlining on or off (they make no difference.)
> 
> import std.stdio, std.perf;
> 
> class TestClass
> {
>     ubyte[56] member1;
> 
>     version (slow)
>         ubyte[4] member2;
> }

Without version(slow), TestClass will occupy 64 bytes, 4 bytes for the
vtbl ptr, 4 bytes for the monitor ptr, and 56 bytes for the data.  With
version(slow), the GC will reserve 128 bytes for the same class, because
it will need 68 bytes of storage.


Sean


More information about the Digitalmars-d-learn mailing list