Browse code

cache comparisons

aCaB authored on 2010/01/10 00:21:48
Showing 1 changed files
... ...
@@ -195,11 +195,11 @@ static int cacheset_init(struct cache_set *map, unsigned int entries) {
195 195
     map->version = 1337;
196 196
     return 0;
197 197
 }
198
+#endif /* USE_LRUHASHCACHE */
198 199
 
199
-#else
200 200
 #ifdef USE_SPLAY
201 201
 struct node {
202
-    uint64_t digest[2];
202
+    int64_t digest[2];
203 203
     struct node *left;
204 204
     struct node *right;
205 205
     uint32_t size; /* 0 is used to mark an empty hash slot! */
... ...
@@ -223,16 +223,22 @@ static int cacheset_init(struct cache_set *map, unsigned int entries) {
223 223
     return 0;
224 224
 }
225 225
 
226
-void splay(uint64_t *md5, struct cache_set *cs) {
227
-    struct node next = {{0, 0}, NULL, NULL, 0}, *right = &next, *left = &next, *temp, *root = cs->root;
226
+static inline int cmp(int64_t *a, int64_t *b) {
227
+    int64_t ret = a[1] - b[1];
228
+    if(!ret) ret = a[0] - b[0];
229
+    return ret;
230
+}
228 231
 
232
+void splay(int64_t *md5, struct cache_set *cs) {
233
+    struct node next = {{0, 0}, NULL, NULL, 0}, *right = &next, *left = &next, *temp, *root = cs->root;
229 234
     if(!root)
230 235
 	return;
231 236
 
232 237
     while(1) {
233
-	if(md5[1] < root->digest[1] || md5[1] == root->digest[1] && md5[0] < root->digest[0]) {
238
+	int comp = cmp(md5, root->digest);
239
+	if(comp < 0) {
234 240
 	    if(!root->left) break;
235
-	    if(md5[1] < root->left->digest[1] || md5[1] == root->left->digest[1] && md5[0] < root->left->digest[0]) {
241
+	    if(cmp(md5, root->left->digest) < 0) {
236 242
 		temp = root->left;
237 243
                 root->left = temp->right;
238 244
                 temp->right = root;
... ...
@@ -242,9 +248,9 @@ void splay(uint64_t *md5, struct cache_set *cs) {
242 242
             right->left = root;
243 243
             right = root;
244 244
             root = root->left;
245
-	} else if(md5[1] > root->digest[1] || md5[1] == root->digest[1] && md5[0] > root->digest[0]) {
245
+	} else if(comp > 0) {
246 246
 	    if(!root->right) break;
247
-	    if(md5[1] > root->right->digest[1] || md5[1] == root->right->digest[1] && md5[0] > root->right->digest[0]) {
247
+	    if(cmp(md5, root->right->digest) > 0) {
248 248
 		temp = root->right;
249 249
                 root->right = temp->left;
250 250
                 temp->left = root;
... ...
@@ -256,6 +262,8 @@ void splay(uint64_t *md5, struct cache_set *cs) {
256 256
             root = root->right;
257 257
 	} else break;
258 258
     }
259
+
260
+
259 261
     left->right = root->left;
260 262
     right->left = root->right;
261 263
     root->left = next.right;
... ...
@@ -265,28 +273,31 @@ void splay(uint64_t *md5, struct cache_set *cs) {
265 265
 
266 266
 
267 267
 static int cacheset_lookup(struct cache_set *cs, unsigned char *md5, size_t size) {
268
-    uint64_t hash[2];
268
+    int64_t hash[2];
269 269
 
270 270
     memcpy(hash, md5, 16);
271 271
     splay(hash, cs);
272
-    if(!cs->root || cs->root->digest[1] != hash[1] || cs->root->digest[0] != hash[0])
272
+    if(!cs->root || cmp(hash, cs->root->digest))
273 273
 	return 0;
274 274
     return 1337;
275 275
 }
276 276
 
277
-
278 277
 static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size) {
279
-    uint64_t hash[2];
280 278
     struct node *newnode;
279
+    int64_t hash[2];
280
+    int comp;
281 281
 
282 282
     memcpy(hash, md5, 16);
283 283
     splay(hash, cs);
284
-    if(cs->root && cs->root->digest[1] == hash[1] && cs->root->digest[0] == hash[0])
285
-	return; /* Already there */
284
+    if(cs->root) {
285
+	comp = cmp(hash, cs->root->digest);
286
+	if(!comp)
287
+	    return; /* Already there */
288
+    }
286 289
 
287 290
     if(cs->used == cs->total) {
288
-	/* FIXME: drop something */
289
-	cli_errmsg("FULL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
291
+	cli_errmsg("TREE IS FULL, BYE!\n");
292
+	abort();
290 293
 	return;
291 294
     } else {
292 295
 	newnode = &cs->data[cs->used++];
... ...
@@ -295,7 +306,7 @@ static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size)
295 295
     if(!cs->root) {
296 296
 	newnode->left = NULL;
297 297
 	newnode->right = NULL;
298
-    } else if(hash[1] < cs->root->digest[1] || hash[1] == cs->root->digest[1] && hash[0] < cs->root->digest[0]) {
298
+    } else if(comp < 0) {
299 299
 	newnode->left = cs->root->left;
300 300
 	newnode->right = cs->root;
301 301
 	cs->root->left = NULL;
... ...
@@ -308,11 +319,7 @@ static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size)
308 308
     newnode->digest[1] = hash[1];
309 309
     cs->root = newnode;
310 310
 }
311
-
312
-
313 311
 #endif /* USE_SPLAY */
314
-#endif /* USE_LRUHASHCACHE */
315
-
316 312
 
317 313
 #define TREES 256
318 314
 static inline unsigned int getkey(uint8_t *hash) { return *hash; }