From 18fecfbd07b2f449ec42234553bfe5ea426510cd Mon Sep 17 00:00:00 2001 From: oltolm Date: Fri, 31 Oct 2025 01:31:08 +0100 Subject: [PATCH] Misc: clean up some for loops --- common/LRUCache.h | 12 +++++------- pcsx2/GS/Renderers/DX12/D3D12StreamBuffer.cpp | 17 ++++++++--------- pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp | 17 ++++++++--------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/common/LRUCache.h b/common/LRUCache.h index 05764ea6ec..11af22b417 100644 --- a/common/LRUCache.h +++ b/common/LRUCache.h @@ -3,6 +3,7 @@ #pragma once #include "HeterogeneousContainers.h" +#include #include #include @@ -75,12 +76,9 @@ public: { while (!m_items.empty() && count > 0) { - typename MapType::iterator lowest = m_items.end(); - for (auto iter = m_items.begin(); iter != m_items.end(); ++iter) - { - if (lowest == m_items.end() || iter->second.last_access < lowest->second.last_access) - lowest = iter; - } + auto lowest = std::min_element(m_items.begin(), m_items.end(), [](const auto& a, const auto& b) { + return a.second.last_access < b.second.last_access; + }); m_items.erase(lowest); count--; } @@ -121,4 +119,4 @@ private: CounterType m_last_counter = 0; std::size_t m_max_capacity = 0; bool m_manual_evict = false; -}; \ No newline at end of file +}; diff --git a/pcsx2/GS/Renderers/DX12/D3D12StreamBuffer.cpp b/pcsx2/GS/Renderers/DX12/D3D12StreamBuffer.cpp index 4713751066..3f28ce4b88 100644 --- a/pcsx2/GS/Renderers/DX12/D3D12StreamBuffer.cpp +++ b/pcsx2/GS/Renderers/DX12/D3D12StreamBuffer.cpp @@ -197,20 +197,18 @@ bool D3D12StreamBuffer::WaitForClearSpace(u32 num_bytes) u32 new_space = 0; u32 new_gpu_position = 0; - auto iter = m_tracked_fences.begin(); - for (; iter != m_tracked_fences.end(); ++iter) - { + auto iter = std::find_if(m_tracked_fences.begin(), m_tracked_fences.end(), [&, this](const auto& iter) { // Would this fence bring us in line with the GPU? // This is the "last resort" case, where a command buffer execution has been forced // after no additional data has been written to it, so we can assume that after the // fence has been signaled the entire buffer is now consumed. - u32 gpu_position = iter->second; + u32 gpu_position = iter.second; if (m_current_offset == gpu_position) { new_offset = 0; new_space = m_size; new_gpu_position = 0; - break; + return true; } // Assuming that we wait for this fence, are we allocating in front of the GPU? @@ -225,7 +223,7 @@ bool D3D12StreamBuffer::WaitForClearSpace(u32 num_bytes) new_offset = m_current_offset; new_space = m_size - m_current_offset; new_gpu_position = gpu_position; - break; + return true; } // We can wrap around to the start, behind the GPU, if there is enough space. @@ -236,7 +234,7 @@ bool D3D12StreamBuffer::WaitForClearSpace(u32 num_bytes) new_offset = 0; new_space = gpu_position; new_gpu_position = gpu_position; - break; + return true; } } else @@ -251,10 +249,11 @@ bool D3D12StreamBuffer::WaitForClearSpace(u32 num_bytes) new_offset = m_current_offset; new_space = gpu_position - m_current_offset; new_gpu_position = gpu_position; - break; + return true; } } - } + return false; + }); // Did any fences satisfy this condition? // Has the command buffer been executed yet? If not, the caller should execute it. diff --git a/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp b/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp index 78ca8eea18..c0a6a4ccc3 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp +++ b/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp @@ -234,20 +234,18 @@ bool VKStreamBuffer::WaitForClearSpace(u32 num_bytes) u32 new_space = 0; u32 new_gpu_position = 0; - auto iter = m_tracked_fences.begin(); - for (; iter != m_tracked_fences.end(); ++iter) - { + auto iter = std::find_if(m_tracked_fences.begin(), m_tracked_fences.end(), [&, this](const auto& iter) { // Would this fence bring us in line with the GPU? // This is the "last resort" case, where a command buffer execution has been forced // after no additional data has been written to it, so we can assume that after the // fence has been signaled the entire buffer is now consumed. - u32 gpu_position = iter->second; + u32 gpu_position = iter.second; if (m_current_offset == gpu_position) { new_offset = 0; new_space = m_size; new_gpu_position = 0; - break; + return true; } // Assuming that we wait for this fence, are we allocating in front of the GPU? @@ -262,7 +260,7 @@ bool VKStreamBuffer::WaitForClearSpace(u32 num_bytes) new_offset = m_current_offset; new_space = m_size - m_current_offset; new_gpu_position = gpu_position; - break; + return true; } // We can wrap around to the start, behind the GPU, if there is enough space. @@ -273,7 +271,7 @@ bool VKStreamBuffer::WaitForClearSpace(u32 num_bytes) new_offset = 0; new_space = gpu_position - 1; new_gpu_position = gpu_position; - break; + return true; } } else @@ -288,10 +286,11 @@ bool VKStreamBuffer::WaitForClearSpace(u32 num_bytes) new_offset = m_current_offset; new_space = available_space_inbetween - 1; new_gpu_position = gpu_position; - break; + return true; } } - } + return false; + }); // Did any fences satisfy this condition? // Has the command buffer been executed yet? If not, the caller should execute it.