From 75557f3eb80e678f5ca8bc3dfb5fa9147c09fa7c Mon Sep 17 00:00:00 2001 From: Buqian Zheng Date: Mon, 22 Sep 2025 18:02:09 +0800 Subject: [PATCH] enhance: Use std::shared_lock and std::unique_lock for mutexes (#44459) issue: https://github.com/milvus-io/milvus/issues/44452 Signed-off-by: zhengbuqian Co-authored-by: buqian.zheng --- internal/core/src/index/InvertedIndexTantivy.cpp | 9 +++++---- internal/core/src/index/JsonInvertedIndex.cpp | 3 ++- internal/core/src/index/JsonInvertedIndex.h | 3 ++- internal/core/src/index/TextMatchIndex.cpp | 7 ++++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/core/src/index/InvertedIndexTantivy.cpp b/internal/core/src/index/InvertedIndexTantivy.cpp index c10cb42dcb..1721eeb29b 100644 --- a/internal/core/src/index/InvertedIndexTantivy.cpp +++ b/internal/core/src/index/InvertedIndexTantivy.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include "InvertedIndexTantivy.h" @@ -105,7 +106,7 @@ InvertedIndexTantivy::finish() { template BinarySet InvertedIndexTantivy::Serialize(const Config& config) { - folly::SharedMutex::ReadHolder lock(mutex_); + std::shared_lock lock(mutex_); auto index_valid_data_length = null_offset_.size() * sizeof(size_t); std::shared_ptr index_valid_data( new uint8_t[index_valid_data_length]); @@ -293,7 +294,7 @@ InvertedIndexTantivy::IsNull() { }; if (is_growing_) { - folly::SharedMutex::ReadHolder lock(mutex_); + std::shared_lock lock(mutex_); fill_bitset(); } else { fill_bitset(); @@ -317,7 +318,7 @@ InvertedIndexTantivy::IsNotNull() { }; if (is_growing_) { - folly::SharedMutex::ReadHolder lock(mutex_); + std::shared_lock lock(mutex_); fill_bitset(); } else { fill_bitset(); @@ -365,7 +366,7 @@ InvertedIndexTantivy::NotIn(size_t n, const T* values) { }; if (is_growing_) { - folly::SharedMutex::ReadHolder lock(mutex_); + std::shared_lock lock(mutex_); fill_bitset(); } else { fill_bitset(); diff --git a/internal/core/src/index/JsonInvertedIndex.cpp b/internal/core/src/index/JsonInvertedIndex.cpp index 5e4713f7b6..5242e31be8 100644 --- a/internal/core/src/index/JsonInvertedIndex.cpp +++ b/internal/core/src/index/JsonInvertedIndex.cpp @@ -11,6 +11,7 @@ #include "index/JsonInvertedIndex.h" #include +#include #include #include #include "common/EasyAssert.h" @@ -84,7 +85,7 @@ JsonInvertedIndex::Exists() { }; if (this->is_growing_) { - folly::SharedMutex::ReadHolder lock(this->mutex_); + std::shared_lock lock(this->mutex_); fill_bitset(); } else { fill_bitset(); diff --git a/internal/core/src/index/JsonInvertedIndex.h b/internal/core/src/index/JsonInvertedIndex.h index e7a0fd6fd6..803c6299b8 100644 --- a/internal/core/src/index/JsonInvertedIndex.h +++ b/internal/core/src/index/JsonInvertedIndex.h @@ -11,6 +11,7 @@ #pragma once #include +#include #include "common/Slice.h" #include "common/FieldDataInterface.h" #include "common/JsonCastFunction.h" @@ -134,7 +135,7 @@ class JsonInvertedIndex : public index::InvertedIndexTantivy { BinarySet Serialize(const Config& config) override { - folly::SharedMutex::ReadHolder lock(this->mutex_); + std::shared_lock lock(this->mutex_); auto index_valid_data_length = this->null_offset_.size() * sizeof(size_t); std::shared_ptr index_valid_data( diff --git a/internal/core/src/index/TextMatchIndex.cpp b/internal/core/src/index/TextMatchIndex.cpp index 01a966f670..cfb00b46d6 100644 --- a/internal/core/src/index/TextMatchIndex.cpp +++ b/internal/core/src/index/TextMatchIndex.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "index/TextMatchIndex.h" #include "index/InvertedIndexUtil.h" @@ -211,7 +212,7 @@ TextMatchIndex::AddTextsGrowing(size_t n, for (int i = 0; i < n; i++) { auto offset = i + offset_begin; if (!valids[i]) { - folly::SharedMutex::WriteHolder lock(mutex_); + std::unique_lock lock(mutex_); null_offset_.push_back(offset); } } @@ -233,14 +234,14 @@ TextMatchIndex::BuildIndexFromFieldData( total += data->get_null_count(); } { - folly::SharedMutex::WriteHolder lock(mutex_); + std::unique_lock lock(mutex_); null_offset_.reserve(total); } for (const auto& data : field_datas) { auto n = data->get_num_rows(); for (int i = 0; i < n; i++) { if (!data->is_valid(i)) { - folly::SharedMutex::WriteHolder lock(mutex_); + std::unique_lock lock(mutex_); null_offset_.push_back(i); } wrapper_->add_data(