Skip to content

Commit

Permalink
4
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Dec 24, 2024
1 parent 31167a3 commit 309b2d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 48 deletions.
42 changes: 19 additions & 23 deletions be/src/olap/lru_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,30 +289,27 @@ void LRUCache::_lru_append(LRUHandle* list, LRUHandle* e) {
}

Cache::Handle* LRUCache::lookup(const CacheKey& key, uint32_t hash) {
LRUHandle* e;
{
std::lock_guard l(_mutex);
++_lookup_count;
e = _table.lookup(key, hash);
if (e != nullptr) {
// we get it from _table, so in_cache must be true
DCHECK(e->in_cache);
if (e->refs == 1) {
// only in LRU free list, remove it from list
_lru_remove(e);
}
e->refs++;
++_hit_count;
e->last_visit_time = UnixMillis();
} else {
++_miss_count;
std::lock_guard l(_mutex);
++_lookup_count;
LRUHandle* e = _table.lookup(key, hash);
if (e != nullptr) {
// we get it from _table, so in_cache must be true
DCHECK(e->in_cache);
if (e->refs == 1) {
// only in LRU free list, remove it from list
_lru_remove(e);
}
e->refs++;
++_hit_count;
e->last_visit_time = UnixMillis();
} else {
++_miss_count;
}

// If key not exist in cache, and is lru k cache, and key in visits list,
// then move the key to beginning of the visits list.
// key in visits list indicates that the key has been inserted once after the cache is full.
if (e == nullptr && _is_lru_k) {
std::lock_guard l(_visits_lru_cache_mutex);
auto it = _visits_lru_cache_map.find(hash);
if (it != _visits_lru_cache_map.end()) {
_visits_lru_cache_list.splice(_visits_lru_cache_list.begin(), _visits_lru_cache_list,
Expand Down Expand Up @@ -434,7 +431,6 @@ bool LRUCache::_check_element_count_limit() {
bool LRUCache::_lru_k_insert_visits_list(size_t total_size, visits_lru_cache_key visits_key) {
if (_usage + total_size > _capacity ||
_check_element_count_limit()) { // this line no lock required
std::lock_guard l(_visits_lru_cache_mutex);
auto it = _visits_lru_cache_map.find(visits_key);
if (it != _visits_lru_cache_map.end()) {
_visits_lru_cache_usage -= it->second->second;
Expand Down Expand Up @@ -482,14 +478,14 @@ Cache::Handle* LRUCache::insert(const CacheKey& key, uint32_t hash, void* value,
memcpy(e->key_data, key.data(), key.size());
e->last_visit_time = UnixMillis();

if (_is_lru_k && _lru_k_insert_visits_list(e->total_size, hash)) {
return reinterpret_cast<Cache::Handle*>(e);
}

LRUHandle* to_remove_head = nullptr;
{
std::lock_guard l(_mutex);

if (_is_lru_k && _lru_k_insert_visits_list(e->total_size, hash)) {
return reinterpret_cast<Cache::Handle*>(e);
}

// Free the space following strict LRU policy until enough space
// is freed or the lru list is empty
if (_cache_value_check_timestamp) {
Expand Down
25 changes: 0 additions & 25 deletions be/src/olap/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,6 @@

namespace doris {

#define OLAP_CACHE_STRING_TO_BUF(cur, str, r_len) \
do { \
if (r_len > str.size()) { \
memcpy(cur, str.c_str(), str.size()); \
r_len -= str.size(); \
cur += str.size(); \
} else { \
LOG(WARNING) << "construct cache key buf not enough."; \
return CacheKey(nullptr, 0); \
} \
} while (0)

#define OLAP_CACHE_NUMERIC_TO_BUF(cur, numeric, r_len) \
do { \
if (r_len > sizeof(numeric)) { \
memcpy(cur, &numeric, sizeof(numeric)); \
r_len -= sizeof(numeric); \
cur += sizeof(numeric); \
} else { \
LOG(WARNING) << "construct cache key buf not enough."; \
return CacheKey(nullptr, 0); \
} \
} while (0)

class Cache;
class LRUCachePolicy;
struct LRUHandle;
Expand Down Expand Up @@ -411,7 +387,6 @@ class LRUCache {
std::unordered_map<visits_lru_cache_key, std::list<visits_lru_cache_pair>::iterator>
_visits_lru_cache_map;
size_t _visits_lru_cache_usage = 0;
std::mutex _visits_lru_cache_mutex;
};

class ShardedLRUCache : public Cache {
Expand Down

0 comments on commit 309b2d8

Please sign in to comment.