D and a bazillion of small objects
bearophileHUGS at lycos.com
Wed Jun 2 17:18:37 PDT 2010
> Hello everybody.
> What's the best practice or more efficient way to deal, in D, with lots of
> small objects? My problem is this: I have a small control, ListView. This
> control can have hundreds (and theoretically thousands) of items
> (ListViewItem instances, to be precise). And, in turn each one of this
> ListViewItems can own sub-items (ListViewSubItem instances), effectively
> creating an humongous quantity of small objects.
> Should I just go and create all of those items as classes? I have the
> feeling that this not very efficient (a lot of small heap allocations).
> There's also the possibility of make them structs. But then I would have
> the issue of passing value objects in methods and losing whatever change I
> made (because I would be modifying a copy of the original). To solve this
> I could make an internal, Impl structure, and then use reference counting
> to manage the copies and to make effective all the item modification
> across all copies. But then I would be where I started: doing tons of heap
> So, how should I deal with this issue?
First of all run your program and profile it. If it's fast enough for your purposes, or if the slow parts are elsewhere then you can avoid to optimize the allocation of all those objects.
Otherwise, or if you are writing library code it has to be fast regardless. If you can change those objects into structs, then it's "easy", you can pass them in functions by ref, keeping their changes, and you can allocate them in large chunks, using some kind of arena, pool or stack with a freelist, etc. In my dlibs1 I have something like this to allocate chunks of structs in a pool.
If you must use objects, then you can create the same pools, using placement new. Unfortunately placement new is now deprecated in D2 :-) (If you are using D1 it's OK). Andrei has recently shown a TightArray to replace placement new in D2, but I think it's not good enough yet to solve your problem (but I probably Andrei will improve it).
More information about the Digitalmars-d-learn