Skip to content

Commit

Permalink
Merge pull request #578 from Barenboim/master
Browse files Browse the repository at this point in the history
remove mutex from LRUCache
  • Loading branch information
Barenboim authored Sep 28, 2021
2 parents fafe09a + efa2784 commit b1db18c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
28 changes: 9 additions & 19 deletions src/manager/DnsCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
Authors: Wu Jiaxu ([email protected])
Xie Han ([email protected])
*/

#include <stdint.h>
Expand All @@ -27,40 +28,30 @@

const DnsCache::DnsHandle *DnsCache::get_inner(const HostPort& host_port, int type)
{
int64_t cur_time = GET_CURRENT_SECOND;
std::lock_guard<std::mutex> lock(mutex_);
const DnsHandle *handle = cache_pool_.get(host_port);

if (handle)
{
int64_t cur_time = GET_CURRENT_SECOND;

switch (type)
{
case GET_TYPE_TTL:
if (cur_time > handle->value.expire_time)
{
std::lock_guard<std::mutex> lock(mutex_);

if (cur_time > handle->value.expire_time)
{
const_cast<DnsHandle *>(handle)->value.expire_time += TTL_INC;
cache_pool_.release(handle);
return NULL;
}
const_cast<DnsHandle *>(handle)->value.expire_time += TTL_INC;
cache_pool_.release(handle);
return NULL;
}

break;

case GET_TYPE_CONFIDENT:
if (cur_time > handle->value.confident_time)
{
std::lock_guard<std::mutex> lock(mutex_);

if (cur_time > handle->value.confident_time)
{
const_cast<DnsHandle *>(handle)->value.confident_time += CONFIDENT_INC;
cache_pool_.release(handle);
return NULL;
}
const_cast<DnsHandle *>(handle)->value.confident_time += CONFIDENT_INC;
cache_pool_.release(handle);
return NULL;
}

break;
Expand Down Expand Up @@ -96,7 +87,6 @@ const DnsCache::DnsHandle *DnsCache::put(const HostPort& host_port,
expire_time = cur_time + dns_ttl_default;

std::lock_guard<std::mutex> lock(mutex_);

return cache_pool_.put(host_port, {addrinfo, confident_time, expire_time});
}

4 changes: 4 additions & 0 deletions src/manager/DnsCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class DnsCache
// release handle by get/put
void release(DnsHandle *handle)
{
std::lock_guard<std::mutex> lock(mutex_);
cache_pool_.release(handle);
}

void release(const DnsHandle *handle)
{
std::lock_guard<std::mutex> lock(mutex_);
cache_pool_.release(handle);
}

Expand All @@ -65,6 +67,7 @@ class DnsCache
//Handle *get(const KEY &key);
const DnsHandle *get(const HostPort& host_port)
{
std::lock_guard<std::mutex> lock(mutex_);
return cache_pool_.get(host_port);
}

Expand Down Expand Up @@ -134,6 +137,7 @@ class DnsCache
// delete from cache, deleter delay called when all inuse-handle release.
void del(const HostPort& key)
{
std::lock_guard<std::mutex> lock(mutex_);
cache_pool_.del(key);
}

Expand Down
22 changes: 3 additions & 19 deletions src/util/LRUCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
limitations under the License.
Authors: Wu Jiaxu ([email protected])
Xie Han ([email protected]) -- remove mutex
*/

#ifndef _LRUCACHE_H_
#define _LRUCACHE_H_

#include <assert.h>
#include <map>
#include <mutex>
//#include <unordered_map>

/**
* @file LRUCache.h
Expand Down Expand Up @@ -101,8 +101,6 @@ typedef typename Map::const_iterator MapConstIterator;

~LRUCache()
{
std::lock_guard<std::mutex> lock(mutex_);

// Error if caller has an unreleased handle
assert(in_use_.next == &in_use_);
for (Handle *e = not_use_.next; e != &not_use_; )
Expand All @@ -121,8 +119,6 @@ typedef typename Map::const_iterator MapConstIterator;
// max_size means max cache number of key-value pairs
void set_max_size(size_t max_size)
{
std::lock_guard<std::mutex> lock(mutex_);

max_size_ = max_size;
}

Expand All @@ -132,8 +128,6 @@ typedef typename Map::const_iterator MapConstIterator;
// Remove all cache that are not actively in use.
void prune()
{
std::lock_guard<std::mutex> lock(mutex_);

while (not_use_.next != &not_use_)
{
Handle *e = not_use_.next;
Expand All @@ -147,12 +141,7 @@ typedef typename Map::const_iterator MapConstIterator;
// release handle by get/put
void release(Handle *handle)
{
if (handle)
{
std::lock_guard<std::mutex> lock(mutex_);

unref(handle);
}
unref(handle);
}

void release(const Handle *handle)
Expand All @@ -164,7 +153,6 @@ typedef typename Map::const_iterator MapConstIterator;
// Need call release when handle no longer needed
const Handle *get(const KEY& key)
{
std::lock_guard<std::mutex> lock(mutex_);
MapConstIterator it = cache_map_.find(key);

if (it != cache_map_.end())
Expand All @@ -183,8 +171,6 @@ typedef typename Map::const_iterator MapConstIterator;
Handle *e = new Handle(key, value);

e->ref = 1;
std::lock_guard<std::mutex> lock(mutex_);

size_++;
e->in_cache = true;
e->ref++;
Expand Down Expand Up @@ -216,7 +202,6 @@ typedef typename Map::const_iterator MapConstIterator;
// delete from cache, deleter delay called when all inuse-handle release.
void del(const KEY& key)
{
std::lock_guard<std::mutex> lock(mutex_);
MapConstIterator it = cache_map_.find(key);

if (it != cache_map_.end())
Expand Down Expand Up @@ -280,7 +265,6 @@ typedef typename Map::const_iterator MapConstIterator;
unref(e);
}

std::mutex mutex_;
size_t max_size_;
size_t size_;

Expand Down

1 comment on commit b1db18c

@chanchann
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.