summaryrefslogtreecommitdiff
path: root/src/redis.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-03-20 16:52:12 +0100
committerantirez <antirez@gmail.com>2014-03-20 16:52:12 +0100
commitc641b670c333486dc0ea887ae61c86ff9202ab69 (patch)
tree8f6a321bdd54da8faa827aadbb1647fc2eb29b55 /src/redis.c
parent82b53c650c41db4bd8af8d54cb75227df5b7abd4 (diff)
downloadredis-c641b670c333486dc0ea887ae61c86ff9202ab69.tar.gz
Use new dictGetRandomKeys() API to get samples for eviction.
The eviction quality degradates a bit in my tests, but since the API is faster, it allows to raise the number of samples, and overall is a win.
Diffstat (limited to 'src/redis.c')
-rw-r--r--src/redis.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/redis.c b/src/redis.c
index 29c605f72..c2a858fd3 100644
--- a/src/redis.c
+++ b/src/redis.c
@@ -2845,16 +2845,35 @@ struct evictionPoolEntry *evictionPoolAlloc(void) {
* We insert keys on place in ascending order, so keys with the smaller
* idle time are on the left, and keys with the higher idle time on the
* right. */
+
+#define EVICTION_SAMPLES_ARRAY_SIZE 16
void evictionPoolPopulate(dict *sampledict, dict *keydict, struct evictionPoolEntry *pool) {
- int j, k;
+ int j, k, count;
+ dictEntry *_samples[EVICTION_SAMPLES_ARRAY_SIZE];
+ dictEntry **samples;
+
+ /* Try to use a static buffer: this function is a big hit...
+ * Note: it was actually measured that this helps. */
+ if (server.maxmemory_samples <= EVICTION_SAMPLES_ARRAY_SIZE) {
+ samples = _samples;
+ } else {
+ samples = zmalloc(sizeof(samples[0])*server.maxmemory_samples);
+ }
+
+#if 1 /* Use bulk get by default. */
+ count = dictGetRandomKeys(sampledict,samples,server.maxmemory_samples);
+#else
+ count = server.maxmemory_samples;
+ for (j = 0; j < count; j++) samples[j] = dictGetRandomKey(sampledict);
+#endif
- for (j = 0; j < server.maxmemory_samples; j++) {
+ for (j = 0; j < count; j++) {
unsigned long long idle;
sds key;
robj *o;
dictEntry *de;
- de = dictGetRandomKey(sampledict);
+ de = samples[j];
key = dictGetKey(de);
/* If the dictionary we are sampling from is not the main
* dictionary (but the expires one) we need to lookup the key
@@ -2896,6 +2915,7 @@ void evictionPoolPopulate(dict *sampledict, dict *keydict, struct evictionPoolEn
pool[k].key = sdsdup(key);
pool[k].idle = idle;
}
+ if (samples != _samples) zfree(samples);
}
int freeMemoryIfNeeded(void) {