This is an automated email from the ASF dual-hosted git repository.

alexey pushed a commit to branch branch-1.18.x
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 4967eb0ae5ca6153a2a158a4e369ba9f9d6f1e8a
Author: Mahesh Reddy <[email protected]>
AuthorDate: Tue Dec 10 15:35:22 2024 -0500

    KUDU-613: Specialize template Insert functions
    
    This patch refactors the Insert adjacent functions to
    use template specialization. Now only two functions exist,
    Insert() and ReInsert().
    
    This patch does not contain any functional modifications.
    
    Change-Id: I39d45fa96bb0a758d059a43ccc0dff47b0c3f472
    Reviewed-on: http://gerrit.cloudera.org:8080/22193
    Reviewed-by: Alexey Serbin <[email protected]>
    Tested-by: Alexey Serbin <[email protected]>
    (cherry picked from commit 0259a4dbbb19e72bbe7554cfc924d391bf886622)
    Reviewed-on: http://gerrit.cloudera.org:8080/22196
    Reviewed-by: Abhishek Chennaka <[email protected]>
    Tested-by: Abhishek Chennaka <[email protected]>
---
 src/kudu/util/slru_cache.cc | 46 ++++++++++++++++++++++++---------------------
 src/kudu/util/slru_cache.h  | 14 +++++---------
 2 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/src/kudu/util/slru_cache.cc b/src/kudu/util/slru_cache.cc
index 751d95404..0c97e8b61 100644
--- a/src/kudu/util/slru_cache.cc
+++ b/src/kudu/util/slru_cache.cc
@@ -248,6 +248,7 @@ void 
SLRUCacheShard<Segment::kProbationary>::RemoveEntriesPastCapacity() {
   }
 }
 
+// Inserts handle into the probationary shard and removes any entries past 
capacity.
 template<>
 Handle* SLRUCacheShard<Segment::kProbationary>::Insert(SLRUHandle* handle,
                                                        EvictionCallback* 
eviction_callback) {
@@ -277,26 +278,10 @@ Handle* 
SLRUCacheShard<Segment::kProbationary>::Insert(SLRUHandle* handle,
   return reinterpret_cast<Handle*>(handle);
 }
 
+// Inserts handle into the protected shard when updating entry in the 
protected segment.
 template<>
-void SLRUCacheShard<Segment::kProtected>::UpgradeInsert(SLRUHandle* handle) {
-  handle->in_protected_segment.store(true, std::memory_order_relaxed);
-  if (PREDICT_TRUE(metrics_)) {
-    metrics_->upgrades->Increment();
-    metrics_->protected_segment_cache_usage->IncrementBy(handle->charge);
-    metrics_->protected_segment_inserts->Increment();
-  }
-
-  handle->Sanitize();
-  RL_Append(handle);
-
-  // No entries should exist with same key in protected segment when upgrading.
-  SLRUHandle* old_entry = table_.Insert(handle);
-  DCHECK(old_entry == nullptr);
-}
-
-template<>
-Handle* SLRUCacheShard<Segment::kProtected>::UpdateInsert(SLRUHandle* handle,
-                                                          EvictionCallback* 
eviction_callback) {
+Handle* SLRUCacheShard<Segment::kProtected>::Insert(SLRUHandle* handle,
+                                                    EvictionCallback* 
eviction_callback) {
   handle->eviction_callback = eviction_callback;
   // Two refs for the handle: one from SLRUCacheShard, one for the returned 
handle.
   // Even though this function is for updates in the protected segment, it's 
treated similarly
@@ -324,6 +309,7 @@ Handle* 
SLRUCacheShard<Segment::kProtected>::UpdateInsert(SLRUHandle* handle,
   return reinterpret_cast<Handle*>(handle);
 }
 
+// Inserts handle into the probationary shard when downgrading entry from the 
protected segment.
 template<>
 void SLRUCacheShard<Segment::kProbationary>::ReInsert(SLRUHandle* handle) {
   handle->in_protected_segment.store(false, std::memory_order_relaxed);
@@ -341,6 +327,24 @@ void 
SLRUCacheShard<Segment::kProbationary>::ReInsert(SLRUHandle* handle) {
   RemoveEntriesPastCapacity();
 }
 
+// Inserts handle into the protected shard when upgrading entry from the 
probationary segment.
+template<>
+void SLRUCacheShard<Segment::kProtected>::ReInsert(SLRUHandle* handle) {
+  handle->in_protected_segment.store(true, std::memory_order_relaxed);
+  if (PREDICT_TRUE(metrics_)) {
+    metrics_->upgrades->Increment();
+    metrics_->protected_segment_cache_usage->IncrementBy(handle->charge);
+    metrics_->protected_segment_inserts->Increment();
+  }
+
+  handle->Sanitize();
+  RL_Append(handle);
+
+  // No entries should exist with same key in protected segment when upgrading.
+  SLRUHandle* old_entry = table_.Insert(handle);
+  DCHECK(old_entry == nullptr);
+}
+
 template<Segment segment>
 void SLRUCacheShard<segment>::Erase(const Slice& key, uint32_t hash) {
   SLRUHandle* e = table_.Remove(key, hash);
@@ -393,7 +397,7 @@ Handle* SLRUCacheShardPair::Insert(SLRUHandle* handle,
   if (!ProtectedContains(handle->key(), handle->hash)) {
     return probationary_shard_.Insert(handle, eviction_callback);
   }
-  auto* inserted = protected_shard_.UpdateInsert(handle, eviction_callback);
+  auto* inserted = protected_shard_.Insert(handle, eviction_callback);
   // If newly inserted entry has greater charge than previous one,
   // possible that entries can be evicted if at capacity.
   DowngradeEntries();
@@ -444,7 +448,7 @@ Handle* SLRUCacheShardPair::Lookup(const Slice& key, 
uint32_t hash, bool caching
   // probationary segment then adding entry to the protected segment.
   // Downgrade any entries in the protected segment if past capacity.
   probationary_shard_.SoftErase(key, hash);
-  protected_shard_.UpgradeInsert(val_handle);
+  protected_shard_.ReInsert(val_handle);
   DowngradeEntries();
 
   return probationary_handle;
diff --git a/src/kudu/util/slru_cache.h b/src/kudu/util/slru_cache.h
index 6d8613868..2cee6b0da 100644
--- a/src/kudu/util/slru_cache.h
+++ b/src/kudu/util/slru_cache.h
@@ -115,14 +115,12 @@ class SLRUCacheShard {
 
   void SetMetrics(SLRUCacheMetrics* metrics) { metrics_ = metrics; }
 
-  // Inserts handle into the probationary shard and removes any entries if 
past capacity.
-  // Returns the inserted handle.
+  // Inserts handle into the appropriate shard and returns the inserted handle.
+  // See comments on template specialization for each function for more 
details.
   Handle* Insert(SLRUHandle* handle, EvictionCallback* eviction_callback);
-  // Inserts handle into the protected shard when upgrading entry from the 
probationary segment.
-  void UpgradeInsert(SLRUHandle* handle);
-  // Inserts handle into the protected shard when updating entry in protected 
segment.
-  // Returns the inserted handle.
-  Handle* UpdateInsert(SLRUHandle* handle, EvictionCallback* 
eviction_callback);
+  // Inserts handle into the appropriate shard when upgrading or downgrading 
entry.
+  // See comments on template specialization for each function for more 
details.
+  void ReInsert(SLRUHandle* handle);
   // Like SLRUCache::Lookup, but with an extra "hash" parameter.
   Handle* Lookup(const Slice& key, uint32_t hash, bool caching);
   // Reduces the entry's ref by one, frees the entry if no refs are remaining.
@@ -134,8 +132,6 @@ class SLRUCacheShard {
   void SoftErase(const Slice& key, uint32_t hash);
   // Returns true if shard contains entry, false if not.
   bool Contains(const Slice& key, uint32_t hash);
-  // Inserts handle into the probationary shard when downgrading entry from 
the protected segment.
-  void ReInsert(SLRUHandle* handle);
   // Update the high-level metrics for a lookup operation.
   void UpdateMetricsLookup(bool was_hit, bool caching);
   // Update the segment-level metrics for a lookup operation.

Reply via email to