HISM BatchUpdateInstancesTransforms not actually batching?

Looking at the Unreal Engine 5 source code for UHierarchicalInstancedStaticMeshComponent, am I correct that the batching instance methods don’t actually ‘batch’ the transform updates?

When I read the code I see the batch methods just call UpdateInstanceTransform(), which then ultimately always marks bIsOutOfDate and invokes BuildTreeIfOutdated(/Async/true, /ForceUpdate/false) on every update.

Am I missing something?

To me it seems that rebuilding the tree only after applying all the transforms would be beneficial. And the underlying UInstancedStaticMeshComponent batch updates should be faster right?

Thanks for your help!

Yes, the source code is very straightforward here: There is no benefit to calling BatchUpdateInstancesTransforms over your own custom loop of UpdateInstanceTransform, for HISMs specifically.

On the other hand, there is no risk either. Unless you manage to implement your own, superior implementation, you should use the engine provided one in case this section is eventually reworked & optimized.

Now that I take a closer look, if you wanted to optimize the HISM batch update, it seems there is a pretty easy improvement: just copy the entire code of UpdateInstanceTransform, and move the call to BuildTreeIfOutdated to outside the loop.

Because as it stands right now, we start building asynchronously immediately, then invalidate said build right after the first loop… And for every instance we do a bunch of checks related to if we can build or not, this could really add-up when talking about thousands of transforms.

Interestingly, AddInstances is much more optimized. And specifically does the BuildTree thing outside the loop. It’s probably much more common to batch add than it is to batch update.

Thanks @Altrue ,

Yes indeed it would be easy to update as discussed for more efficient rebuilding… Will do some tests and then try to publish a pull request sometime.

1 Like