Skip to content

Commit

Permalink
Merge pull request #33026 from vespa-engine/vekterli/wire-proton-cfg-…
Browse files Browse the repository at this point in the history
…to-cache-lfu

Wire proton config to cache LFU sketch size
  • Loading branch information
vekterli authored Dec 11, 2024
2 parents 038c83b + 0ebecc9 commit 68dde35
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
12 changes: 12 additions & 0 deletions configdefinitions/src/vespa/proton.def
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion searchcore/src/vespa/searchcore/proton/server/proton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostingListCache>(params);
}

Expand Down
12 changes: 9 additions & 3 deletions searchlib/src/vespa/searchlib/diskindex/posting_list_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PostingListCache::Cache : public vespalib::cache<CacheParams> {
public:
using Parent = vespalib::cache<CacheParams>;
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); }
};

Expand All @@ -82,7 +82,7 @@ class PostingListCache::BitVectorCache : public vespalib::cache<BitVectorCachePa
public:
using Parent = vespalib::cache<BitVectorCacheParams>;
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); }
};

Expand All @@ -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;
Expand Down
23 changes: 17 additions & 6 deletions searchlib/src/vespa/searchlib/diskindex/posting_list_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ class PostingListCache : public IPostingListCache {
std::unique_ptr<BitVectorCache> _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)
{
}

Expand All @@ -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);
Expand Down

0 comments on commit 68dde35

Please sign in to comment.