Would be usefull to cache meshes?

The question is vague so I’ll try to explain what is the case. I have a custom model format which is parsed at runtime and constructed in the scene, it may happen that in the scene the same mesh need to be created. I was thinking that maybe parsing the same model data from external file each time could be expensive so maybe would be good to cache what can be reusable.

The fact is that if I cache the mesh I would still hold arrays of vertices, uvs and tris as “duplicate” as I have anyway to reconstruct the meshes assigning new arrays, so how much would be usefull to cache mesh data, should I bother with it at all?

Note that is not about pooling objects with a mesh on them, but only caching mesh data (vertices, triangles, uvs, normals) in a custom class that work as a container.

Maybe using a picture would be clearer. Let’s suppose I have to build 3 meshes that comes from the same type of data:

By not caching the mesh I’m parsing the mesh file 3 times, by caching it I parse it once, but all mesh data remain in the container class, probably I should clear that data when the process is finished, what you think?

Depends on how long it takes to parse your file and build the mesh and also on how many times you have to do it for the same mesh. Nothing stopping you from holding onto the data while you’re building your scene and then disposing of it when you’re done. Doesn’t have to be a “container” - could just be a dictionary or similar lookup.

The parsing is fast, I don’t see any hookups when building the meshes, tested with 100 cubes and is almost immediate.

My plan is this, have a dictionary of <string, MeshContainer>, I have already on the mesh file to parse an entry that says if the mesh should be cached, first thing the static function does is to lookup for the mesh id in the dictionary, if it doesn’t find proceed to parse and create the mesh, if the cache entry is true then proceed to add an entry in the dictionary with the mesh data.

All the meshes after this look up first in the dictionary, if it finds the key proceed to build the mesh from the cached data. So on for the rest of the meshes, when everything is done dispose the cached meshes.

Now, during my tests I didn’t see cons and pros in both methods but I’m not sure on the long run if I should consider to cache all the data, thats why I’m not sure is a pointless “optimization”.

That seems fine to me just to skip the IO of loading the file again.

If you’re not doing it often then there isn’t really a reason to hold onto the data.

Are you doing something unique per mesh after they have been created? If not, the biggest optimization I see is in sharing identical meshes rather than in just speeding up their creation process.

Yes I do some uv animating and vertex coloring. I opted to have shared materials instead of meshes for coloring and animating, there are few cases where I could use shared meshes. I guess I can put another specific case on the mesh parser to check if is completly non-dynamic and static and then in this case share the mesh, not sure if is worth it to over complicate the thing as shared meshes would be used in few cases.