[.net] Performance of Inheriting From CollectionBase

Started by
0 comments, last by RipTorn 18 years, 3 months ago
In my code I've been making extensive use of collections, because they are super handy. I've got more than a few objects that inherit from CollectionBase. However as my skills (and my code base) have grown I've started to wonder about the performance of collections. Things are starting to slow down. To help me understand why I've been be using the CLR profiler to get a look at what’s getting called most. From what I can interpret from the results is that my code is spending a significant amount of time in the collections code. Now I recall back in VB6 collections really did suck. To squeeze the most out of things it helped a lot to use arrays of objects and manage those arrays yourself. Since it has been 8 years since VB6, I would have thought that compilers would have gotten a lot smarter concerning collections. Is it really the case they slow things down so much or is it more because of what I do? My collections typically do a lot of typecasting because lets face it an 'object' is only so useful. Should I just ditch the collections and manage arrays or should I do something even more advanced? Any thoughts or advice?
Advertisement
are you using .net 2 generic collections? If not you should consider moving up as this will help a lot with the type casting you mention.

One thing to look for in the CLR profiler is memory usage. If the memory graph looks like a mountain range (up a lot, down a lot, repeat) then it's a good indication that you are having issues with the garbage collector.

I recently had a similar problem to deal with. Over the course of a frame I was collecting all objects I wanted to draw, putting them in various List<>'s, and eventually they ended up in a List<> of structures that also held a bunch of information about how they were to be rendererd. Unfortunatly, because I was doing a lot of multi-threaded work here, the lists were all becomming much harder for the GC to collect efficiently, secondly I was using a new list every frame (so I could start collecting one frame while the previous was still rendering). The end result was that a list of up to 10,000 items (complex scene) would be generated each frame, and wouldn't be collected until upto 10+ frames later.
With a bunch of work optimizing this, not creating new lists, being careful how I shared them, etc, I was able to boost performance by well over 10x in my 10k object 'stress test', going from 3fps to approximatly 50fps. I was pretty amazed by this. Never had this sort of gain before...

basically trying to say while collections are really great, you still need to take care using them. Or just get it working first, then optimize to hell later I guess :-)

This topic is closed to new replies.

Advertisement