From 0ebecc997f368fc395fe40f23778274cd3e6d51b Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Wed, 11 Dec 2024 13:12:21 +0000 Subject: [PATCH] Wire proton config to cache LFU sketch size --- configdefinitions/src/vespa/proton.def | 12 ++++++++++ .../vespa/searchcore/proton/server/proton.cpp | 5 +++- .../diskindex/posting_list_cache.cpp | 12 +++++++--- .../searchlib/diskindex/posting_list_cache.h | 23 ++++++++++++++----- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/configdefinitions/src/vespa/proton.def b/configdefinitions/src/vespa/proton.def index 59b8999d8df..a3053e5df9b 100644 --- a/configdefinitions/src/vespa/proton.def +++ b/configdefinitions/src/vespa/proton.def @@ -561,6 +561,18 @@ index.cache.bitvector.maxbytes long default=0 restart index.cache.postinglist.slru_protected_segment_ratio double default=0.0 restart index.cache.bitvector.slru_protected_segment_ratio double default=0.0 restart +## Configures the cache to use a LFU frequency sketch to estimate the frequency +## of cacheable elements and to prevent less frequently accessed elements from +## displacing more frequent ones. +## The specified element count should be at least as high as the expected number +## of elements the cache could contain (_not_ its size in bytes), as this has +## direct impact on how accurate the (inherently probabilistic) frequency +## estimates are. +## +## Any value greater than 0 enables LFU semantics, 0 disables LFU (default). +index.cache.postinglist.lfu_sketch_max_element_count long default=0 restart +index.cache.bitvector.lfu_sketch_max_element_count long default=0 restart + ## Specifies which tensor implementation to use for all backend code. ## ## TENSOR_ENGINE (default) uses DefaultTensorEngine, which has been the production implementation for years. diff --git a/searchcore/src/vespa/searchcore/proton/server/proton.cpp b/searchcore/src/vespa/searchcore/proton/server/proton.cpp index a107eedbe5f..80f9302054f 100644 --- a/searchcore/src/vespa/searchcore/proton/server/proton.cpp +++ b/searchcore/src/vespa/searchcore/proton/server/proton.cpp @@ -175,9 +175,12 @@ make_posting_list_cache(const ProtonConfig& cfg) // assumptions about how things work...! Otherwise, things will likely explode. posting_max_bytes = std::max(posting_max_bytes, 0L); int64_t bitvector_max_bytes = std::max(cfg.index.cache.bitvector.maxbytes, 0L); + int64_t posting_lfu_max_element_count = std::max(cfg.index.cache.postinglist.lfuSketchMaxElementCount, 0L); + int64_t bitvector_lfu_max_element_count = std::max(cfg.index.cache.bitvector.lfuSketchMaxElementCount, 0L); PostingListCache::CacheSizingParams params(posting_max_bytes, bitvector_max_bytes, cfg.index.cache.postinglist.slruProtectedSegmentRatio, - cfg.index.cache.bitvector.slruProtectedSegmentRatio); + cfg.index.cache.bitvector.slruProtectedSegmentRatio, + posting_lfu_max_element_count, bitvector_lfu_max_element_count); return std::make_shared(params); } diff --git a/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.cpp b/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.cpp index 505083adddc..93fd44c838b 100644 --- a/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.cpp +++ b/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.cpp @@ -56,7 +56,7 @@ class PostingListCache::Cache : public vespalib::cache { public: using Parent = vespalib::cache; Cache(BackingStore& backing_store, size_t max_bytes, size_t max_protected_bytes); - ~Cache(); + ~Cache() override; static size_t element_size() { return sizeof(value_type); } }; @@ -82,7 +82,7 @@ class PostingListCache::BitVectorCache : public vespalib::cache; BitVectorCache(BackingStore& backing_store, size_t max_bytes, size_t max_protected_bytes); - ~BitVectorCache(); + ~BitVectorCache() override; static size_t element_size() { return sizeof(value_type); } }; @@ -103,10 +103,16 @@ PostingListCache::PostingListCache(const CacheSizingParams& params) params.bitvector_slru_probationary_bytes(), params.bitvector_slru_protected_bytes())) { + if (params.posting_lfu_max_element_count() > 0) { + _cache->set_frequency_sketch_size(params.posting_lfu_max_element_count()); + } + if (params.bitvector_lfu_max_element_count() > 0) { + _bitvector_cache->set_frequency_sketch_size(params.bitvector_lfu_max_element_count()); + } } PostingListCache::PostingListCache(size_t max_bytes, size_t bitvector_max_bytes) - : PostingListCache(CacheSizingParams(max_bytes, bitvector_max_bytes, 0.0, 0.0)) + : PostingListCache(CacheSizingParams(max_bytes, bitvector_max_bytes, 0.0, 0.0, 0, 0)) {} PostingListCache::~PostingListCache() = default; diff --git a/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.h b/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.h index 9f2b7680b38..87c13b07f8b 100644 --- a/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.h +++ b/searchlib/src/vespa/searchlib/diskindex/posting_list_cache.h @@ -21,18 +21,23 @@ class PostingListCache : public IPostingListCache { std::unique_ptr _bitvector_cache; public: class CacheSizingParams { - size_t _posting_max_bytes = 0; - size_t _bitvector_max_bytes = 0; - double _posting_slru_protected_ratio = 0.0; // [0, 1] - double _bitvector_slru_protected_ratio = 0.0; // [0, 1] + size_t _posting_max_bytes = 0; + size_t _bitvector_max_bytes = 0; + double _posting_slru_protected_ratio = 0.0; // [0, 1] + double _bitvector_slru_protected_ratio = 0.0; // [0, 1] + size_t _posting_lfu_max_element_count = 0; + size_t _bitvector_lfu_max_element_count = 0; public: constexpr CacheSizingParams() noexcept = default; CacheSizingParams(size_t posting_max_bytes, size_t bitvector_max_bytes, - double posting_slru_protected_ratio, double bitvector_slru_protected_ratio) noexcept + double posting_slru_protected_ratio, double bitvector_slru_protected_ratio, + size_t posting_lfu_max_element_count, size_t bitvector_lfu_max_element_count) noexcept : _posting_max_bytes(posting_max_bytes), _bitvector_max_bytes(bitvector_max_bytes), _posting_slru_protected_ratio(std::min(std::max(posting_slru_protected_ratio, 0.0), 1.0)), - _bitvector_slru_protected_ratio(std::min(std::max(bitvector_slru_protected_ratio, 0.0), 1.0)) + _bitvector_slru_protected_ratio(std::min(std::max(bitvector_slru_protected_ratio, 0.0), 1.0)), + _posting_lfu_max_element_count(posting_lfu_max_element_count), + _bitvector_lfu_max_element_count(bitvector_lfu_max_element_count) { } @@ -55,6 +60,12 @@ class PostingListCache : public IPostingListCache { [[nodiscard]] size_t bitvector_slru_probationary_bytes() const noexcept { return _bitvector_max_bytes - bitvector_slru_protected_bytes(); } + [[nodiscard]] size_t posting_lfu_max_element_count() const noexcept { + return _posting_lfu_max_element_count; + } + [[nodiscard]] size_t bitvector_lfu_max_element_count() const noexcept { + return _bitvector_lfu_max_element_count; + } }; explicit PostingListCache(const CacheSizingParams& params);