Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Move KisRotatingCache to a separate file and add a unittest
Dmitry Kazakov2ca1afd42d4a
diff --git a/libs/global/KisRotatingCache.h b/libs/global/KisRotatingCache.hnew file mode 100644index 0000000000..3c50640147--- /dev/null+++ b/libs/global/KisRotatingCache.h@@ -0,0 +1,48 @@+/*+ * SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef KISROTATINGCACHE_H+#define KISROTATINGCACHE_H++#include <optional>+#include <utility>++#include <boost/container/static_vector.hpp>+++/**+ */+template <typename Key, typename Value, size_t CacheSize>+class KisRotatingCache+{+public:+ using CacheItem = std::pair<Key, Value>;+public:+ void push(Key key, Value value) {+ if (m_storage.size() == m_storage.max_size()) {+ m_storage.erase(m_storage.begin());+ }+ m_storage.emplace_back(std::move(key), std::move(value));+ }++ inline std::optional<Value> find(const Key &key) const {+ for (auto it = m_storage.begin(); it != m_storage.end(); ++it) {+ if (it->first == key) {+ return it->second;+ }+ }+ return std::nullopt;+ }++ void clear() {+ m_storage.clear();+ }++private:+ boost::container::static_vector<CacheItem, CacheSize> m_storage;+};++#endif // KISROTATINGCACHE_Hdiff --git a/libs/global/tests/KisGlobalTest.cpp b/libs/global/tests/KisGlobalTest.cppindex 6e609be410..73faaa11fe 100644--- a/libs/global/tests/KisGlobalTest.cpp+++ b/libs/global/tests/KisGlobalTest.cpp@@ -9,6 +9,7 @@#include "simpletest.h"#include "KisFileUtils.h"+#include "KisRotatingCache.h"#include <QTest>@@ -127,4 +128,28 @@ void KisGlobalTest::testDeduplicateFileName()QCOMPARE(result, expectedResult);}+void KisGlobalTest::testRotatingCacheOverflow()+{+ KisRotatingCache<int, int, 3> cache;++ cache.push(1, 1);+ QCOMPARE(cache.find(1), std::make_optional(1));++ cache.push(2, 2);+ QCOMPARE(cache.find(1), std::make_optional(1));+ QCOMPARE(cache.find(2), std::make_optional(2));++ cache.push(3, 3);+ QCOMPARE(cache.find(1), std::make_optional(1));+ QCOMPARE(cache.find(2), std::make_optional(2));+ QCOMPARE(cache.find(3), std::make_optional(3));++ cache.push(4, 4);+ QCOMPARE(cache.find(1), std::nullopt);+ QCOMPARE(cache.find(2), std::make_optional(2));+ QCOMPARE(cache.find(3), std::make_optional(3));+ QCOMPARE(cache.find(4), std::make_optional(4));++}+SIMPLE_TEST_MAIN(KisGlobalTest);diff --git a/libs/global/tests/KisGlobalTest.h b/libs/global/tests/KisGlobalTest.hindex 4088e19e07..541e30153c 100644--- a/libs/global/tests/KisGlobalTest.h+++ b/libs/global/tests/KisGlobalTest.h@@ -15,6 +15,8 @@ class KisGlobalTest : public QObjectprivate Q_SLOTS:void testDeduplicateFileName_data();void testDeduplicateFileName();++ void testRotatingCacheOverflow();};#endif // KISGLOBALTEST_Hdiff --git a/libs/pigment/KoColorConversionCache.cpp b/libs/pigment/KoColorConversionCache.cppindex 45f1326ee9..fc79695fc1 100644--- a/libs/pigment/KoColorConversionCache.cpp+++ b/libs/pigment/KoColorConversionCache.cpp@@ -15,42 +15,8 @@#include <KoColorSpace.h>-#include <boost/container/static_vector.hpp>+#include <KisRotatingCache.h>-template <typename Key, typename Value, size_t CacheSize>-class RotatingCache-{-public:- using CacheItem = std::pair<Key, Value>;-public:- RotatingCache()- : m_storage()- {- }-- void push(Key key, Value value) {- if (m_storage.size() == m_storage.max_size()) {- m_storage.erase(m_storage.begin());- }- m_storage.emplace_back(std::move(key), std::move(value));- }-- inline std::optional<Value> find(const Key &key) const {- for (auto it = m_storage.begin(); it != m_storage.end(); ++it) {- if (it->first == key) {- return it->second;- }- }- return std::nullopt;- }-- void clear() {- m_storage.clear();- }--private:- boost::container::static_vector<CacheItem, CacheSize> m_storage;-};struct KoColorConversionCacheKey {@@ -86,7 +52,7 @@ struct KoColorConversionCache::Private {QMultiHash< KoColorConversionCacheKey, CachedTransformation*> cache;QMutex cacheMutex;- using PerThreadRotatingCache = RotatingCache<KoColorConversionCacheKey, KoCachedColorConversionTransformation, 4>;+ using PerThreadRotatingCache = KisRotatingCache<KoColorConversionCacheKey, KoCachedColorConversionTransformation, 4>;QThreadStorage<PerThreadRotatingCache> fastStorage;};