Class size and performance
Unknown W. Brackets
unknown at simplemachines.org
Sun Jan 20 15:54:24 PST 2008
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;
}
void main()
{
// This is just how many my real example used.
const test_instances = 728_050;
// Let's store them for accuracy to the problem.
TestClass[] example = null;
example.length = test_instances;
auto counter = new PerformanceCounter();
counter.start();
for (int i = 0; i < test_instances; i++)
example[i] = new TestClass();
counter.stop();
PerformanceCounter.interval_type ms = counter.microseconds();
writefln("%f seconds", cast(double) ms / 1_000_000.0);
}
Also of note, if I put a custom allocator on to determine the size, it's
slow until I remove another 4 bytes of members.
Is there some sort of threshold in the gc? Why is this happening?
Thanks,
-[Unknown]
More information about the Digitalmars-d-learn
mailing list