Implement a simple delta_base cache
This trivial 256-entry delta_base cache improves performance for some loads by a factor of 2.5 or so. Instead of always re-generating the delta bases (possibly over and over and over again), just cache the last few ones. They often can get re-used. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Mar 17, 2007 at 12:44 UTC
e5e01619bcb753a3c45fb51d498371c9ff0677da
1 file changed
+42
-1
sha1_file.c
+42
-1
@@ -1352,16 +1352,57 @@ static void *unpack_compressed_entry(struct packed_git *p,
1352
return buffer;
1353
}
1354
1355
+#define MAX_DELTA_CACHE (256)
1356
+
1357
+static struct delta_base_cache_entry {
1358
+ struct packed_git *p;
1359
+ off_t base_offset;
1360
+ unsigned long size;
1361
+ void *data;
1362
+ enum object_type type;
1363
+} delta_base_cache[MAX_DELTA_CACHE];
1364
+
1365
+static unsigned long pack_entry_hash(struct packed_git *p, off_t base_offset)
1366
+{
1367
+ unsigned long hash;
1368
+
1369
+ hash = (unsigned long)p + (unsigned long)base_offset;
1370
+ hash += (hash >> 8) + (hash >> 16);
1371
+ return hash & 0xff;
1372
+}
1373
+
1374
static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
1375
unsigned long *base_size, enum object_type *type)
1376
{
1377
+ void *ret;
1378
+ unsigned long hash = pack_entry_hash(p, base_offset);
1379
+ struct delta_base_cache_entry *ent = delta_base_cache + hash;
1380
+
1381
+ ret = ent->data;
1382
+ if (ret && ent->p == p && ent->base_offset == base_offset)
1383
+ goto found_cache_entry;
1384
return unpack_entry(p, base_offset, type, base_size);
1385
+
1386
+found_cache_entry:
1387
+ ent->data = NULL;
1388
+ *type = ent->type;
1389
+ *base_size = ent->size;
1390
+ return ret;
1391
}
1392
1393
static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1394
void *base, unsigned long base_size, enum object_type type)
1395
{
1364
- free(base);
1396
+ unsigned long hash = pack_entry_hash(p, base_offset);
1397
+ struct delta_base_cache_entry *ent = delta_base_cache + hash;
1398
+
1399
+ if (ent->data)
1400
+ free(ent->data);
1401
+ ent->p = p;
1402
+ ent->base_offset = base_offset;
1403
+ ent->type = type;
1404
+ ent->data = base;
1405
+ ent->size = base_size;
1406
}
1407
1408
static void *unpack_delta_entry(struct packed_git *p,