From 23a7b61e56aa3beabfa15a080c1ee2d8967f07c3 Mon Sep 17 00:00:00 2001 From: shekhar suman Date: Fri, 13 Feb 2026 10:37:24 +0530 Subject: [PATCH 1/3] perf(utils): optimize CacheMap concurrency with std::shared_mutex --- lib/inc/drogon/CacheMap.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/inc/drogon/CacheMap.h b/lib/inc/drogon/CacheMap.h index e36202fe58..2b2a749c91 100644 --- a/lib/inc/drogon/CacheMap.h +++ b/lib/inc/drogon/CacheMap.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -222,14 +223,14 @@ class CacheMap if (timeout > 0) { MapValue v{std::move(value), timeout, std::move(timeoutCallback)}; - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); map_.insert(std::make_pair(key, std::move(v))); eraseAfter(timeout, key); } else { MapValue v{std::move(value)}; - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); map_.insert(std::make_pair(key, std::move(v))); } if (fnOnInsert_) @@ -254,14 +255,14 @@ class CacheMap if (timeout > 0) { MapValue v{value, timeout, std::move(timeoutCallback)}; - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); map_.insert(std::make_pair(key, std::move(v))); eraseAfter(timeout, key); } else { MapValue v{value}; - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); map_.insert(std::make_pair(key, std::move(v))); } if (fnOnInsert_) @@ -280,7 +281,7 @@ class CacheMap T2 operator[](const T1 &key) { size_t timeout = 0; - std::lock_guard lock(mtx_); + std::shared_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { @@ -312,7 +313,7 @@ class CacheMap void modify(const T1 &key, Callable &&handler, size_t timeout = 0) { { - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { @@ -341,7 +342,8 @@ class CacheMap size_t timeout = 0; bool flag = false; - std::lock_guard lock(mtx_); + // Using shared_lock to allow other threads as well at the same time + std::shared_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { @@ -364,7 +366,7 @@ class CacheMap { size_t timeout = 0; bool flag = false; - std::lock_guard lock(mtx_); + std::shared_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { @@ -388,7 +390,7 @@ class CacheMap { // in this case,we don't evoke the timeout callback; { - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); map_.erase(key); } if (fnOnErase_) @@ -453,7 +455,7 @@ class CacheMap std::atomic ticksCounter_{0}; - std::mutex mtx_; + std::shared_mutex mtx_; std::mutex bucketMutex_; trantor::TimerId timerId_; trantor::EventLoop *loop_; @@ -529,7 +531,7 @@ class CacheMap bool erased{false}; std::function timeoutCallback; { - std::lock_guard lock(mtx_); + std::unique_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { From dea1f1f003c0dacbcf6a88a6a0f0fee43c0d8be7 Mon Sep 17 00:00:00 2001 From: shekhar suman Date: Fri, 13 Feb 2026 18:26:37 +0530 Subject: [PATCH 2/3] resolve race condition --- lib/inc/drogon/CacheMap.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/inc/drogon/CacheMap.h b/lib/inc/drogon/CacheMap.h index 2b2a749c91..6dd9ddd867 100644 --- a/lib/inc/drogon/CacheMap.h +++ b/lib/inc/drogon/CacheMap.h @@ -281,7 +281,7 @@ class CacheMap T2 operator[](const T1 &key) { size_t timeout = 0; - std::shared_lock lock(mtx_); + std::unique_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { @@ -342,8 +342,8 @@ class CacheMap size_t timeout = 0; bool flag = false; - // Using shared_lock to allow other threads as well at the same time - std::shared_lock lock(mtx_); + // We use unique_lock here because eraseAfter modifies the internal map state + std::unique_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { @@ -366,7 +366,7 @@ class CacheMap { size_t timeout = 0; bool flag = false; - std::shared_lock lock(mtx_); + std::unique_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end()) { From 59c11d2f1ddaa7f95523522f69e8088f8242a66b Mon Sep 17 00:00:00 2001 From: shekhar suman Date: Fri, 13 Feb 2026 18:33:16 +0530 Subject: [PATCH 3/3] style: fix formatting in CacheMap.h --- lib/inc/drogon/CacheMap.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/inc/drogon/CacheMap.h b/lib/inc/drogon/CacheMap.h index 6dd9ddd867..cac2edd054 100644 --- a/lib/inc/drogon/CacheMap.h +++ b/lib/inc/drogon/CacheMap.h @@ -342,7 +342,8 @@ class CacheMap size_t timeout = 0; bool flag = false; - // We use unique_lock here because eraseAfter modifies the internal map state + // We use unique_lock here because eraseAfter modifies the internal map + // state std::unique_lock lock(mtx_); auto iter = map_.find(key); if (iter != map_.end())