diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-05-25 14:14:11 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-05-25 14:14:27 +0000 |
commit | 18a3749298480a2f188dce67adfd98d9a7621941 (patch) | |
tree | 0ac7358f5a0a39ed2d1c6a94bc252fa70068c800 /searchlib | |
parent | d180f4be67f0d0ea0e5dc474de4cd08b2dd71c54 (diff) |
- Use a rwlock as reading surpasses writing by a very large factor.
- size() does not need a lock.
Diffstat (limited to 'searchlib')
5 files changed, 33 insertions, 31 deletions
diff --git a/searchlib/src/tests/attribute/bitvector_search_cache/bitvector_search_cache_test.cpp b/searchlib/src/tests/attribute/bitvector_search_cache/bitvector_search_cache_test.cpp index b9c82892a97..bb65beed68b 100644 --- a/searchlib/src/tests/attribute/bitvector_search_cache/bitvector_search_cache_test.cpp +++ b/searchlib/src/tests/attribute/bitvector_search_cache/bitvector_search_cache_test.cpp @@ -9,8 +9,9 @@ using namespace search::attribute; using BitVectorSP = BitVectorSearchCache::BitVectorSP; using Entry = BitVectorSearchCache::Entry; +using EntrySP = std::shared_ptr<Entry>; -Entry::SP +EntrySP makeEntry() { return std::make_shared<Entry>(IDocumentMetaStoreContext::IReadGuard::SP(), BitVector::create(5), 10); @@ -18,8 +19,8 @@ makeEntry() struct Fixture { BitVectorSearchCache cache; - Entry::SP entry1; - Entry::SP entry2; + EntrySP entry1; + EntrySP entry2; Fixture() : cache(), entry1(makeEntry()), diff --git a/searchlib/src/tests/attribute/imported_search_context/imported_search_context_test.cpp b/searchlib/src/tests/attribute/imported_search_context/imported_search_context_test.cpp index 311d3ef71e7..61e66d384e1 100644 --- a/searchlib/src/tests/attribute/imported_search_context/imported_search_context_test.cpp +++ b/searchlib/src/tests/attribute/imported_search_context/imported_search_context_test.cpp @@ -467,7 +467,7 @@ struct SearchCacheFixture : Fixture { SearchCacheFixture::~SearchCacheFixture() = default; -BitVectorSearchCache::Entry::SP +std::shared_ptr<BitVectorSearchCache::Entry> makeSearchCacheEntry(const std::vector<uint32_t> docIds, uint32_t docIdLimit) { std::shared_ptr<BitVector> bitVector = BitVector::create(docIdLimit); diff --git a/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.cpp b/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.cpp index 4b8916273e7..70d34eef2ca 100644 --- a/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.cpp +++ b/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.cpp @@ -3,44 +3,45 @@ #include "bitvector_search_cache.h" #include <vespa/searchlib/common/bitvector.h> #include <vespa/vespalib/stllike/hash_map.hpp> +#include <mutex> namespace search::attribute { -using BitVectorSP = BitVectorSearchCache::BitVectorSP; +BitVectorSearchCache::BitVectorSearchCache() + : _mutex(), + _size(0), + _cache() +{} -BitVectorSearchCache::BitVectorSearchCache() = default; BitVectorSearchCache::~BitVectorSearchCache() = default; void -BitVectorSearchCache::insert(const vespalib::string &term, Entry::SP entry) +BitVectorSearchCache::insert(const vespalib::string &term, std::shared_ptr<Entry> entry) { - LockGuard guard(_mutex); + std::unique_lock guard(_mutex); _cache.insert(std::make_pair(term, std::move(entry))); + _size.store(_cache.size()); } -BitVectorSearchCache::Entry::SP +std::shared_ptr<BitVectorSearchCache::Entry> BitVectorSearchCache::find(const vespalib::string &term) const { - LockGuard guard(_mutex); - auto itr = _cache.find(term); - if (itr != _cache.end()) { - return itr->second; + if (size() > 0ul) { + std::shared_lock guard(_mutex); + auto itr = _cache.find(term); + if (itr != _cache.end()) { + return itr->second; + } } - return Entry::SP(); -} - -size_t -BitVectorSearchCache::size() const -{ - LockGuard guard(_mutex); - return _cache.size(); + return {}; } void BitVectorSearchCache::clear() { - LockGuard guard(_mutex); + std::unique_lock guard(_mutex); _cache.clear(); + _size.store(0ul, std::memory_order_relaxed); } } diff --git a/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.h b/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.h index 3936fb2ee67..455c27459cd 100644 --- a/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.h +++ b/searchlib/src/vespa/searchlib/attribute/bitvector_search_cache.h @@ -6,7 +6,8 @@ #include <vespa/vespalib/stllike/hash_map.h> #include <vespa/vespalib/stllike/string.h> #include <memory> -#include <mutex> +#include <shared_mutex> +#include <atomic> namespace search { class BitVector; } namespace search::attribute { @@ -22,7 +23,6 @@ public: using ReadGuardSP = IDocumentMetaStoreContext::IReadGuard::SP; struct Entry { - using SP = std::shared_ptr<Entry>; // We need to keep a document meta store read guard to ensure that no lids that are cached // in the bit vector are re-used until the guard is released. ReadGuardSP dmsReadGuard; @@ -33,18 +33,18 @@ public: }; private: - using LockGuard = std::lock_guard<std::mutex>; - using Cache = vespalib::hash_map<vespalib::string, Entry::SP>; + using Cache = vespalib::hash_map<vespalib::string, std::shared_ptr<Entry>>; - mutable std::mutex _mutex; + mutable std::shared_mutex _mutex; + std::atomic<uint64_t> _size; Cache _cache; public: BitVectorSearchCache(); ~BitVectorSearchCache(); - void insert(const vespalib::string &term, Entry::SP entry); - Entry::SP find(const vespalib::string &term) const; - size_t size() const; + void insert(const vespalib::string &term, std::shared_ptr<Entry> entry); + std::shared_ptr<Entry> find(const vespalib::string &term) const; + size_t size() const { return _size.load(std::memory_order_relaxed); } void clear(); }; diff --git a/searchlib/src/vespa/searchlib/attribute/imported_search_context.h b/searchlib/src/vespa/searchlib/attribute/imported_search_context.h index 77d09c55a41..3cbc9a3d97e 100644 --- a/searchlib/src/vespa/searchlib/attribute/imported_search_context.h +++ b/searchlib/src/vespa/searchlib/attribute/imported_search_context.h @@ -32,7 +32,7 @@ class ImportedSearchContext : public ISearchContext { const ImportedAttributeVector& _imported_attribute; vespalib::string _queryTerm; bool _useSearchCache; - BitVectorSearchCache::Entry::SP _searchCacheLookup; + std::shared_ptr<BitVectorSearchCache::Entry> _searchCacheLookup; IDocumentMetaStoreContext::IReadGuard::SP _dmsReadGuardFallback; const ReferenceAttribute& _reference_attribute; const IAttributeVector &_target_attribute; |