Currently, ViewLayers' collection and objects cache is rebuilt way too much (it can be thousands of time during a single edit action in a relatively heavy scene).
Use a lazy update approach:
- Code changing the the data only tags the relevant view layers' caches to be updated.
- Code actually using those caches is responsible to call a new 'ensure()' callback first, which will check those flags and rebuild the cache as needed.
We can stay fairly general (tag a whole cache as dirty, and just fully rebuild it then when required), or be more specific by tagging every ID that changed, and then cache rebuilding code could only update the parts that need it.
Profiling on worst-case scenarii should tell us if high level of granularity is really needed here (worst case would be a viewlayer with many collections and objects).
Another possibility would be to add an API to actually update the needed parts of the caches on the fly (called from code modifying the related IDs relationships), but this is likely more complex to get working.
Note: Thread safety should not be an issue here, at least there is none at all in current code…