diff options
author | robot-piglet <[email protected]> | 2025-05-01 01:47:41 +0300 |
---|---|---|
committer | robot-piglet <[email protected]> | 2025-05-01 01:58:27 +0300 |
commit | 8a6954f35eee99eef660e76e775774d720e111a9 (patch) | |
tree | 108307772882c3c8dec5addca321fd09f8225b4d | |
parent | 0764e86815875f548fecbfe1e23e12655ab05c8a (diff) |
Intermediate changes
commit_hash:d4e265c03467c90626a9303475ec66dae1d86fca
-rw-r--r-- | yt/yt/core/misc/async_slru_cache-inl.h | 256 | ||||
-rw-r--r-- | yt/yt/core/misc/async_slru_cache.h | 32 |
2 files changed, 150 insertions, 138 deletions
diff --git a/yt/yt/core/misc/async_slru_cache-inl.h b/yt/yt/core/misc/async_slru_cache-inl.h index 5520cbfb2b4..49247cae60d 100644 --- a/yt/yt/core/misc/async_slru_cache-inl.h +++ b/yt/yt/core/misc/async_slru_cache-inl.h @@ -31,13 +31,20 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::TItem::GetValueFuture() const -> //////////////////////////////////////////////////////////////////////////////// +template <class TKey, class TValue, class THash> +TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostItem::TGhostItem(TKey key) + : Key(std::move(key)) +{ } + +//////////////////////////////////////////////////////////////////////////////// + template <class TItem, class TDerived> void TAsyncSlruCacheListManager<TItem, TDerived>::PushToYounger(TItem* item, i64 weight) { YT_ASSERT(item->Empty()); - YoungerLruList.PushFront(item); + YoungerLruList_.PushFront(item); item->CachedWeight = weight; - YoungerWeightCounter += weight; + YoungerWeightCounter_ += weight; AsDerived()->OnYoungerUpdated(1, weight); item->Younger = true; } @@ -47,12 +54,12 @@ void TAsyncSlruCacheListManager<TItem, TDerived>::MoveToYounger(TItem* item) { YT_ASSERT(!item->Empty()); item->Unlink(); - YoungerLruList.PushFront(item); + YoungerLruList_.PushFront(item); if (!item->Younger) { i64 weight = item->CachedWeight; - OlderWeightCounter -= weight; + OlderWeightCounter_ -= weight; AsDerived()->OnOlderUpdated(-1, -weight); - YoungerWeightCounter += weight; + YoungerWeightCounter_ += weight; AsDerived()->OnYoungerUpdated(1, weight); item->Younger = true; } @@ -63,12 +70,12 @@ void TAsyncSlruCacheListManager<TItem, TDerived>::MoveToOlder(TItem* item) { YT_ASSERT(!item->Empty()); item->Unlink(); - OlderLruList.PushFront(item); + OlderLruList_.PushFront(item); if (item->Younger) { i64 weight = item->CachedWeight; - YoungerWeightCounter -= weight; + YoungerWeightCounter_ -= weight; AsDerived()->OnYoungerUpdated(-1, -weight); - OlderWeightCounter += weight; + OlderWeightCounter_ += weight; AsDerived()->OnOlderUpdated(1, weight); item->Younger = false; } @@ -81,14 +88,14 @@ void TAsyncSlruCacheListManager<TItem, TDerived>::PopFromLists(TItem* item) return; } - YT_VERIFY(TouchBufferPosition.load() == 0); + YT_VERIFY(TouchBufferPosition_.load() == 0); i64 weight = item->CachedWeight; if (item->Younger) { - YoungerWeightCounter -= weight; + YoungerWeightCounter_ -= weight; AsDerived()->OnYoungerUpdated(-1, -weight); } else { - OlderWeightCounter -= weight; + OlderWeightCounter_ -= weight; AsDerived()->OnOlderUpdated(-1, -weight); } item->Unlink(); @@ -99,10 +106,10 @@ void TAsyncSlruCacheListManager<TItem, TDerived>::UpdateWeight(TItem* item, i64 { YT_VERIFY(!item->Empty()); if (item->Younger) { - YoungerWeightCounter += weightDelta; + YoungerWeightCounter_ += weightDelta; AsDerived()->OnYoungerUpdated(0, weightDelta); } else { - OlderWeightCounter += weightDelta; + OlderWeightCounter_ += weightDelta; AsDerived()->OnOlderUpdated(0, weightDelta); } item->CachedWeight += weightDelta; @@ -112,7 +119,7 @@ template <class TItem, class TDerived> void TAsyncSlruCacheListManager<TItem, TDerived>::UpdateCookie(TItem* item, i64 countDelta, i64 weightDelta) { YT_VERIFY(item->Empty()); - CookieWeightCounter += weightDelta; + CookieWeightCounter_ += weightDelta; item->CachedWeight += weightDelta; AsDerived()->OnCookieUpdated(countDelta, weightDelta); } @@ -121,17 +128,17 @@ template <class TItem, class TDerived> TIntrusiveListWithAutoDelete<TItem, TDelete> TAsyncSlruCacheListManager<TItem, TDerived>::TrimNoDelete() { // Move from older to younger. - auto capacity = Capacity.load(); - auto youngerSizeFraction = YoungerSizeFraction.load(); - while (!OlderLruList.Empty() && OlderWeightCounter > capacity * (1 - youngerSizeFraction)) { - auto* item = &*(--OlderLruList.End()); + auto capacity = Capacity_.load(); + auto youngerSizeFraction = YoungerSizeFraction_.load(); + while (!OlderLruList_.Empty() && OlderWeightCounter_ > capacity * (1 - youngerSizeFraction)) { + auto* item = &*(--OlderLruList_.End()); MoveToYounger(item); } // Evict from younger. TIntrusiveListWithAutoDelete<TItem, TDelete> evictedItems; - while (!YoungerLruList.Empty() && static_cast<i64>(YoungerWeightCounter + OlderWeightCounter + CookieWeightCounter) > capacity) { - auto* item = &*(--YoungerLruList.End()); + while (!YoungerLruList_.Empty() && static_cast<i64>(YoungerWeightCounter_ + OlderWeightCounter_ + CookieWeightCounter_) > capacity) { + auto* item = &*(--YoungerLruList_.End()); PopFromLists(item); evictedItems.PushBack(item); } @@ -146,8 +153,8 @@ bool TAsyncSlruCacheListManager<TItem, TDerived>::TouchItem(TItem* item) return false; } - int capacity = std::ssize(TouchBuffer); - int index = TouchBufferPosition++; + int capacity = std::ssize(TouchBuffer_); + int index = TouchBufferPosition_++; if (index >= capacity) { // Drop touch request due to buffer overflow. // NB: We still return false since the other thread is already responsible for @@ -155,31 +162,31 @@ bool TAsyncSlruCacheListManager<TItem, TDerived>::TouchItem(TItem* item) return false; } - TouchBuffer[index] = item; + TouchBuffer_[index] = item; return index == capacity - 1; } template <class TItem, class TDerived> void TAsyncSlruCacheListManager<TItem, TDerived>::DrainTouchBuffer() { - int count = std::min<int>(TouchBufferPosition.load(), std::ssize(TouchBuffer)); + int count = std::min<int>(TouchBufferPosition_.load(), std::ssize(TouchBuffer_)); for (int index = 0; index < count; ++index) { - MoveToOlder(TouchBuffer[index]); + MoveToOlder(TouchBuffer_[index]); } - TouchBufferPosition = 0; + TouchBufferPosition_ = 0; } template <class TItem, class TDerived> void TAsyncSlruCacheListManager<TItem, TDerived>::Reconfigure(i64 capacity, double youngerSizeFraction) { - Capacity = capacity; - YoungerSizeFraction = youngerSizeFraction; + Capacity_.store(capacity); + YoungerSizeFraction_.store(youngerSizeFraction); } template <class TItem, class TDerived> void TAsyncSlruCacheListManager<TItem, TDerived>::SetTouchBufferCapacity(i64 touchBufferCapacity) { - TouchBuffer.resize(touchBufferCapacity); + TouchBuffer_.resize(touchBufferCapacity); } template <class TItem, class TDerived> @@ -211,7 +218,7 @@ TIntrusivePtr<typename TAsyncCacheValueBase<TKey, TValue, THash>::TCache> TAsync template <class TKey, class TValue, class THash> void TAsyncCacheValueBase<TKey, TValue, THash>::SetCache(TWeakPtr<TCache> cache) { - Cache_.Store(cache); + Cache_.Store(std::move(cache)); } template <class TKey, class TValue, class THash> @@ -429,7 +436,7 @@ TAsyncSlruCacheBase<TKey, TValue, THash>::GetAll() auto readerGuard = ReaderGuard(shard.SpinLock); for (const auto& [key, rawValue] : shard.ValueMap) { if (auto value = DangerousGetPtr<TValue>(rawValue)) { - result.push_back(value); + result.push_back(std::move(value)); } } } @@ -553,7 +560,7 @@ TAsyncSlruCacheBase<TKey, TValue, THash>::DoLookup(TShard* shard, const TKey& ke auto valueFuture = item->GetValueFuture(); - YT_VERIFY(itemMap.emplace(key, item).second); + EmplaceOrCrash(itemMap, key, item); ++Size_; i64 weight = GetWeight(item->Value); @@ -601,9 +608,9 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::BeginInsert(const TKey& key, i64 return TInsertCookie( key, - nullptr, + /*cache*/ nullptr, std::move(valueFuture), - false); + /*active*/ false); } while (true) { @@ -651,7 +658,7 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::BeginInsert(const TKey& key, i64 key, nullptr, std::move(valueFuture), - false); + /*active*/ false); } auto valueIt = valueMap.find(key); @@ -659,7 +666,7 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::BeginInsert(const TKey& key, i64 auto* item = new TItem(); auto valueFuture = item->GetValueFuture(); - YT_VERIFY(itemMap.emplace(key, item).second); + EmplaceOrCrash(itemMap, key, item); ++Size_; Counters_.MissedCounter.Increment(); @@ -674,9 +681,9 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::BeginInsert(const TKey& key, i64 auto insertCookie = TInsertCookie( key, - this, + /*cache*/ this, std::move(valueFuture), - true); + /*active*/ true); if (GhostCachesEnabled_.load()) { insertCookie.InsertedIntoSmallGhost_ = shard->SmallGhost.BeginInsert(key, cookieWeight); @@ -690,7 +697,7 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::BeginInsert(const TKey& key, i64 auto* item = new TItem(value); value->Item_ = item; - YT_VERIFY(itemMap.emplace(key, item).second); + EmplaceOrCrash(itemMap, key, item); ++Size_; i64 weight = GetWeight(item->Value); @@ -710,9 +717,9 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::BeginInsert(const TKey& key, i64 return TInsertCookie( key, - nullptr, + /*cache*/ nullptr, MakeFuture(value), - false); + /*active*/ false); } // Back off. @@ -727,7 +734,7 @@ template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::UpdateCookieWeight(const TInsertCookie& insertCookie, i64 newWeight) { YT_VERIFY(newWeight >= 0); - auto key = insertCookie.GetKey(); + const auto& key = insertCookie.GetKey(); auto* shard = GetShardByKey(key); @@ -765,7 +772,7 @@ template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::EndInsert(const TInsertCookie& insertCookie, TValuePtr value) { YT_VERIFY(value); - auto key = value->GetKey(); + const auto& key = value->GetKey(); auto* shard = GetShardByKey(key); @@ -780,7 +787,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::EndInsert(const TInsertCookie& in value->Item_ = item; auto promise = item->ValuePromise; - YT_VERIFY(shard->ValueMap.emplace(key, value.Get()).second); + EmplaceOrCrash(shard->ValueMap, key, value.Get()); auto cookieWeight = item->CachedWeight; shard->UpdateCookie(item, /*countDelta*/ -1, -cookieWeight); @@ -828,11 +835,9 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::CancelInsert(const TInsertCookie& shard->DrainTouchBuffer(); auto& itemMap = shard->ItemMap; - auto itemIt = itemMap.find(key); - YT_VERIFY(itemIt != itemMap.end()); + auto itemIt = GetIteratorOrCrash(itemMap, key); auto* item = itemIt->second; - auto promise = item->ValuePromise; itemMap.erase(itemIt); --Size_; @@ -842,6 +847,8 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::CancelInsert(const TInsertCookie& auto cookieWeight = item->CachedWeight; shard->UpdateCookie(item, /*countDelta*/ -1, -cookieWeight); + auto promise = std::move(item->ValuePromise); + delete item; guard.Release(); @@ -870,7 +877,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::Unregister(const TKey& key) template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TryRemove(const TKey& key, bool forbidResurrection) { - DoTryRemove(key, nullptr, forbidResurrection); + DoTryRemove(key, /*value*/ nullptr, forbidResurrection); } template <class TKey, class TValue, class THash> @@ -919,8 +926,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::DoTryRemove( } auto* item = itemIt->second; - auto actualValue = item->Value; - if (!actualValue) { + if (!item->Value) { return; } @@ -929,16 +935,45 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::DoTryRemove( shard->PopFromLists(item); - YT_VERIFY(actualValue->Item_ == item); - actualValue->Item_ = nullptr; + YT_VERIFY(item->Value->Item_ == item); + item->Value->Item_ = nullptr; - delete item; - - OnRemoved(actualValue); + OnRemoved(item->Value); // It is necessary to remove the guard before the actual value is destroyed. // Otherwise, it will lead to a deadlock in unregister. guard.Release(); + + delete item; +} + +template <class TKey, class TValue, class THash> +std::vector<typename TAsyncSlruCacheBase<TKey, TValue, THash>::TValuePtr> +TAsyncSlruCacheBase<TKey, TValue, THash>::TrimWithNotify( + TShard* shard, + NThreading::TWriterGuard<NThreading::TReaderWriterSpinLock>& guard, + const TValuePtr& insertedValue, + i64 weightDelta) +{ + YT_ASSERT_WRITER_SPINLOCK_AFFINITY(shard->SpinLock); + + auto evictedItems = shard->TrimNoDelete(); + auto evictedValues = shard->Trim(std::move(evictedItems)); + + if (weightDelta != 0) { + OnWeightUpdated(weightDelta); + } + if (insertedValue) { + OnAdded(insertedValue); + } + for (const auto& value : evictedValues) { + OnRemoved(value); + } + + // NB. Evicted items must die outside of critical section. + guard.Release(); + + return evictedValues; } template <class TKey, class TValue, class THash> @@ -1032,7 +1067,7 @@ auto TAsyncSlruCacheBase<TKey, TValue, THash>::GetLargeGhostCounters() const -> template <class TKey, class TValue, class THash> bool TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::DoLookup(const TKey& key, bool allowAsyncHits) { - auto readerGuard = ReaderGuard(SpinLock); + auto readerGuard = ReaderGuard(SpinLock_); auto itemIt = ItemMap_.find(key); if (itemIt == ItemMap_.end()) { @@ -1057,7 +1092,7 @@ bool TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::DoLookup(const TKey& readerGuard.Release(); if (needToDrain) { - auto writerGuard = WriterGuard(SpinLock); + auto writerGuard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); } @@ -1067,7 +1102,7 @@ bool TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::DoLookup(const TKey& template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Find(const TKey& key) { - if (!DoLookup(key, false)) { + if (!DoLookup(key, /*allowAsyncHits*/ false)) { Counters_->MissedCounter.Increment(); } } @@ -1075,7 +1110,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Find(const TKey& key template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Lookup(const TKey& key) { - if (!DoLookup(key, true)) { + if (!DoLookup(key, /*allowAsyncHits*/ true)) { Counters_->MissedCounter.Increment(); } } @@ -1083,7 +1118,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Lookup(const TKey& k template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Touch(const TValuePtr& value) { - auto readerGuard = ReaderGuard(SpinLock); + auto readerGuard = ReaderGuard(SpinLock_); if (!value) { return; @@ -1100,7 +1135,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Touch(const TValuePt readerGuard.Release(); if (needToDrain) { - auto writerGuard = WriterGuard(SpinLock); + auto writerGuard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); } } @@ -1112,7 +1147,7 @@ bool TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::BeginInsert(const TK return false; } - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); @@ -1134,7 +1169,7 @@ bool TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::BeginInsert(const TK auto* item = new TGhostItem(key); Counters_->MissedCounter.Increment(); - YT_VERIFY(ItemMap_.emplace(key, item).second); + EmplaceOrCrash(ItemMap_, key, item); this->UpdateCookie(item, /*countDelta*/ 1, cookieWeight); if (cookieWeight > 0) { @@ -1148,12 +1183,11 @@ bool TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::BeginInsert(const TK template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::CancelInsert(const TKey& key) { - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); - auto itemIt = ItemMap_.find(key); - YT_VERIFY(itemIt != ItemMap_.end()); + auto itemIt = GetIteratorOrCrash(ItemMap_, key); auto* item = itemIt->second; YT_VERIFY(!item->Inserted); @@ -1162,16 +1196,18 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::CancelInsert(const T this->UpdateCookie(item, /*countDelta*/ -1, -item->CachedWeight); + guard.Release(); + delete item; } template <class TKey, class TValue, class THash> -void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::EndInsert(const TValuePtr& value, i64 weight) +void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::EndInsert(TValuePtr value, i64 weight) { YT_VERIFY(value); - auto key = value->GetKey(); + const auto& key = value->GetKey(); - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); @@ -1180,7 +1216,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::EndInsert(const TVal YT_VERIFY(!item->Inserted); this->UpdateCookie(item, /*countDelta*/ -1, -item->CachedWeight); - item->Value = value; + item->Value = std::move(value); item->Inserted = true; this->PushToYounger(item, weight); @@ -1196,9 +1232,9 @@ template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Resurrect(const TValuePtr& value, i64 weight) { YT_VERIFY(value); - auto key = value->GetKey(); + const auto& key = value->GetKey(); - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); @@ -1211,7 +1247,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Resurrect(const TVal item->Value = value; item->Inserted = true; - YT_VERIFY(ItemMap_.emplace(key, item).second); + EmplaceOrCrash(ItemMap_, key, item); this->PushToYounger(item, weight); @@ -1225,7 +1261,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Resurrect(const TVal template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::TryRemove(const TKey& key, const TValuePtr& value) { - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); @@ -1238,28 +1274,32 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::TryRemove(const TKey if (!item->Inserted) { return; } - auto actualValue = item->Value.Lock(); - // If value is null, it means that we don't care about the removed value and remove just by key. - // If actualValue is null, then it refers to the value removed from the main cache, and always - // doesn't match our provided value. Otherwise, just compare the values. Note that the condition - // can be simplified just to (value && value != actualValue), but is retained as-is to make the - // intention more clear. - if (value && (!actualValue || value != actualValue)) { - return; + + { + auto actualValue = item->Value.Lock(); + // If value is null, it means that we don't care about the removed value and remove just by key. + // If actualValue is null, then it refers to the value removed from the main cache, and always + // doesn't match our provided value. Otherwise, just compare the values. Note that the condition + // can be simplified just to (value && value != actualValue), but is retained as-is to make the + // intention more clear. + if (value && (!actualValue || value != actualValue)) { + return; + } } - actualValue.Reset(); ItemMap_.erase(itemIt); this->PopFromLists(item); + guard.Release(); + delete item; } template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::UpdateWeight(const TKey& key, i64 newWeight) { - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); this->DrainTouchBuffer(); @@ -1290,7 +1330,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::UpdateWeight(const T template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::UpdateCookieWeight(const TKey& key, i64 newWeight) { - auto guard = WriterGuard(SpinLock); + auto guard = WriterGuard(SpinLock_); auto itemIt = ItemMap_.find(key); if (itemIt == ItemMap_.end()) { @@ -1314,7 +1354,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::UpdateCookieWeight(c template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Reconfigure(i64 capacity, double youngerSizeFraction) { - auto writerGuard = WriterGuard(SpinLock); + auto writerGuard = WriterGuard(SpinLock_); TAsyncSlruCacheListManager<TGhostItem, TGhostShard>::Reconfigure(capacity, youngerSizeFraction); this->DrainTouchBuffer(); Trim(writerGuard); @@ -1325,7 +1365,7 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Trim(NThreading::TWr { auto evictedItems = this->TrimNoDelete(); for (const auto& item : evictedItems) { - YT_VERIFY(ItemMap_.erase(item.Key) == 1); + EraseOrCrash(ItemMap_, item.Key); } // NB. Evicted items must die outside of critical section. @@ -1336,13 +1376,15 @@ void TAsyncSlruCacheBase<TKey, TValue, THash>::TGhostShard::Trim(NThreading::TWr template <class TKey, class TValue, class THash> std::vector<typename TAsyncSlruCacheBase<TKey, TValue, THash>::TValuePtr> -TAsyncSlruCacheBase<TKey, TValue, THash>::TShard::Trim(const TIntrusiveListWithAutoDelete<TItem, TDelete>& evictedItems) +TAsyncSlruCacheBase<TKey, TValue, THash>::TShard::Trim(TIntrusiveListWithAutoDelete<TItem, TDelete>&& evictedItems) { Parent->Size_ -= static_cast<int>(evictedItems.Size()); std::vector<TValuePtr> evictedValues; - for (const auto& item : evictedItems) { - auto value = item.Value; + evictedValues.reserve(evictedItems.Size()); + + for (auto& item : evictedItems) { + auto& value = item.Value; EraseOrCrash(ItemMap, value->GetKey()); @@ -1361,35 +1403,6 @@ TAsyncSlruCacheBase<TKey, TValue, THash>::TShard::Trim(const TIntrusiveListWithA } template <class TKey, class TValue, class THash> -std::vector<typename TAsyncSlruCacheBase<TKey, TValue, THash>::TValuePtr> -TAsyncSlruCacheBase<TKey, TValue, THash>::TrimWithNotify( - TShard* shard, - NThreading::TWriterGuard<NThreading::TReaderWriterSpinLock>& guard, - const TValuePtr& insertedValue, - i64 weightDelta) -{ - YT_ASSERT_SPINLOCK_AFFINITY(shard->SpinLock); - - auto evictedItems = shard->TrimNoDelete(); - auto evictedValues = shard->Trim(evictedItems); - - if (weightDelta != 0) { - OnWeightUpdated(weightDelta); - } - if (insertedValue) { - OnAdded(insertedValue); - } - for (const auto& value : evictedValues) { - OnRemoved(value); - } - - // NB. Evicted items must die outside of critical section. - guard.Release(); - - return evictedValues; -} - -template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TShard::OnYoungerUpdated(i64 deltaCount, i64 deltaWeight) { Parent->YoungerSizeCounter_ += deltaCount; @@ -1434,7 +1447,8 @@ TAsyncSlruCacheBase<TKey, TValue, THash>::TInsertCookie::~TInsertCookie() } template <class TKey, class TValue, class THash> -typename TAsyncSlruCacheBase<TKey, TValue, THash>::TInsertCookie& TAsyncSlruCacheBase<TKey, TValue, THash>::TInsertCookie::operator =(TInsertCookie&& other) +typename TAsyncSlruCacheBase<TKey, TValue, THash>::TInsertCookie& +TAsyncSlruCacheBase<TKey, TValue, THash>::TInsertCookie::operator = (TInsertCookie&& other) { if (this != &other) { Abort(); @@ -1488,7 +1502,7 @@ template <class TKey, class TValue, class THash> void TAsyncSlruCacheBase<TKey, TValue, THash>::TInsertCookie::EndInsert(TValuePtr value) { if (Active_.exchange(false)) { - Cache_->EndInsert(*this, value); + Cache_->EndInsert(*this, std::move(value)); } } diff --git a/yt/yt/core/misc/async_slru_cache.h b/yt/yt/core/misc/async_slru_cache.h index ac95f4259d3..889d063d201 100644 --- a/yt/yt/core/misc/async_slru_cache.h +++ b/yt/yt/core/misc/async_slru_cache.h @@ -117,18 +117,18 @@ protected: void OnCookieUpdated(i64 deltaCount, i64 deltaWeight); private: - TIntrusiveListWithAutoDelete<TItem, TDelete> YoungerLruList; - TIntrusiveListWithAutoDelete<TItem, TDelete> OlderLruList; + TIntrusiveListWithAutoDelete<TItem, TDelete> YoungerLruList_; + TIntrusiveListWithAutoDelete<TItem, TDelete> OlderLruList_; - std::vector<TItem*> TouchBuffer; - std::atomic<int> TouchBufferPosition = 0; + std::vector<TItem*> TouchBuffer_; + std::atomic<int> TouchBufferPosition_ = 0; - i64 YoungerWeightCounter = 0; - i64 OlderWeightCounter = 0; - i64 CookieWeightCounter = 0; + i64 YoungerWeightCounter_ = 0; + i64 OlderWeightCounter_ = 0; + i64 CookieWeightCounter_ = 0; - std::atomic<i64> Capacity; - std::atomic<double> YoungerSizeFraction; + std::atomic<i64> Capacity_; + std::atomic<double> YoungerSizeFraction_; }; //////////////////////////////////////////////////////////////////////////////// @@ -201,8 +201,8 @@ public: // NB: Shards store reference to the cache, so the cache cannot be simply copied or moved. TAsyncSlruCacheBase(const TAsyncSlruCacheBase&) = delete; TAsyncSlruCacheBase(TAsyncSlruCacheBase&&) = delete; - TAsyncSlruCacheBase& operator=(const TAsyncSlruCacheBase&) = delete; - TAsyncSlruCacheBase& operator=(TAsyncSlruCacheBase&&) = delete; + TAsyncSlruCacheBase& operator = (const TAsyncSlruCacheBase&) = delete; + TAsyncSlruCacheBase& operator = (TAsyncSlruCacheBase&&) = delete; int GetSize() const; i64 GetCapacity() const; @@ -298,9 +298,7 @@ private: struct TGhostItem : public TIntrusiveListItem<TGhostItem> { - explicit TGhostItem(TKey key) - : Key(std::move(key)) - { } + explicit TGhostItem(TKey key); TKey Key; //! The value associated with this item. If Inserted == true and Value is null, then we refer to some @@ -348,7 +346,7 @@ private: //! called with the same key. Do not call CancelInsert() or EndInsert() without matching BeginInsert(). bool BeginInsert(const TKey& key, i64 cookieWeight); void CancelInsert(const TKey& key); - void EndInsert(const TValuePtr& value, i64 weight); + void EndInsert(TValuePtr value, i64 weight); //! Inserts the value back to the cache immediately. Called when the value is resurected in the //! main cache. @@ -371,7 +369,7 @@ private: private: friend class TAsyncSlruCacheListManager<TGhostItem, TGhostShard>; - YT_DECLARE_SPIN_LOCK(NThreading::TReaderWriterSpinLock, SpinLock); + YT_DECLARE_SPIN_LOCK(NThreading::TReaderWriterSpinLock, SpinLock_); THashMap<TKey, TGhostItem*, THash> ItemMap_; @@ -421,7 +419,7 @@ private: TGhostShard LargeGhost; //! Returns the list of evicted items. - std::vector<TValuePtr> Trim(const TIntrusiveListWithAutoDelete<TItem, TDelete>& evictedItems); + std::vector<TValuePtr> Trim(TIntrusiveListWithAutoDelete<TItem, TDelete>&& evictedItems); protected: void OnYoungerUpdated(i64 deltaCount, i64 deltaWeight); |