HateDread
Topic Author
Posts: 72
Joined: 06 Feb 2020, 10:27

Tips for performance issues when removing from collections?

23 Dec 2023, 12:50

Here is an image from a profiler of a single object removal taking ~3ms in a Release build.

Image

You can see the perf macro matching that span (though was renamed after the screenshot); when I find the desired item, I remove it from three Observable Collections:
void GameViewModel::RemoveSpaceObject(Entity entity)
{
	for (int i = 0; i < entities_->Count(); ++i)
	{
		EntityViewModel* model = entities_->Get(i);
		if (model->GetEntityID() == entity.GetID())
		{
			RTG_PROFILE_EVENT("GameViewModel Removing Space Object");

			const int indexInPlayerEntities = playerEntities_->IndexOf(model);
			if (indexInPlayerEntities != -1)
			{
				playerEntities_->RemoveAt(indexInPlayerEntities); // Noesis::Ptr<Noesis::ObservableCollection<EntityViewModel>>
			}

			entities_->Remove(model); // Noesis::Ptr<Noesis::ObservableCollection<EntityViewModel>>

			if (const ClientPredictionViewModelComponent* pPredictionViewModelComponent = entity.TryGetComponent<const ClientPredictionViewModelComponent>())
			{
				predictionsViewModel_->GetPredictions()->Remove(pPredictionViewModelComponent->GetConst()); // Noesis::Ptr<Noesis::ObservableCollection<ClientPredictionViewModel>>
			}

			break;
		}
	}
}
Obviously knowing exactly what I have in XAML would be good but there's a lot to go through. Are there some best practices at least? Would help me narrow down on dumb things I'm doing that could cause this - I don't understand how e.g. DependencyObject::OnDestroy could end up taking 1ms. I'm on version 3.0.12 btw.

Side-note: it'd be nice if I could remove a bunch of items from an Observable Collection and only then trigger a dirty/refresh, rather than on each individual change; is that possible?
 
User avatar
sfernandez
Site Admin
Posts: 3008
Joined: 22 Dec 2011, 19:20

Re: Tips for performance issues when removing from collections?

02 Jan 2024, 12:25

Hi, could you please open a ticket about this in our bugtracker?
We've been recently working on some optimizations on that front and I can provide some patched libraries for you to try.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests