Class size and performance
Daniel Lewis
murpsoft at hotmail.com
Sun Jan 20 16:47:38 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;
> }
>
> 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]
Sir, I'm not sure about the GC, but I can say that your program probably doesn't need to create 728,000 objects to parse an input stream.
I'm writing a parser too at the moment; it's a rather experimental, oddly written one though. It [should] perform tokenization and parsing in a single O(1) pass for the ECMAScript language.
Take a look if you're interested;
http://dsource.org/projects/walnut/browser/branches/1.9/source/interpreter.d
Regards,
Dan
More information about the Digitalmars-d-learn
mailing list