Browse code

micro oprimization

aCaB authored on 2010/01/11 21:10:36
Showing 1 changed files
... ...
@@ -35,8 +35,9 @@
35 35
 
36 36
 static mpool_t *mempool = NULL;
37 37
 
38
-#define USE_LRUHASHCACHE
39
-//#define USE_SPLAY
38
+//#define DONT_CACHE
39
+//#define USE_LRUHASHCACHE
40
+#define USE_SPLAY
40 41
 
41 42
 #ifdef USE_LRUHASHCACHE
42 43
 struct cache_key {
... ...
@@ -203,6 +204,7 @@ struct node {
203 203
     int64_t digest[2];
204 204
     struct node *left;
205 205
     struct node *right;
206
+    struct node *up;
206 207
     uint32_t size;
207 208
 };
208 209
 
... ...
@@ -230,10 +232,12 @@ static inline int cmp(int64_t *a, int64_t *b) {
230 230
     return ret;
231 231
 }
232 232
 
233
-void splay(int64_t *md5, struct cache_set *cs) {
233
+static int splay(int64_t *md5, struct cache_set *cs) {
234 234
     struct node next = {{0, 0}, NULL, NULL, 0}, *right = &next, *left = &next, *temp, *root = cs->root;
235
+    int ret = 0;
236
+
235 237
     if(!root)
236
-	return;
238
+	return 0;
237 239
 
238 240
     while(1) {
239 241
 	int comp = cmp(md5, root->digest);
... ...
@@ -244,7 +248,7 @@ void splay(int64_t *md5, struct cache_set *cs) {
244 244
                 root->left = temp->right;
245 245
                 temp->right = root;
246 246
                 root = temp;
247
-                if (!root->left) break;
247
+                if(!root->left) break;
248 248
 	    }
249 249
             right->left = root;
250 250
             right = root;
... ...
@@ -261,15 +265,18 @@ void splay(int64_t *md5, struct cache_set *cs) {
261 261
 	    left->right = root;
262 262
             left = root;
263 263
             root = root->right;
264
-	} else break;
264
+	} else {
265
+	    ret = 1;
266
+	    break;
267
+	}
265 268
     }
266 269
 
267
-
268 270
     left->right = root->left;
269 271
     right->left = root->right;
270 272
     root->left = next.right;
271 273
     root->right = next.left;
272 274
     cs->root = root;
275
+    return ret;
273 276
 }
274 277
 
275 278
 
... ...
@@ -277,10 +284,7 @@ static int cacheset_lookup(struct cache_set *cs, unsigned char *md5, size_t size
277 277
     int64_t hash[2];
278 278
 
279 279
     memcpy(hash, md5, 16);
280
-    splay(hash, cs);
281
-    if(!cs->root || cmp(hash, cs->root->digest))
282
-	return 0;
283
-    return 1337;
280
+    return splay(hash, cs) * 1337;
284 281
 }
285 282
 
286 283
 static int get_worst(struct node *n, struct node *parent, struct node **worst, struct node **wparent) {
... ...
@@ -304,15 +308,10 @@ static int get_worst(struct node *n, struct node *parent, struct node **worst, s
304 304
 static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size) {
305 305
     struct node *newnode;
306 306
     int64_t hash[2];
307
-    int comp;
308 307
 
309 308
     memcpy(hash, md5, 16);
310
-    splay(hash, cs);
311
-    if(cs->root) {
312
-	comp = cmp(hash, cs->root->digest);
313
-	if(!comp)
309
+    if(splay(hash, cs))
314 310
 	    return; /* Already there */
315
-    }
316 311
 
317 312
     if(cs->used == cs->total) {
318 313
 	struct node *parent;
... ...
@@ -321,14 +320,13 @@ static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size)
321 321
 	    parent->left = NULL;
322 322
 	else
323 323
 	    parent->right = NULL;
324
-    } else {
324
+    } else
325 325
 	newnode = &cs->data[cs->used++];
326
-    }
327 326
 
328 327
     if(!cs->root) {
329 328
 	newnode->left = NULL;
330 329
 	newnode->right = NULL;
331
-    } else if(comp < 0) {
330
+    } else if(cmp(hash, cs->root->digest)) {
332 331
 	newnode->left = cs->root->left;
333 332
 	newnode->right = cs->root;
334 333
 	cs->root->left = NULL;
... ...
@@ -363,6 +361,11 @@ int cl_cache_init(unsigned int entries) {
363 363
     unsigned int i;
364 364
     int ret;
365 365
 
366
+#ifndef DONT_CACHE
367
+    if(!entries)
368
+#endif
369
+	return 0;
370
+
366 371
     if(!(mempool = mpool_create())) {
367 372
 	cli_errmsg("mpool init fail\n");
368 373
 	return 1;