Make OpenSim::Mesh
refcounted on copy
#4010
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes a practical issue that I encounter downstream in opensim-creator when loading/editing models that contain a large amount of expensive-to-load mesh data.
In OSC,
OpenSim::Model
s are copied around quite a bit. E.g. whenever a user performs any model-editing action, it takes a copy of the model, so that if any user edits result in an error/exception there's an easy way to roll it back. Undo/redo works similarly, simulating a model on a separate thread works similarly, and so on - it's computationally expensive, but very robust to user-enacted edits, which OSC has to manage a lot of.My problem is that, whenever a system wants to use a model copy, it usually has to re-call
buildSystem
,finalizeFromProperties
, etc. on the copy. If the model has a lot of mesh data, it can take a very long time (e.g. multiple seconds) for the implementation to re-load and re-parse the mesh data from disk. It also increases memory usage in the case where multiple fully-finalized-and-built models are used concurrently (e.g. one model in the UI, one in a simulator).If you want a concrete example, load https://simtk.org/projects/dogmodel in OpenSim Creator, edit it a bit (probably fast) and then
Undo
it. The undo will take forever because the model copy (in the undo buffer) will be re-finalized and reload all the mesh data.The solution is to reference-count the mesh data once it's loaded, so that all copies of
OpenSim::Mesh
share a pointer to an immutable copy of the mesh data. This, however, has some additional drawbacks/considerations:OpenSim::Mesh
loading needs to happen more proactively, duringextendFinalizeFromProperties
, because copies may be made beforegenerateDecorations
is called (which is what's currently lazy-loading mesh data). **This might affect how @aymanhab is loading mesh data in OpenSim GUI (i.e. is the lazy loading still necessary)?extendFinalizeFromProperties
The implementation now has to track whether the underlying (resolved) filesystem path has changed, or whether the timestamp of the underlying mesh data is newer than what's loaded, because the existing implementation always reloaded, but we're now caching (so we need a cache invalidation mechanism). Therefore, the implementation now has to store+check paths, file timestamps, etc.Brief summary of changes
OpenSim::Mesh::extendFinalizeFromProperties
to perform property checking, mesh loading, and mesh cachingTesting I've completed
Looking for feedback on...
OpenSim::Mesh::extendFinalizeFromProperties
. I didn't just whack the caching in. I also ended up rewriting most of the existing code in order to make it easier to understand what each step is doing.CHANGELOG.md
This change is![Reviewable](https://camo.githubusercontent.com/1541c4039185914e83657d3683ec25920c672c6c5c7ab4240ee7bff601adec0b/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)