-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix side effect of Remove operation.
In the original ARC paper, there was no explanation of how Remove should be handled when the cache is full. The Remove operation would have a side effect on subsequent Put operations when the cache is not full, which is still evicting items even though it's clear that there is no need to evict when the cache is not full. Now, code has been added to check if the cache is full to avoid performance degradation. Also fix c_, it should be max_count. Also add unittest.
- Loading branch information
Showing
2 changed files
with
182 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (c) 2020 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: curve | ||
* Created Date: 20211010 | ||
* Author: xuchaojie,lixiaocui | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include <glog/logging.h> | ||
#include <cstdint> | ||
|
||
#include "src/common/lru_cache.h" | ||
|
||
namespace curve { | ||
namespace common { | ||
|
||
TEST(ArcTest, test_cache_create) { | ||
int maxCount = 5; | ||
auto cache = std::make_shared<ARCCache<int, int>>(maxCount, | ||
std::make_shared<CacheMetrics>("Cache")); | ||
|
||
ASSERT_TRUE(cache->Capacity() == 5); | ||
ASSERT_TRUE(cache->Size() == 0); | ||
} | ||
|
||
TEST(ArcTest, test_cache_put) { | ||
int maxCount = 5; | ||
auto cache = std::make_shared<ARCCache<int, int>>(maxCount, | ||
std::make_shared<CacheMetrics>("Cache")); | ||
|
||
for (int i = 0; i < maxCount; ++i) { | ||
cache->Put(i, i); | ||
} | ||
|
||
ASSERT_TRUE(cache->Size() == maxCount); | ||
|
||
int v; | ||
for (int i = 0; i < maxCount; ++i) { | ||
ASSERT_TRUE(cache->Get(i, &v)); | ||
ASSERT_EQ(v, i); | ||
} | ||
} | ||
|
||
TEST(ArcTest, test_cache_retire) { | ||
int maxCount = 5; | ||
auto cache = std::make_shared<ARCCache<int, int>>(maxCount, | ||
std::make_shared<CacheMetrics>("Cache")); | ||
|
||
for (int i = 0; i < maxCount+1; ++i) { | ||
cache->Put(i, i); | ||
} | ||
|
||
ASSERT_TRUE(cache->Size() == maxCount); | ||
|
||
int v; | ||
ASSERT_TRUE(cache->Get(0, &v) == false); | ||
for (int i = 1; i < maxCount+1; ++i) { | ||
ASSERT_TRUE(cache->Get(i, &v)); | ||
ASSERT_EQ(v, i); | ||
} | ||
|
||
for (int i = 100; i < 200; ++i) { | ||
cache->Put(i, i); | ||
} | ||
|
||
auto s = cache->ArcSize(); | ||
ASSERT_TRUE(s.first + s.second <= 2 * maxCount); | ||
} | ||
|
||
TEST(ArcTest, test_cache_remove) { | ||
int maxCount = 5; | ||
auto cache = std::make_shared<ARCCache<int, int>>(maxCount, | ||
std::make_shared<CacheMetrics>("Cache")); | ||
|
||
for (int i = 0; i < maxCount; ++i) { | ||
cache->Put(i, i); | ||
} | ||
|
||
cache->Remove(0); | ||
int v; | ||
ASSERT_FALSE(cache->Get(0, &v)); | ||
ASSERT_TRUE(cache->Size() == maxCount-1); | ||
|
||
for (int i = 1; i < maxCount; ++i) { | ||
ASSERT_TRUE(cache->Get(i, &v)); | ||
ASSERT_EQ(v, i); | ||
} | ||
|
||
for (int i = 100; i < 200; ++i) { | ||
cache->Put(i, i); | ||
} | ||
|
||
auto s = cache->ArcSize(); | ||
ASSERT_TRUE(s.first + s.second <= 2 * maxCount); | ||
} | ||
|
||
} // namespace common | ||
} // namespace curve | ||
|