Efficiently filling an ObservableCollection<T>?
Posted: 09 Jul 2021, 19:01
Tonight was my first time using ObservableCollection<T>, and I was surprised to not find equivalents of std::vector's reserve and resize, or inserting ranges of elements (inc. overload to move instead of copy, although less useful for my purposes).
If I have a collection coming from the game/engine layer outside of the UI, e.g. a std::vector<SomeUnit>, and I want to fill out an ObservableCollection<SomeUnitModel> or equivalent, my go-to pattern would be to reserve the collection size up-front to avoid multiple allocations, and then to iterate and add. In serious perf situations, esp. if the insertion required some more expensive conversion step, I would resize up-front and run a parallel for split by index.
Some of that would be a bit much for now, but just curious why those functions aren't available. Is there a way to get down to the memory of the base vector under the collection?
EDIT: On further inspection, it only takes objects by pointer anyway. It's not clear whether it owns the memory or not; I'm guessing not? How is it intended to be used; with some backing memory behind it that actually owns the objects?
If I have a collection coming from the game/engine layer outside of the UI, e.g. a std::vector<SomeUnit>, and I want to fill out an ObservableCollection<SomeUnitModel> or equivalent, my go-to pattern would be to reserve the collection size up-front to avoid multiple allocations, and then to iterate and add. In serious perf situations, esp. if the insertion required some more expensive conversion step, I would resize up-front and run a parallel for split by index.
Some of that would be a bit much for now, but just curious why those functions aren't available. Is there a way to get down to the memory of the base vector under the collection?
EDIT: On further inspection, it only takes objects by pointer anyway. It's not clear whether it owns the memory or not; I'm guessing not? How is it intended to be used; with some backing memory behind it that actually owns the objects?