From aedaf51d0d8bdbeec58c4d220d9a24d1c886f79b Mon Sep 17 00:00:00 2001 From: maks0 Date: Mon, 7 Oct 2024 02:00:32 +0500 Subject: [PATCH 1/3] Fixed some mistakes in details cache optimization --- src/Layers/xrRender/DetailManager_CACHE.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Layers/xrRender/DetailManager_CACHE.cpp b/src/Layers/xrRender/DetailManager_CACHE.cpp index ca52afada0f..b4e25fd21aa 100644 --- a/src/Layers/xrRender/DetailManager_CACHE.cpp +++ b/src/Layers/xrRender/DetailManager_CACHE.cpp @@ -185,6 +185,10 @@ void CDetailManager::cache_Update(int v_x, int v_z, Fvector& view) float bestDistances[dm_max_decompress]; std::fill_n(bestDistances, dm_max_decompress, flt_max); + auto bestDistancesBegin = std::begin(bestDistances); + auto bestDistancesEnd = std::end(bestDistances); + ptrdiff_t maxIdx = 0; + for (u32 i = 0, size = cache_task.size(); i < size; ++i) { // Gain access to data @@ -197,17 +201,19 @@ void CDetailManager::cache_Update(int v_x, int v_z, Fvector& view) float D = view.distance_to_sqr(C); // Select - for (int j = 0; j < dm_max_decompress; ++j) + if (D < bestDistances[maxIdx]) { - if (D < bestDistances[j]) - { - bestDistances[j] = D; - bestIndexes[j] = i; - } + bestDistances[maxIdx] = D; + bestIndexes[maxIdx] = i; + + const auto maxIt = std::max_element(bestDistancesBegin, bestDistancesEnd); + maxIdx = std::distance(bestDistancesBegin, maxIt); } } - for (int i = 0; i < dm_max_decompress; ++i) + std::sort(bestIndexes, bestIndexes + dm_max_decompress); + + for (int i = dm_max_decompress - 1; i >= 0; --i) { const u32 bestId = bestIndexes[i]; From 0a186b9751ec1c77b0ff7930fca8cc5e04f8d86c Mon Sep 17 00:00:00 2001 From: maks0 Date: Thu, 10 Oct 2024 19:02:49 +0500 Subject: [PATCH 2/3] Added comment --- src/Layers/xrRender/DetailManager_CACHE.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Layers/xrRender/DetailManager_CACHE.cpp b/src/Layers/xrRender/DetailManager_CACHE.cpp index b4e25fd21aa..1ab7a06632a 100644 --- a/src/Layers/xrRender/DetailManager_CACHE.cpp +++ b/src/Layers/xrRender/DetailManager_CACHE.cpp @@ -211,6 +211,7 @@ void CDetailManager::cache_Update(int v_x, int v_z, Fvector& view) } } + // We sort indexes and process them in reverse order to erase correctly from cache_task std::sort(bestIndexes, bestIndexes + dm_max_decompress); for (int i = dm_max_decompress - 1; i >= 0; --i) From 9462a0ac288b55363ca84d655c86b32e626cf453 Mon Sep 17 00:00:00 2001 From: Sultan Uramaev Date: Thu, 10 Oct 2024 17:21:36 +0300 Subject: [PATCH 3/3] Update src/Layers/xrRender/DetailManager_CACHE.cpp --- src/Layers/xrRender/DetailManager_CACHE.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Layers/xrRender/DetailManager_CACHE.cpp b/src/Layers/xrRender/DetailManager_CACHE.cpp index 1ab7a06632a..d3fab4b11fe 100644 --- a/src/Layers/xrRender/DetailManager_CACHE.cpp +++ b/src/Layers/xrRender/DetailManager_CACHE.cpp @@ -211,7 +211,7 @@ void CDetailManager::cache_Update(int v_x, int v_z, Fvector& view) } } - // We sort indexes and process them in reverse order to erase correctly from cache_task + // Because indexes become invalid after an erase, sort them and process in the reverse order to erase everything correctly std::sort(bestIndexes, bestIndexes + dm_max_decompress); for (int i = dm_max_decompress - 1; i >= 0; --i)