From 67d03aabd0c9b57d00e18d0aca8aca60e27f2ea9 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Tue, 21 Jun 2022 19:14:32 +0200 Subject: [PATCH] [dxvk] Make recycler a ring buffer Ensures that recycled objects actually get reused soon. Somewhat important for memory efficiency in descriptor pools. --- src/dxvk/dxvk_recycler.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/dxvk/dxvk_recycler.h b/src/dxvk/dxvk_recycler.h index b90befd4..2dd1d595 100644 --- a/src/dxvk/dxvk_recycler.h +++ b/src/dxvk/dxvk_recycler.h @@ -31,10 +31,10 @@ namespace dxvk { Rc retrieveObject() { std::lock_guard lock(m_mutex); - if (m_objectId == 0) + if (m_get == m_put) return nullptr; - return m_objects.at(--m_objectId); + return std::exchange(m_objects[(m_get++) % N], Rc()); } /** @@ -48,15 +48,17 @@ namespace dxvk { void returnObject(const Rc& object) { std::lock_guard lock(m_mutex); - if (m_objectId < N) - m_objects.at(m_objectId++) = object; + if (m_put - m_get < N) + m_objects[(m_put++) % N] = object; } private: - dxvk::mutex m_mutex; - std::array, N> m_objects; - size_t m_objectId = 0; + dxvk::mutex m_mutex; + std::array, N> m_objects; + + uint64_t m_get = 0; + uint64_t m_put = 0; };