Skip to content

Commit

Permalink
[RocksDB perf] Cache speedup
Browse files Browse the repository at this point in the history
Summary:
I have ran a get benchmark where all the data is in the cache and observed that most of the time is spent on waiting for lock in LRUCache.

This is an effort to optimize LRUCache.

Test Plan:
The data was loaded with fillseq. Then, I ran a benchmark:

    /db_bench --db=/tmp/rocksdb_stat_bench --num=1000000 --benchmarks=readrandom --statistics=1 --use_existing_db=1 --threads=16 --disable_seek_compaction=1 --cache_size=20000000000 --cache_numshardbits=8 --table_cache_numshardbits=8

I ran the benchmark three times. Here are the results:
AFTER THE PATCH: 798072, 803998, 811807
BEFORE THE PATCH: 782008, 815593, 763017

Reviewers: dhruba, haobo, kailiu

Reviewed By: haobo

CC: leveldb

Differential Revision: https://reviews.facebook.net/D14571
  • Loading branch information
igorcanadi committed Dec 11, 2013
1 parent 5e4ab76 commit e8d40c3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions util/cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include <vector>

#include "rocksdb/cache.h"
#include "port/port.h"
Expand Down Expand Up @@ -111,8 +111,8 @@ class HandleTable {
}

void Resize() {
uint32_t new_length = 4;
while (new_length < elems_) {
uint32_t new_length = 16;
while (new_length < elems_ * 1.5) {
new_length *= 2;
}
LRUHandle** new_list = new LRUHandle*[new_length];
Expand Down Expand Up @@ -255,18 +255,20 @@ Cache::Handle* LRUCache::Insert(

LRUHandle* e = reinterpret_cast<LRUHandle*>(
malloc(sizeof(LRUHandle)-1 + key.size()));
std::list<LRUHandle*> last_reference_list;
std::vector<LRUHandle*> last_reference_list;
last_reference_list.reserve(1);

e->value = value;
e->deleter = deleter;
e->charge = charge;
e->key_length = key.size();
e->hash = hash;
e->refs = 2; // One from LRUCache, one for the returned handle
memcpy(e->key_data, key.data(), key.size());

{
MutexLock l(&mutex_);

e->value = value;
e->deleter = deleter;
e->charge = charge;
e->key_length = key.size();
e->hash = hash;
e->refs = 2; // One from LRUCache, one for the returned handle
memcpy(e->key_data, key.data(), key.size());
LRU_Append(e);

LRUHandle* old = table_.Insert(e);
Expand Down

0 comments on commit e8d40c3

Please sign in to comment.