| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* cache.d - thread-local cache for a variety of operations |
| 6 |
* |
| 7 |
* Copyright (c) 2011 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <ecl/ecl.h> |
| 14 |
#include <ecl/cache.h> |
| 15 |
#include <ecl/internal.h> |
| 16 |
#include "newhash.h" |
| 17 |
|
| 18 |
#define RECORD_KEY(e) ((e)[0]) |
| 19 |
#define RECORD_VALUE(e) ((e)[1]) |
| 20 |
#define RECORD_GEN(e) ecl_fixnum((e+2)[0]) |
| 21 |
#define RECORD_GEN_SET(e,v) ((e+2)[0]=ecl_make_fixnum(v)) |
| 22 |
|
| 23 |
static void |
| 24 |
empty_cache(ecl_cache_ptr cache) |
| 25 |
{ |
| 26 |
cl_object table = cache->table; |
| 27 |
cl_index i, total_size = table->vector.dim; |
| 28 |
cache->generation = 0; |
| 29 |
for (i = 0; i < total_size; i+=3) { |
| 30 |
table->vector.self.t[i] = OBJNULL; |
| 31 |
table->vector.self.t[i+1] = OBJNULL; |
| 32 |
table->vector.self.fix[i+2] = 0; |
| 33 |
} |
| 34 |
#ifdef ECL_THREADS |
| 35 |
cache->clear_list = ECL_NIL; |
| 36 |
#endif |
| 37 |
} |
| 38 |
|
| 39 |
#ifndef ECL_THREADS |
| 40 |
static void |
| 41 |
clear_one_from_cache(ecl_cache_ptr cache, cl_object target) |
| 42 |
{ |
| 43 |
cl_object table = cache->table; |
| 44 |
cl_index i, total_size = table->vector.dim; |
| 45 |
for (i = 0; i < total_size; i+=3) { |
| 46 |
cl_object key = table->vector.self.t[i]; |
| 47 |
if (key != OBJNULL) { |
| 48 |
if (target == key->vector.self.t[0]) { |
| 49 |
table->vector.self.t[i] = OBJNULL; |
| 50 |
table->vector.self.fix[i+2] = 0; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
#else |
| 56 |
static void |
| 57 |
clear_list_from_cache(ecl_cache_ptr cache) |
| 58 |
{ |
| 59 |
cl_object list = ecl_atomic_get(&cache->clear_list); |
| 60 |
cl_object table = cache->table; |
| 61 |
cl_index i, total_size = table->vector.dim; |
| 62 |
for (i = 0; i < total_size; i+=3) { |
| 63 |
cl_object key = table->vector.self.t[i]; |
| 64 |
if (key != OBJNULL) { |
| 65 |
if (ecl_member_eq(key->vector.self.t[0], list)) { |
| 66 |
table->vector.self.t[i] = OBJNULL; |
| 67 |
table->vector.self.fix[i+2] = 0; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
ecl_cache_ptr |
| 75 |
ecl_make_cache(cl_index key_size, cl_index cache_size) |
| 76 |
{ |
| 77 |
ecl_cache_ptr cache = ecl_alloc(sizeof(struct ecl_cache)); |
| 78 |
cache->keys = |
| 79 |
si_make_vector(ECL_T, /* element type */ |
| 80 |
ecl_make_fixnum(key_size), /* Maximum size */ |
| 81 |
ECL_T, /* adjustable */ |
| 82 |
ecl_make_fixnum(0), /* fill pointer */ |
| 83 |
ECL_NIL, /* displaced */ |
| 84 |
ECL_NIL); |
| 85 |
cache->table = |
| 86 |
si_make_vector(ECL_T, /* element type */ |
| 87 |
ecl_make_fixnum(3*cache_size), /* Maximum size */ |
| 88 |
ECL_NIL, /* adjustable */ |
| 89 |
ECL_NIL, /* fill pointer */ |
| 90 |
ECL_NIL, /* displaced */ |
| 91 |
ECL_NIL); |
| 92 |
empty_cache(cache); |
| 93 |
return cache; |
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
ecl_cache_remove_one(ecl_cache_ptr cache, cl_object first_key) |
| 98 |
{ |
| 99 |
#ifdef ECL_THREADS |
| 100 |
ecl_atomic_push(&cache->clear_list, first_key); |
| 101 |
#else |
| 102 |
clear_one_from_cache(cache, first_key); |
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
static cl_index |
| 107 |
vector_hash_key(cl_object keys) |
| 108 |
{ |
| 109 |
cl_index c, n, a = GOLDEN_RATIO, b = GOLDEN_RATIO; |
| 110 |
for (c = 0, n = keys->vector.fillp; n >= 3; ) { |
| 111 |
c += keys->vector.self.index[--n]; |
| 112 |
b += keys->vector.self.index[--n]; |
| 113 |
a += keys->vector.self.index[--n]; |
| 114 |
mix(a, b, c); |
| 115 |
} |
| 116 |
switch (n) { |
| 117 |
case 2: b += keys->vector.self.index[--n]; |
| 118 |
case 1: a += keys->vector.self.index[--n]; |
| 119 |
c += keys->vector.dim; |
| 120 |
mix(a,b,c); |
| 121 |
} |
| 122 |
return c; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/* |
| 127 |
* variation of ecl_gethash from hash.d, which takes an array of objects as key |
| 128 |
* It also assumes that entries are never removed except by clrhash. |
| 129 |
* This method must be called with interrupts disabled! |
| 130 |
*/ |
| 131 |
|
| 132 |
ecl_cache_record_ptr |
| 133 |
ecl_search_cache(ecl_cache_ptr cache) |
| 134 |
{ |
| 135 |
#ifdef ECL_THREADS |
| 136 |
if (!Null(cache->clear_list)) { |
| 137 |
clear_list_from_cache(cache); |
| 138 |
} |
| 139 |
#endif |
| 140 |
{ |
| 141 |
cl_object table = cache->table; |
| 142 |
cl_object keys = cache->keys; |
| 143 |
cl_index argno = keys->vector.fillp; |
| 144 |
cl_index i = vector_hash_key(keys); |
| 145 |
cl_index total_size = table->vector.dim; |
| 146 |
cl_fixnum min_gen, gen; |
| 147 |
cl_object *min_e; |
| 148 |
int k; |
| 149 |
i = i % total_size; |
| 150 |
i = i - (i % 3); |
| 151 |
min_gen = cache->generation; |
| 152 |
min_e = 0; |
| 153 |
for (k = 20; k--; ) { |
| 154 |
cl_object *e = table->vector.self.t + i; |
| 155 |
cl_object hkey = RECORD_KEY(e); |
| 156 |
if (hkey == OBJNULL) { |
| 157 |
min_gen = -1; |
| 158 |
min_e = e; |
| 159 |
if (RECORD_VALUE(e) == OBJNULL) { |
| 160 |
/* This record is not only deleted but empty |
| 161 |
* Hence we cannot find our method ahead */ |
| 162 |
break; |
| 163 |
} |
| 164 |
/* Else we only know that the record has been |
| 165 |
* delete, but we might find our data ahead. */ |
| 166 |
} else if (argno == hkey->vector.fillp) { |
| 167 |
cl_index n; |
| 168 |
for (n = 0; n < argno; n++) { |
| 169 |
if (keys->vector.self.t[n] != |
| 170 |
hkey->vector.self.t[n]) |
| 171 |
goto NO_MATCH; |
| 172 |
} |
| 173 |
min_e = e; |
| 174 |
goto FOUND; |
| 175 |
} else if (min_gen >= 0) { |
| 176 |
NO_MATCH: |
| 177 |
/* Unless we have found a deleted record, keep |
| 178 |
* looking for the oldest record that we can |
| 179 |
* overwrite with the new data. */ |
| 180 |
gen = RECORD_GEN(e); |
| 181 |
if (gen < min_gen) { |
| 182 |
min_gen = gen; |
| 183 |
min_e = e; |
| 184 |
} |
| 185 |
} |
| 186 |
i += 3; |
| 187 |
if (i >= total_size) i = 0; |
| 188 |
} |
| 189 |
if (min_e == 0) { |
| 190 |
ecl_internal_error("search_method_hash"); |
| 191 |
} |
| 192 |
RECORD_KEY(min_e) = OBJNULL; |
| 193 |
cache->generation++; |
| 194 |
FOUND: |
| 195 |
/* |
| 196 |
* Once we have reached here, we set the new generation of |
| 197 |
* this record and perform a global shift so that the total |
| 198 |
* generation number does not become too large and we can |
| 199 |
* expire some elements. |
| 200 |
*/ |
| 201 |
gen = cache->generation; |
| 202 |
RECORD_GEN_SET(min_e, gen); |
| 203 |
if (gen >= total_size/2) { |
| 204 |
cl_object *e = table->vector.self.t; |
| 205 |
gen = 0.5*gen; |
| 206 |
cache->generation -= gen; |
| 207 |
for (i = table->vector.dim; i; i-= 3, e += 3) { |
| 208 |
cl_fixnum g = RECORD_GEN(e) - gen; |
| 209 |
if (g <= 0) { |
| 210 |
RECORD_KEY(e) = OBJNULL; |
| 211 |
RECORD_VALUE(e) = ECL_NIL; |
| 212 |
g = 0; |
| 213 |
} |
| 214 |
RECORD_GEN_SET(e, g); |
| 215 |
} |
| 216 |
} |
| 217 |
return (ecl_cache_record_ptr)min_e; |
| 218 |
} |
| 219 |
} |
| 220 |
|