CesiumGS / cesium-unreal

Bringing the 3D geospatial ecosystem to Unreal Engine
https://cesium.com/platform/cesium-for-unreal/
Apache License 2.0
943 stars 296 forks source link

Use object pooling #1500

Open j9liu opened 2 months ago

j9liu commented 2 months ago

We currently create and destroy numerous CesiumGltfComponents and UMaterialInstanceDynamic objects throughout the lifetime of the program. Unfortunately, this accumulates in a large use of FNames over time (and potentially other performance / memory drawbacks we have not yet discovered). This was partially solved in #1423, but the FName out-of-memory crash can still happen even after this fix, as noted on the forum.

The real solution would be to create and use object pools. Beyond materials, this could be beneficial for components or meshes, too. That way, we can avoid infinitely accumulating FName objects.

spikhoff commented 2 months ago

Excessive creation and destruction of CesiumGltfComponents and UMaterialInstanceDynamic objects can lead to a significant increase in FName objects, impacting memory usage and performance.

Solution: Leveraging Unreal Engine's Built-in Pooling Mechanisms

Unreal Engine provides robust pooling mechanisms that can effectively address this memory issue. By utilizing these features, you can significantly reduce the creation and destruction of objects, improving performance and memory efficiency.

Key Points:

Additional Considerations:

By effectively utilizing Unreal Engine's built-in pooling mechanisms, you can significantly mitigate memory issues and improve the performance of your application.

kring commented 2 months ago

@spikhoff that reads as AI generated (my apologies if I'm mistaken, but references to "your specific application" jump out at me), but it's a reasonable summary nonetheless. Unreal may already have an object pool of some sort, so I'd look for that before implementing something like the ObjectPool class. In any case, the most challenging aspect is likely to be figure out how to "reset" Unreal UObjects to prepare them for reuse. I wouldn't be surprised to run into Unreal bugs or race conditions when trying to do this, since rapid reuse of UObjects at runtime is not a very common use-case as far as I know.

spikhoff commented 2 months ago

Hey @kring and folks,

Just wanted to give a heads-up about the recent post. I've been hacking away at it, and to be honest, I've been using some AI tools to help out here and there.

I know some of you might be raising an eyebrow at that, but trust me, it's not about replacing human ingenuity. It's about being efficient and trying out new stuff. I've been keeping a close eye on the AI-generated stuff and making sure it fits in with what we're doing here.

If you have any questions or think something's off, don't hesitate to shout. This is all about open communication and transparency.

kring commented 1 month ago

Ok, thanks for the heads up @spikhoff. For the development we do ourselves, we currently have a policy against using code-writing AI tools. Not because we're worried about it replacing human ingenuity, but because the ownership (copyright) of the resulting code is still at least a little bit uncertain. We want people to be able to rely on our open source products in a wide variety of circumstances, so we are very cautious about this kind of thing. I'm not currently certain if and how that policy applies to external contributions, given that such contributions are covered by the Contributor License Agreement, but am trying to find out.

spikhoff commented 1 month ago

Hey @kring,

Thanks for bringing up the AI tool policy. It's a good point to consider. Copyright can be a bit of a pain, especially in open source.

Ultimately, the decision on whether to allow AI-generated code in contributions is up to the project community. If there are legal concerns, we need clear rules to keep things running smoothly.