From 4f70d8c830c6213bafcbf40f5ff7eb72840739cc Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Fri, 2 Jan 2026 21:34:34 -0500 Subject: secondlife/viewer#2462: Optimize unloading of prims Signed-off-by: Rye --- indra/newview/llmeshrepository.cpp | 81 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) (limited to 'indra/newview/llmeshrepository.cpp') diff --git a/indra/newview/llmeshrepository.cpp b/indra/newview/llmeshrepository.cpp index c0b1a5326a..c3513229c6 100644 --- a/indra/newview/llmeshrepository.cpp +++ b/indra/newview/llmeshrepository.cpp @@ -4271,20 +4271,95 @@ S32 LLMeshRepository::update() return static_cast(size); } -void LLMeshRepository::unregisterMesh(LLVOVolume* vobj) +#ifdef SHOW_ASSERT +// Brute-force remove the object from all loading queues. Returns true if +// something was removed. +// This function is used in a debug assert to ensure unregisterMesh and +// unregisterSkinInfo are called as intended. +// *TODO: Consider removing at some point if we feel confident about the code +// working as intended. +bool LLMeshRepository::forceUnregisterMesh(LLVOVolume* vobj) { + LL_PROFILE_ZONE_SCOPED_CATEGORY_VOLUME; + + bool found = false; + for (auto& lod : mLoadingMeshes) { for (auto& param : lod) { - vector_replace_with_last(param.second.mVolumes, vobj); + llassert(std::find(param.second.mVolumes.begin(), param.second.mVolumes.end(), vobj) == param.second.mVolumes.end()); + found = found || vector_replace_with_last(param.second.mVolumes, vobj); } } for (auto& skin_pair : mLoadingSkins) { - vector_replace_with_last(skin_pair.second.mVolumes, vobj); + llassert(std::find(skin_pair.mVolumes.second.begin(), skin_pair.mVolumes.second.end(), vobj) == skin_pair.mVolumes.second.end()); + found = found || vector_replace_with_last(skin_pair.second.mVolumes, vobj); + } + + return found; +} +#endif + +void LLMeshRepository::unregisterMesh(LLVOVolume* vobj, const LLVolumeParams& mesh_params, S32 detail) +{ + LL_PROFILE_ZONE_SCOPED_CATEGORY_VOLUME; + + llassert((mesh_params.getSculptType() & LL_SCULPT_TYPE_MASK) == LL_SCULPT_TYPE_MESH); + llassert(mesh_params.getSculptID().notNull()); + auto& lod = mLoadingMeshes[detail]; + auto param_iter = lod.find(mesh_params.getSculptID()); + if (param_iter != lod.end()) + { + vector_replace_with_last(param_iter->second.mVolumes, vobj); + llassert(!vector_replace_with_last(param_iter->second.mVolumes, vobj)); + if (param_iter->second.mVolumes.empty()) + { + lod.erase(param_iter); + } + } +} + +void LLMeshRepository::unregisterSkinInfo(const LLUUID& mesh_id, LLVOVolume* vobj) +{ + LL_PROFILE_ZONE_SCOPED_CATEGORY_VOLUME; + + llassert(mesh_id.notNull()); + auto skin_pair_iter = mLoadingSkins.find(mesh_id); + if (skin_pair_iter != mLoadingSkins.end()) + { + vector_replace_with_last(skin_pair_iter->second.mVolumes, vobj); + llassert(!vector_replace_with_last(skin_pair_iter->second.mVolumes, vobj)); + if (skin_pair_iter->second.mVolumes.empty()) + { + mLoadingSkins.erase(skin_pair_iter); + } + } +} + +// Lots of dead objects make expensive calls to +// LLMeshRepository::unregisterMesh which may delay shutdown. Avoid this by +// preemptively unregistering all meshes. +// We can also do this safely if all objects are confirmed dead for some other +// reason. +void LLMeshRepository::unregisterAllMeshes() +{ + LL_PROFILE_ZONE_SCOPED_CATEGORY_VOLUME; + + // The size of mLoadingMeshes and mLoadingSkins may be large and thus + // expensive to iterate over in LLVOVolume::~LLVOVolume. + // This is unnecessary during shutdown, so we ignore the referenced objects in the + // least expensive way which is still safe: by clearing these containers. + // Clear now and not in LLMeshRepository::shutdown because + // LLMeshRepository::notifyLoadedMeshes could (depending on invocation + // order) reference a pointer to an object after it has been deleted. + for (auto& lod : mLoadingMeshes) + { + lod.clear(); } + mLoadingSkins.clear(); } S32 LLMeshRepository::loadMesh(LLVOVolume* vobj, const LLVolumeParams& mesh_params, S32 new_lod, S32 last_lod) -- cgit v1.3 From 24dccc4015a291b819c2a4bb0451c8898e5a3cc2 Mon Sep 17 00:00:00 2001 From: Cosmic Linden Date: Thu, 19 Sep 2024 17:34:28 -0700 Subject: secondlife/viewer#2623: Remove assert Signed-off-by: Rye --- indra/newview/llmeshrepository.cpp | 32 -------------------------------- indra/newview/llmeshrepository.h | 3 --- indra/newview/llvovolume.cpp | 1 - 3 files changed, 36 deletions(-) (limited to 'indra/newview/llmeshrepository.cpp') diff --git a/indra/newview/llmeshrepository.cpp b/indra/newview/llmeshrepository.cpp index c3513229c6..8ee5a4f028 100644 --- a/indra/newview/llmeshrepository.cpp +++ b/indra/newview/llmeshrepository.cpp @@ -4271,38 +4271,6 @@ S32 LLMeshRepository::update() return static_cast(size); } -#ifdef SHOW_ASSERT -// Brute-force remove the object from all loading queues. Returns true if -// something was removed. -// This function is used in a debug assert to ensure unregisterMesh and -// unregisterSkinInfo are called as intended. -// *TODO: Consider removing at some point if we feel confident about the code -// working as intended. -bool LLMeshRepository::forceUnregisterMesh(LLVOVolume* vobj) -{ - LL_PROFILE_ZONE_SCOPED_CATEGORY_VOLUME; - - bool found = false; - - for (auto& lod : mLoadingMeshes) - { - for (auto& param : lod) - { - llassert(std::find(param.second.mVolumes.begin(), param.second.mVolumes.end(), vobj) == param.second.mVolumes.end()); - found = found || vector_replace_with_last(param.second.mVolumes, vobj); - } - } - - for (auto& skin_pair : mLoadingSkins) - { - llassert(std::find(skin_pair.mVolumes.second.begin(), skin_pair.mVolumes.second.end(), vobj) == skin_pair.mVolumes.second.end()); - found = found || vector_replace_with_last(skin_pair.second.mVolumes, vobj); - } - - return found; -} -#endif - void LLMeshRepository::unregisterMesh(LLVOVolume* vobj, const LLVolumeParams& mesh_params, S32 detail) { LL_PROFILE_ZONE_SCOPED_CATEGORY_VOLUME; diff --git a/indra/newview/llmeshrepository.h b/indra/newview/llmeshrepository.h index 886ef70a07..b0dd8d7ba1 100644 --- a/indra/newview/llmeshrepository.h +++ b/indra/newview/llmeshrepository.h @@ -866,9 +866,6 @@ public: void shutdown(); S32 update(); -#ifdef SHOW_ASSERT - bool forceUnregisterMesh(LLVOVolume* volume); -#endif void unregisterMesh(LLVOVolume* vobj, const LLVolumeParams& mesh_params, S32 detail); void unregisterSkinInfo(const LLUUID& mesh_id, LLVOVolume* vobj); //mesh management functions diff --git a/indra/newview/llvovolume.cpp b/indra/newview/llvovolume.cpp index c68ed01cc1..b328d3a414 100644 --- a/indra/newview/llvovolume.cpp +++ b/indra/newview/llvovolume.cpp @@ -251,7 +251,6 @@ LLVOVolume::~LLVOVolume() mVolumeImpl = NULL; unregisterOldMeshAndSkin(); - llassert(!gMeshRepo.forceUnregisterMesh(this)); if(!mMediaImplList.empty()) { -- cgit v1.3 From 23bfecf0b1dd92efcd743ec00637cd1122220fb9 Mon Sep 17 00:00:00 2001 From: Rye Date: Fri, 2 Jan 2026 09:50:34 -0500 Subject: Further optimize unregistration of loading meshes via std::unordered_set This container type is better suited to the repeated find and erase patterns used here. Signed-off-by: Rye --- indra/newview/llmeshrepository.cpp | 12 ++++++------ indra/newview/llmeshrepository.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'indra/newview/llmeshrepository.cpp') diff --git a/indra/newview/llmeshrepository.cpp b/indra/newview/llmeshrepository.cpp index 8ee5a4f028..ed342935ad 100644 --- a/indra/newview/llmeshrepository.cpp +++ b/indra/newview/llmeshrepository.cpp @@ -4281,8 +4281,8 @@ void LLMeshRepository::unregisterMesh(LLVOVolume* vobj, const LLVolumeParams& me auto param_iter = lod.find(mesh_params.getSculptID()); if (param_iter != lod.end()) { - vector_replace_with_last(param_iter->second.mVolumes, vobj); - llassert(!vector_replace_with_last(param_iter->second.mVolumes, vobj)); + param_iter->second.mVolumes.erase(vobj); + llassert(!param_iter->second.mVolumes.contains(vobj)); if (param_iter->second.mVolumes.empty()) { lod.erase(param_iter); @@ -4298,8 +4298,8 @@ void LLMeshRepository::unregisterSkinInfo(const LLUUID& mesh_id, LLVOVolume* vob auto skin_pair_iter = mLoadingSkins.find(mesh_id); if (skin_pair_iter != mLoadingSkins.end()) { - vector_replace_with_last(skin_pair_iter->second.mVolumes, vobj); - llassert(!vector_replace_with_last(skin_pair_iter->second.mVolumes, vobj)); + skin_pair_iter->second.mVolumes.erase(vobj); + llassert(!skin_pair_iter->second.mVolumes.contains(vobj)); if (skin_pair_iter->second.mVolumes.empty()) { mLoadingSkins.erase(skin_pair_iter); @@ -4349,7 +4349,7 @@ S32 LLMeshRepository::loadMesh(LLVOVolume* vobj, const LLVolumeParams& mesh_para mesh_load_map::iterator iter = mLoadingMeshes[new_lod].find(mesh_id); if (iter != mLoadingMeshes[new_lod].end()) { //request pending for this mesh, append volume id to list - auto it = std::find(iter->second.mVolumes.begin(), iter->second.mVolumes.end(), vobj); + auto it = iter->second.mVolumes.find(vobj); if (it == iter->second.mVolumes.end()) { iter->second.addVolume(vobj); } @@ -4847,7 +4847,7 @@ const LLMeshSkinInfo* LLMeshRepository::getSkinInfo(const LLUUID& mesh_id, LLVOV skin_load_map::iterator iter = mLoadingSkins.find(mesh_id); if (iter != mLoadingSkins.end()) { //request pending for this mesh, append volume id to list - auto it = std::find(iter->second.mVolumes.begin(), iter->second.mVolumes.end(), requesting_obj); + auto it = iter->second.mVolumes.find(requesting_obj); if (it == iter->second.mVolumes.end()) { iter->second.addVolume(requesting_obj); } diff --git a/indra/newview/llmeshrepository.h b/indra/newview/llmeshrepository.h index b0dd8d7ba1..56ad8297c7 100644 --- a/indra/newview/llmeshrepository.h +++ b/indra/newview/llmeshrepository.h @@ -290,7 +290,7 @@ private: class MeshLoadData { public: - MeshLoadData() {} + MeshLoadData() = default; ~MeshLoadData() { if (std::shared_ptr request = mRequest.lock()) @@ -300,19 +300,19 @@ public: } void initData(LLVOVolume* vol, std::shared_ptr& request) { - mVolumes.push_back(vol); + mVolumes.insert(vol); request->trackData(this); mRequest = request; } void addVolume(LLVOVolume* vol) { - mVolumes.push_back(vol); + mVolumes.insert(vol); if (std::shared_ptr request = mRequest.lock()) { request->setScoreDirty(); } } - std::vector mVolumes; + std::unordered_set mVolumes; private: std::weak_ptr mRequest; }; -- cgit v1.3