@matheusbsbs / matheus-bagliano / commits / 5e08ecbff2
Fork 0

use a LRU eviction policy for the delta base cache

This provides a smoother degradation in performance when the cache gets trashed due to the delta_base_cache_limit being reached. Limited testing with really small delta_base_cache_limit values appears to confirm this. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Mar 19, 2007 at 16:31 UTC 5e08ecbff219dc68a13eb372b39030134e737c8a
1 file changed +31 -8
sha1_file.c
+31 -8
@@ -1355,11 +1355,18 @@ static void *unpack_compressed_entry(struct packed_git *p,
1355 #define MAX_DELTA_CACHE (256)
1356
1357 static size_t delta_base_cached;
1358 +
1359 +static struct delta_base_cache_lru_list {
1360 + struct delta_base_cache_lru_list *prev;
1361 + struct delta_base_cache_lru_list *next;
1362 +} delta_base_cache_lru = { &delta_base_cache_lru, &delta_base_cache_lru };
1363 +
1364 static struct delta_base_cache_entry {
1365 + struct delta_base_cache_lru_list lru;
1366 + void *data;
1367 struct packed_git *p;
1368 off_t base_offset;
1369 unsigned long size;
1362 - void *data;
1370 enum object_type type;
1371 } delta_base_cache[MAX_DELTA_CACHE];
1372
@@ -1387,6 +1394,8 @@ static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
1394 found_cache_entry:
1395 if (!keep_cache) {
1396 ent->data = NULL;
1397 + ent->lru.next->prev = ent->lru.prev;
1398 + ent->lru.prev->next = ent->lru.next;
1399 delta_base_cached -= ent->size;
1400 }
1401 else {
@@ -1404,6 +1413,8 @@ static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
1413 if (ent->data) {
1414 free(ent->data);
1415 ent->data = NULL;
1416 + ent->lru.next->prev = ent->lru.prev;
1417 + ent->lru.prev->next = ent->lru.next;
1418 delta_base_cached -= ent->size;
1419 }
1420 }
@@ -1411,26 +1422,38 @@ static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
1422 static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1423 void *base, unsigned long base_size, enum object_type type)
1424 {
1414 - unsigned long i, hash = pack_entry_hash(p, base_offset);
1425 + unsigned long hash = pack_entry_hash(p, base_offset);
1426 struct delta_base_cache_entry *ent = delta_base_cache + hash;
1427 + struct delta_base_cache_lru_list *lru;
1428
1429 release_delta_base_cache(ent);
1430 delta_base_cached += base_size;
1419 - for (i = 0; delta_base_cached > delta_base_cache_limit
1420 - && i < MAX_DELTA_CACHE ; i++) {
1421 - struct delta_base_cache_entry *f = delta_base_cache + i;
1431 +
1432 + for (lru = delta_base_cache_lru.next;
1433 + delta_base_cached > delta_base_cache_limit
1434 + && lru != &delta_base_cache_lru;
1435 + lru = lru->next) {
1436 + struct delta_base_cache_entry *f = (void *)lru;
1437 if (f->type == OBJ_BLOB)
1438 release_delta_base_cache(f);
1439 }
1425 - for (i = 0; delta_base_cached > delta_base_cache_limit
1426 - && i < MAX_DELTA_CACHE ; i++)
1427 - release_delta_base_cache(delta_base_cache + i);
1440 + for (lru = delta_base_cache_lru.next;
1441 + delta_base_cached > delta_base_cache_limit
1442 + && lru != &delta_base_cache_lru;
1443 + lru = lru->next) {
1444 + struct delta_base_cache_entry *f = (void *)lru;
1445 + release_delta_base_cache(f);
1446 + }
1447
1448 ent->p = p;
1449 ent->base_offset = base_offset;
1450 ent->type = type;
1451 ent->data = base;
1452 ent->size = base_size;
1453 + ent->lru.next = &delta_base_cache_lru;
1454 + ent->lru.prev = delta_base_cache_lru.prev;
1455 + delta_base_cache_lru.prev->next = &ent->lru;
1456 + delta_base_cache_lru.prev = &ent->lru;
1457 }
1458
1459 static void *unpack_delta_entry(struct packed_git *p,