diff options
author | babenko <babenko@yandex-team.com> | 2024-09-29 20:13:38 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2024-09-29 20:23:20 +0300 |
commit | 4352a69d4659de740b505f63d3f651fba29452f1 (patch) | |
tree | 2d5103b9b3c5de4c2b998a0ce4872f1785a40f3f | |
parent | ac0a093c4e1d1203063631b27de0f6f38662804d (diff) | |
download | ydb-4352a69d4659de740b505f63d3f651fba29452f1.tar.gz |
Cosmetics in caches
commit_hash:ba31e4605df73ac8386f06c52e2b4575b6dce220
-rw-r--r-- | yt/yt/core/http/connection_pool.cpp | 2 | ||||
-rw-r--r-- | yt/yt/core/misc/sync_cache-inl.h | 43 | ||||
-rw-r--r-- | yt/yt/core/misc/sync_cache.h | 43 | ||||
-rw-r--r-- | yt/yt/core/misc/unittests/lru_cache_ut.cpp | 34 |
4 files changed, 64 insertions, 58 deletions
diff --git a/yt/yt/core/http/connection_pool.cpp b/yt/yt/core/http/connection_pool.cpp index 24dc08d5b2..27e6965067 100644 --- a/yt/yt/core/http/connection_pool.cpp +++ b/yt/yt/core/http/connection_pool.cpp @@ -60,7 +60,7 @@ TFuture<IConnectionPtr> TConnectionPool::Connect( { auto guard = Guard(SpinLock_); - while (auto item = Connections_.Extract(address)) { + while (auto item = Connections_.TryExtract(address)) { if (item->GetIdleTime() < Config_->ConnectionIdleTimeout && item->IsOK()) { auto&& connection = item->Connection; YT_LOG_DEBUG("Connection is extracted from cache (Address: %v, ConnectionId: %v)", diff --git a/yt/yt/core/misc/sync_cache-inl.h b/yt/yt/core/misc/sync_cache-inl.h index 20ebee45df..88e625cc3b 100644 --- a/yt/yt/core/misc/sync_cache-inl.h +++ b/yt/yt/core/misc/sync_cache-inl.h @@ -525,14 +525,22 @@ void TMemoryTrackingSyncSlruCacheBase<TKey, TValue, THash>::Reconfigure(const TS //////////////////////////////////////////////////////////////////////////////// template <class TKey, class TValue, class THash> -TSimpleLruCache<TKey, TValue, THash>::TSimpleLruCache(size_t maxWeight) +TSimpleLruCache<TKey, TValue, THash>::TItem::TItem(TValue value, i64 weight) + : Value(std::move(value)) + , Weight(weight) +{ } + +//////////////////////////////////////////////////////////////////////////////// + +template <class TKey, class TValue, class THash> +TSimpleLruCache<TKey, TValue, THash>::TSimpleLruCache(i64 maxWeight) : MaxWeight_(maxWeight) { } template <class TKey, class TValue, class THash> -size_t TSimpleLruCache<TKey, TValue, THash>::GetSize() const +int TSimpleLruCache<TKey, TValue, THash>::GetSize() const { - return ItemMap_.size(); + return std::ssize(ItemMap_); } template <class TKey, class TValue, class THash> @@ -566,7 +574,7 @@ TValue* TSimpleLruCache<TKey, TValue, THash>::FindNoTouch(const TKey& key) } template <class TKey, class TValue, class THash> -TValue* TSimpleLruCache<TKey, TValue, THash>::Insert(const TKey& key, TValue value, size_t weight) +TValue* TSimpleLruCache<TKey, TValue, THash>::Insert(const TKey& key, TValue value, i64 weight) { { auto mapIt = ItemMap_.find(key); @@ -592,7 +600,7 @@ TValue* TSimpleLruCache<TKey, TValue, THash>::Insert(const TKey& key, TValue val } template <class TKey, class TValue, class THash> -void TSimpleLruCache<TKey, TValue, THash>::SetMaxWeight(size_t maxWeight) +void TSimpleLruCache<TKey, TValue, THash>::SetMaxWeight(i64 maxWeight) { MaxWeight_ = maxWeight; } @@ -625,14 +633,22 @@ void TSimpleLruCache<TKey, TValue, THash>::UpdateLruList(typename TItemMap::iter //////////////////////////////////////////////////////////////////////////////// template <class TKey, class TValue, class THash> -TMultiLruCache<TKey, TValue, THash>::TMultiLruCache(size_t maxWeight) +TMultiLruCache<TKey, TValue, THash>::TItem::TItem(TValue value, i64 weight) + : Value(std::move(value)) + , Weight(weight) +{ } + +//////////////////////////////////////////////////////////////////////////////// + +template <class TKey, class TValue, class THash> +TMultiLruCache<TKey, TValue, THash>::TMultiLruCache(i64 maxWeight) : MaxWeight_(maxWeight) { } template <class TKey, class TValue, class THash> -size_t TMultiLruCache<TKey, TValue, THash>::GetSize() const +int TMultiLruCache<TKey, TValue, THash>::GetSize() const { - return LruList_.size(); + return std::ssize(LruList_); } template <class TKey, class TValue, class THash> @@ -661,7 +677,7 @@ TValue* TMultiLruCache<TKey, TValue, THash>::Find(const TKey& key) } template <class TKey, class TValue, class THash> -TValue* TMultiLruCache<TKey, TValue, THash>::Insert(const TKey& key, TValue value, size_t weight) +TValue* TMultiLruCache<TKey, TValue, THash>::Insert(const TKey& key, TValue value, i64 weight) { YT_VERIFY(weight <= MaxWeight_); @@ -678,7 +694,7 @@ TValue* TMultiLruCache<TKey, TValue, THash>::Insert(const TKey& key, TValue valu } template <class TKey, class TValue, class THash> -std::optional<TValue> TMultiLruCache<TKey, TValue, THash>::Extract(const TKey& key) +std::optional<TValue> TMultiLruCache<TKey, TValue, THash>::TryExtract(const TKey& key) { auto mapIt = ItemMap_.find(key); if (mapIt == ItemMap_.end()) { @@ -702,6 +718,7 @@ std::optional<TValue> TMultiLruCache<TKey, TValue, THash>::Extract(const TKey& k template <class TKey, class TValue, class THash> TValue TMultiLruCache<TKey, TValue, THash>::Pop() { + YT_VERIFY(!LruList_.empty()); auto listIt = std::prev(LruList_.end()); auto mapIt = *listIt; @@ -730,12 +747,6 @@ void TMultiLruCache<TKey, TValue, THash>::Clear() } template <class TKey, class TValue, class THash> -TMultiLruCache<TKey, TValue, THash>::TItem::TItem(TValue value, size_t weight) - : Value(std::move(value)) - , Weight(weight) -{ } - -template <class TKey, class TValue, class THash> void TMultiLruCache<TKey, TValue, THash>::UpdateLruList(typename std::deque<TItem>::iterator dequeIt) { auto mapIt = *dequeIt->LruListIterator; diff --git a/yt/yt/core/misc/sync_cache.h b/yt/yt/core/misc/sync_cache.h index 732d4b0b1f..39691aefdc 100644 --- a/yt/yt/core/misc/sync_cache.h +++ b/yt/yt/core/misc/sync_cache.h @@ -89,8 +89,8 @@ private: TIntrusiveListWithAutoDelete<TItem, TDelete> YoungerLruList; TIntrusiveListWithAutoDelete<TItem, TDelete> OlderLruList; - size_t YoungerWeightCounter = 0; - size_t OlderWeightCounter = 0; + i64 YoungerWeightCounter = 0; + i64 OlderWeightCounter = 0; THashMap<TKey, TItem*, THash> ItemMap; @@ -119,7 +119,6 @@ private: void MoveToYounger(TShard* shard, TItem* item); void MoveToOlder(TShard* shard, TItem* item); void Pop(TShard* shard, TItem* item); - }; //////////////////////////////////////////////////////////////////////////////// @@ -129,7 +128,7 @@ class TMemoryTrackingSyncSlruCacheBase : public TSyncSlruCacheBase<TKey, TValue, THash> { public: - explicit TMemoryTrackingSyncSlruCacheBase( + TMemoryTrackingSyncSlruCacheBase( TSlruCacheConfigPtr config, IMemoryUsageTrackerPtr memoryTracker, const NProfiling::TProfiler& profiler = {}); @@ -154,34 +153,31 @@ template <class TKey, class TValue, class THash = THash<TKey>> class TSimpleLruCache { public: - explicit TSimpleLruCache(size_t maxWeight); + explicit TSimpleLruCache(i64 maxWeight); - size_t GetSize() const; + int GetSize() const; const TValue& Get(const TKey& key); TValue* Find(const TKey& key); TValue* FindNoTouch(const TKey& key); - TValue* Insert(const TKey& key, TValue value, size_t weight = 1); + TValue* Insert(const TKey& key, TValue value, i64 weight = 1); - void SetMaxWeight(size_t maxWeight); + void SetMaxWeight(i64 maxWeight); void Clear(); private: struct TItem { - TItem(TValue value, size_t weight) - : Value(std::move(value)) - , Weight(weight) - { } + TItem(TValue value, i64 weight); TValue Value; - size_t Weight; + const i64 Weight; typename std::list<typename THashMap<TKey, TItem, THash>::iterator>::iterator LruListIterator; }; - size_t MaxWeight_ = 0; - size_t CurrentWeight_ = 0; + i64 MaxWeight_ = 0; + i64 CurrentWeight_ = 0; using TItemMap = THashMap<TKey, TItem, THash>; TItemMap ItemMap_; @@ -197,16 +193,15 @@ template <class TKey, class TValue, class THash = THash<TKey>> class TMultiLruCache { public: - explicit TMultiLruCache(size_t maxWeight); + explicit TMultiLruCache(i64 maxWeight); - size_t GetSize() const; + int GetSize() const; const TValue& Get(const TKey& key); TValue* Find(const TKey& key); - TValue* Insert(const TKey& key, TValue value, size_t weight = 1); - std::optional<TValue> Extract(const TKey& key); - + TValue* Insert(const TKey& key, TValue value, i64 weight = 1); + std::optional<TValue> TryExtract(const TKey& key); TValue Pop(); void Clear(); @@ -219,10 +214,10 @@ private: struct TItem { - TItem(TValue value, size_t weight); + TItem(TValue value, i64 weight); TValue Value; - size_t Weight; + const i64 Weight; typename TLruList::iterator LruListIterator; }; @@ -230,8 +225,8 @@ private: void UpdateLruList(typename std::deque<TItem>::iterator listIt); private: - size_t MaxWeight_ = 0; - size_t CurrentWeight_ = 0; + i64 MaxWeight_ = 0; + i64 CurrentWeight_ = 0; TItemMap ItemMap_; TLruList LruList_; diff --git a/yt/yt/core/misc/unittests/lru_cache_ut.cpp b/yt/yt/core/misc/unittests/lru_cache_ut.cpp index da46d7a332..d6f43432f9 100644 --- a/yt/yt/core/misc/unittests/lru_cache_ut.cpp +++ b/yt/yt/core/misc/unittests/lru_cache_ut.cpp @@ -68,14 +68,14 @@ TEST(TSimpleLruCache, Clear) TEST(TMultiLruCache, InsertAndFind) { TMultiLruCache<TString, int> cache(3); - - EXPECT_EQ(cache.GetSize(), 0u); + + EXPECT_EQ(cache.GetSize(), 0); cache.Insert("a", 1); cache.Insert("b", 2); cache.Insert("a", 3); - EXPECT_EQ(cache.GetSize(), 3u); + EXPECT_EQ(cache.GetSize(), 3); ASSERT_TRUE(cache.Find("a")); ASSERT_TRUE(cache.Find("b")); @@ -88,7 +88,7 @@ TEST(TMultiLruCache, InsertAndFind) cache.Insert("b", 4); - EXPECT_EQ(cache.GetSize(), 3u); + EXPECT_EQ(cache.GetSize(), 3); ASSERT_TRUE(cache.Find("a")); ASSERT_TRUE(cache.Find("b")); EXPECT_FALSE(cache.Find("c")); @@ -102,42 +102,42 @@ TEST(TMultiLruCache, InsertAndFind) cache.Insert("c", 5); - EXPECT_EQ(cache.GetSize(), 3u); + EXPECT_EQ(cache.GetSize(), 3); EXPECT_TRUE(cache.Find("a")); EXPECT_TRUE(cache.Find("b")); EXPECT_TRUE(cache.Find("c")); cache.Clear(); - EXPECT_EQ(cache.GetSize(), 0u); + EXPECT_EQ(cache.GetSize(), 0); } TEST(TMultiLruCache, Extract) { TMultiLruCache<TString, int> cache(3); - + cache.Insert("a", 1); cache.Insert("b", 2); cache.Insert("a", 3); - EXPECT_FALSE(cache.Extract("c")); + EXPECT_FALSE(cache.TryExtract("c")); - EXPECT_EQ(*cache.Extract("a"), 1); - EXPECT_EQ(cache.GetSize(), 2u); + EXPECT_EQ(*cache.TryExtract("a"), 1); + EXPECT_EQ(cache.GetSize(), 2); cache.Insert("b", 4); - EXPECT_EQ(*cache.Extract("a"), 3); - EXPECT_EQ(cache.GetSize(), 2u); + EXPECT_EQ(*cache.TryExtract("a"), 3); + EXPECT_EQ(cache.GetSize(), 2); cache.Insert("c", 1); - + EXPECT_TRUE(cache.Find("b")); - EXPECT_EQ(*cache.Extract("b"), 4); - EXPECT_EQ(cache.GetSize(), 2u); + EXPECT_EQ(*cache.TryExtract("b"), 4); + EXPECT_EQ(cache.GetSize(), 2); EXPECT_TRUE(cache.Find("b")); - EXPECT_EQ(*cache.Extract("b"), 2); + EXPECT_EQ(*cache.TryExtract("b"), 2); - EXPECT_FALSE(cache.Extract("b")); + EXPECT_FALSE(cache.TryExtract("b")); } //////////////////////////////////////////////////////////////////////////////// |