forked from liuliu/ccv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccv_cache.c
459 lines (441 loc) · 12.4 KB
/
ccv_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "ccv.h"
#include "ccv_internal.h"
#define CCV_GET_CACHE_TYPE(x) ((x) >> 60)
#define CCV_GET_TERMINAL_AGE(x) (((x) >> 32) & 0x0FFFFFFF)
#define CCV_GET_TERMINAL_SIZE(x) ((x) & 0xFFFFFFFF)
#define CCV_SET_TERMINAL_TYPE(x, y, z) (((uint64_t)(x) << 60) | ((uint64_t)(y) << 32) | (z))
void ccv_cache_init(ccv_cache_t* cache, size_t up, int cache_types, ccv_cache_index_free_f ffree, ...)
{
cache->rnum = 0;
cache->age = 0;
cache->up = up;
cache->size = 0;
assert(cache_types > 0 && cache_types <= 16);
va_list arguments;
va_start(arguments, ffree);
int i;
cache->ffree[0] = ffree;
for (i = 1; i < cache_types; i++)
cache->ffree[i] = va_arg(arguments, ccv_cache_index_free_f);
va_end(arguments);
memset(&cache->origin, 0, sizeof(ccv_cache_index_t));
}
static int bits_in_16bits[0x1u << 16];
static int bits_in_16bits_init = 0;
static int sparse_bitcount(unsigned int n) {
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
}
static void precomputed_16bits() {
int i;
for (i = 0; i < (0x1u << 16); i++)
bits_in_16bits[i] = sparse_bitcount(i);
bits_in_16bits_init = 1;
}
static uint32_t compute_bits(uint64_t m) {
return (bits_in_16bits[m & 0xffff] + bits_in_16bits[(m >> 16) & 0xffff] +
bits_in_16bits[(m >> 32) & 0xffff] + bits_in_16bits[(m >> 48) & 0xffff]);
}
/* update age along a path in the radix tree */
static void _ccv_cache_aging(ccv_cache_index_t* branch, uint64_t sign)
{
if (!bits_in_16bits_init)
precomputed_16bits();
int i;
uint64_t j = 63;
ccv_cache_index_t* breadcrumb[10];
for (i = 0; i < 10; i++)
{
breadcrumb[i] = branch;
int leaf = branch->terminal.off & 0x1;
int full = branch->terminal.off & 0x2;
if (leaf)
{
break;
} else {
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
int dice = (sign & j) >> (i * 6);
if (full)
{
branch = set + dice;
} else {
uint64_t k = 1;
k = k << dice;
if (k & branch->branch.bitmap) {
uint64_t m = (k - 1) & branch->branch.bitmap;
branch = set + compute_bits(m);
} else {
break;
}
}
j <<= 6;
}
}
assert(i < 10);
for (; i >= 0; i--)
{
branch = breadcrumb[i];
int leaf = branch->terminal.off & 0x1;
if (!leaf)
{
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
uint32_t total = compute_bits(branch->branch.bitmap);
uint32_t min_age = (set[0].terminal.off & 0x1) ? CCV_GET_TERMINAL_AGE(set[0].terminal.type) : set[0].branch.age;
for (j = 1; j < total; j++)
{
uint32_t age = (set[j].terminal.off & 0x1) ? CCV_GET_TERMINAL_AGE(set[j].terminal.type) : set[j].branch.age;
if (age < min_age)
min_age = age;
}
branch->branch.age = min_age;
}
}
}
static ccv_cache_index_t* _ccv_cache_seek(ccv_cache_index_t* branch, uint64_t sign, int* depth)
{
if (!bits_in_16bits_init)
precomputed_16bits();
int i;
uint64_t j = 63;
for (i = 0; i < 10; i++)
{
int leaf = branch->terminal.off & 0x1;
int full = branch->terminal.off & 0x2;
if (leaf)
{
if (depth)
*depth = i;
return branch;
} else {
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
int dice = (sign & j) >> (i * 6);
if (full)
{
branch = set + dice;
} else {
uint64_t k = 1;
k = k << dice;
if (k & branch->branch.bitmap) {
uint64_t m = (k - 1) & branch->branch.bitmap;
branch = set + compute_bits(m);
} else {
if (depth)
*depth = i;
return branch;
}
}
j <<= 6;
}
}
return 0;
}
void* ccv_cache_get(ccv_cache_t* cache, uint64_t sign, uint8_t* type)
{
if (cache->rnum == 0)
return 0;
ccv_cache_index_t* branch = _ccv_cache_seek(&cache->origin, sign, 0);
if (!branch)
return 0;
int leaf = branch->terminal.off & 0x1;
if (!leaf)
return 0;
if (branch->terminal.sign != sign)
return 0;
if (type)
*type = CCV_GET_CACHE_TYPE(branch->terminal.type);
return (void*)(branch->terminal.off - (branch->terminal.off & 0x3));
}
// only call this function when the cache space is delpeted
static void _ccv_cache_lru(ccv_cache_t* cache)
{
ccv_cache_index_t* branch = &cache->origin;
int leaf = branch->terminal.off & 0x1;
if (leaf)
{
void* result = (void*)(branch->terminal.off - (branch->terminal.off & 0x3));
uint8_t type = CCV_GET_CACHE_TYPE(branch->terminal.type);
if (result != 0)
{
assert(type >= 0 && type < 16);
cache->ffree[type](result);
}
cache->rnum = 0;
cache->size = 0;
return;
}
uint32_t min_age = branch->branch.age;
int i, j;
for (i = 0; i < 10; i++)
{
ccv_cache_index_t* old_branch = branch;
int leaf = branch->terminal.off & 0x1;
if (leaf)
{
ccv_cache_delete(cache, branch->terminal.sign);
break;
} else {
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
uint32_t total = compute_bits(branch->branch.bitmap);
for (j = 0; j < total; j++)
{
uint32_t age = (set[j].terminal.off & 0x1) ? CCV_GET_TERMINAL_AGE(set[j].terminal.type) : set[j].branch.age;
assert(age >= min_age);
if (age == min_age)
{
branch = set + j;
break;
}
}
assert(old_branch != branch);
}
}
assert(i < 10);
}
static void _ccv_cache_depleted(ccv_cache_t* cache, size_t size)
{
while (cache->size > size)
_ccv_cache_lru(cache);
}
int ccv_cache_put(ccv_cache_t* cache, uint64_t sign, void* x, uint32_t size, uint8_t type)
{
assert(((uint64_t)x & 0x3) == 0);
if (size > cache->up)
return -1;
if (size + cache->size > cache->up)
_ccv_cache_depleted(cache, cache->up - size);
if (cache->rnum == 0)
{
cache->age = 1;
cache->origin.terminal.off = (uint64_t)x | 0x1;
cache->origin.terminal.sign = sign;
cache->origin.terminal.type = CCV_SET_TERMINAL_TYPE(type, cache->age, size);
cache->size = size;
cache->rnum = 1;
return 0;
}
++cache->age;
int i, depth = -1;
ccv_cache_index_t* branch = _ccv_cache_seek(&cache->origin, sign, &depth);
if (!branch)
return -1;
int leaf = branch->terminal.off & 0x1;
uint64_t on = 1;
assert(depth >= 0);
if (leaf)
{
if (sign == branch->terminal.sign)
{
cache->ffree[CCV_GET_CACHE_TYPE(branch->terminal.type)]((void*)(branch->terminal.off - (branch->terminal.off & 0x3)));
branch->terminal.off = (uint64_t)x | 0x1;
uint32_t old_size = CCV_GET_TERMINAL_SIZE(branch->terminal.type);
cache->size = cache->size + size - old_size;
branch->terminal.type = CCV_SET_TERMINAL_TYPE(type, cache->age, size);
_ccv_cache_aging(&cache->origin, sign);
return 1;
} else {
ccv_cache_index_t t = *branch;
uint32_t age = CCV_GET_TERMINAL_AGE(branch->terminal.type);
uint64_t j = 63;
j = j << (depth * 6);
int dice, udice;
assert(depth < 10);
for (i = depth; i < 10; i++)
{
dice = (t.terminal.sign & j) >> (i * 6);
udice = (sign & j) >> (i * 6);
if (dice == udice)
{
branch->branch.bitmap = on << dice;
ccv_cache_index_t* set = (ccv_cache_index_t*)ccmalloc(sizeof(ccv_cache_index_t));
assert(((uint64_t)set & 0x3) == 0);
branch->branch.set = (uint64_t)set;
branch->branch.age = age;
branch = set;
} else {
break;
}
j <<= 6;
}
branch->branch.bitmap = (on << dice) | (on << udice);
ccv_cache_index_t* set = (ccv_cache_index_t*)ccmalloc(sizeof(ccv_cache_index_t) * 2);
assert(((uint64_t)set & 0x3) == 0);
branch->branch.set = (uint64_t)set;
branch->branch.age = age;
int u = dice < udice;
set[u].terminal.sign = sign;
set[u].terminal.off = (uint64_t)x | 0x1;
set[u].terminal.type = CCV_SET_TERMINAL_TYPE(type, cache->age, size);
set[1 - u] = t;
}
} else {
uint64_t k = 1, j = 63;
k = k << ((sign & (j << (depth * 6))) >> (depth * 6));
uint64_t m = (k - 1) & branch->branch.bitmap;
uint32_t start = compute_bits(m);
uint32_t total = compute_bits(branch->branch.bitmap);
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
set = (ccv_cache_index_t*)ccrealloc(set, sizeof(ccv_cache_index_t) * (total + 1));
assert(((uint64_t)set & 0x3) == 0);
for (i = total; i > start; i--)
set[i] = set[i - 1];
set[start].terminal.off = (uint64_t)x | 0x1;
set[start].terminal.sign = sign;
set[start].terminal.type = CCV_SET_TERMINAL_TYPE(type, cache->age, size);
branch->branch.set = (uint64_t)set;
branch->branch.bitmap |= k;
if (total == 63)
branch->branch.set |= 0x2;
}
cache->rnum++;
cache->size += size;
return 0;
}
static void _ccv_cache_cleanup(ccv_cache_index_t* branch)
{
int leaf = branch->terminal.off & 0x1;
if (!leaf)
{
int i;
uint64_t total = compute_bits(branch->branch.bitmap);
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
for (i = 0; i < total; i++)
{
if (!(set[i].terminal.off & 0x1))
_ccv_cache_cleanup(set + i);
}
ccfree(set);
}
}
static void _ccv_cache_cleanup_and_free(ccv_cache_index_t* branch, ccv_cache_index_free_f ffree[])
{
int leaf = branch->terminal.off & 0x1;
if (!leaf)
{
int i;
uint64_t total = compute_bits(branch->branch.bitmap);
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
for (i = 0; i < total; i++)
_ccv_cache_cleanup_and_free(set + i, ffree);
ccfree(set);
} else {
assert(CCV_GET_CACHE_TYPE(branch->terminal.type) >= 0 && CCV_GET_CACHE_TYPE(branch->terminal.type) < 16);
ffree[CCV_GET_CACHE_TYPE(branch->terminal.type)]((void*)(branch->terminal.off - (branch->terminal.off & 0x3)));
}
}
void* ccv_cache_out(ccv_cache_t* cache, uint64_t sign, uint8_t* type)
{
if (!bits_in_16bits_init)
precomputed_16bits();
if (cache->rnum == 0)
return 0;
int i, found = 0, depth = -1;
ccv_cache_index_t* parent = 0;
ccv_cache_index_t* uncle = &cache->origin;
ccv_cache_index_t* branch = &cache->origin;
uint64_t j = 63;
for (i = 0; i < 10; i++)
{
int leaf = branch->terminal.off & 0x1;
int full = branch->terminal.off & 0x2;
if (leaf)
{
found = 1;
break;
}
if (parent != 0 && compute_bits(parent->branch.bitmap) > 1)
uncle = branch;
parent = branch;
depth = i;
ccv_cache_index_t* set = (ccv_cache_index_t*)(branch->branch.set - (branch->branch.set & 0x3));
int dice = (sign & j) >> (i * 6);
if (full)
{
branch = set + dice;
} else {
uint64_t k = 1;
k = k << dice;
if (k & branch->branch.bitmap)
{
uint64_t m = (k - 1) & branch->branch.bitmap;
branch = set + compute_bits(m);
} else {
return 0;
}
}
j <<= 6;
}
if (!found)
return 0;
int leaf = branch->terminal.off & 0x1;
if (!leaf)
return 0;
if (branch->terminal.sign != sign)
return 0;
void* result = (void*)(branch->terminal.off - (branch->terminal.off & 0x3));
if (type)
*type = CCV_GET_CACHE_TYPE(branch->terminal.type);
uint32_t size = CCV_GET_TERMINAL_SIZE(branch->terminal.type);
if (branch != &cache->origin)
{
uint64_t k = 1, j = 63;
int dice = (sign & (j << (depth * 6))) >> (depth * 6);
k = k << dice;
uint64_t m = (k - 1) & parent->branch.bitmap;
uint32_t start = compute_bits(m);
uint32_t total = compute_bits(parent->branch.bitmap);
assert(total > 1);
ccv_cache_index_t* set = (ccv_cache_index_t*)(parent->branch.set - (parent->branch.set & 0x3));
if (total > 2 || (total == 2 && !(set[1 - start].terminal.off & 0x1)))
{
parent->branch.bitmap &= ~k;
for (i = start + 1; i < total; i++)
set[i - 1] = set[i];
set = (ccv_cache_index_t*)ccrealloc(set, sizeof(ccv_cache_index_t) * (total - 1));
parent->branch.set = (uint64_t)set;
} else {
ccv_cache_index_t t = set[1 - start];
_ccv_cache_cleanup(uncle);
*uncle = t;
}
_ccv_cache_aging(&cache->origin, sign);
} else {
// if I only have one item, reset age to 1
cache->age = 1;
}
cache->rnum--;
cache->size -= size;
return result;
}
int ccv_cache_delete(ccv_cache_t* cache, uint64_t sign)
{
uint8_t type = 0;
void* result = ccv_cache_out(cache, sign, &type);
if (result != 0)
{
assert(type >= 0 && type < 16);
cache->ffree[type](result);
return 0;
}
return -1;
}
void ccv_cache_cleanup(ccv_cache_t* cache)
{
if (cache->rnum > 0)
{
_ccv_cache_cleanup_and_free(&cache->origin, cache->ffree);
cache->size = 0;
cache->age = 0;
cache->rnum = 0;
memset(&cache->origin, 0, sizeof(ccv_cache_index_t));
}
}
void ccv_cache_close(ccv_cache_t* cache)
{
// for radix-tree based cache, close/cleanup are the same (it is not the same for cuckoo based one,
// because for cuckoo based one, it will free up space in close whereas only cleanup space in cleanup
ccv_cache_cleanup(cache);
}