4.6.1 UInstancedStaticMeshComponent->RemoveInstance Performance Issues

I am using RemoveInstance for a UInstancedStaticMeshComponent to remove something real time while my game is being played. But there is a big performance hit when RemoveInstance is called. I need to be able to change the game board real time while playing the game. Is there something else I should be using or is this a bug?

I recorded an example of what is happening.

Note in this video there is another instance added in its place however I confirmed it was the RemoveInstance by removing the other code for testing. On top of that I call GetInstanceTransform and AddInstance in the function and they both don’t see the dip that happens with RemoveInstance.

Just an update on my temporary solution to this. I decided to just move it to another location.

Instances->UpdateInstanceTransform(TileNum, MoveInstanceTo);

I have actually pinpointed where the issue lies I think. Its the deletion and allocation of all that memory instead of shifting the indexes.

https://github.com/EpicGames/UnrealEngine/blob/4.6/Engine/Source/Runtime/Engine/Private/InstancedStaticMesh.cpp#L1912

		// TODO: it may be possible to instead just update the BodyInstanceIndex for all bodies after the removed instance. 
		ClearAllInstanceBodies();
		CreateAllInstanceBodies();

Hi Mojobojo,

The slow part here is not memory allocation but rather destruction and recreation of the PhysX objects (Instance Bodies) and adding them to the scene.

For 4.7 we’ve added HierarchicalInstancedStaticMeshComponent which handles large numbers of instances much more efficiently. As part of that change, RemoveInstance now removes items in place, moving an item from the end of the array into the removed item’s spot. It then recreates the PhysX objects only for a single instance.

I’d recommend trying out a 4.7 preview version and converting your class to use a HierarchicalInstancedStaticMeshComponent to see if that will improve things for you.

Cheers

Do I include it just like the other component? When I include it in my 4.7 project I get compile errors.

http://i.imgur.com/6naysb0.png

You need to add

#include "Components/HierarchicalInstancedStaticMeshComponent.h"

at the top of your file.

I believe all components are going to be in the Components folder.

The main change is that we have reduced what we always automatically include, to reduce compiling time, so lesser-used components and include files need to be manually included.

For the future, are all the components are/going to be in this directory or is it just some?

EDIT: Also I am getting a lot of crashes using this. I cant add an instance in the editor without it crashing on

UE4Editor-Engine.dll!FHierarchicalStaticMeshSceneProxy::FHierarchicalStaticMeshSceneProxy(UHierarchicalInstancedStaticMeshComponent * InComponent, ERHIFeatureLevel::Type InFeatureLevel) Line 599 C++

Sorry for bumping this, but I’m trying to use “HierarchicalInstancedStaticMeshComponent”, but I’m having trouble keeping track of the indices after using “RemoveInstance”

I made a post about the problem I’m having here, but maybe you guys can help me out if you’ve already dealt with it.

How do you keep track of the instance indices after removing an instance? I hope I’m missing something obvious!

EDIT: I’m a little embarrassed to bump this again, I just wanted to say my problem has been resolved

I posted in that thread. It swaps out the removed item with the last item in the array. It’s bit inconvenient but you should be able to keep track of the instance indices by using the same mechanism. That’s what the foliage tool does.