Skip to content

Commit b904166

Browse files
Nicolas Pitregitster
authored andcommitted
pack-objects: reverse the delta search sort list
It is currently sorted and then walked backward. Not only this doesn't feel natural for my poor brain, but it would make the next patch less obvious as well. So reverse the sort order, and reverse the list walking direction, which effectively produce the exact same end result as before. Also bring the relevant comment nearer the actual code and adjust it accordingly, with minor additional clarifications. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b7a28f7 commit b904166

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

builtin-pack-objects.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,28 +1245,37 @@ static void get_object_details(void)
12451245
free(sorted_by_offset);
12461246
}
12471247

1248+
/*
1249+
* We search for deltas in a list sorted by type, by filename hash, and then
1250+
* by size, so that we see progressively smaller and smaller files.
1251+
* That's because we prefer deltas to be from the bigger file
1252+
* to the smaller -- deletes are potentially cheaper, but perhaps
1253+
* more importantly, the bigger file is likely the more recent
1254+
* one. The deepest deltas are therefore the oldest objects which are
1255+
* less susceptible to be accessed often.
1256+
*/
12481257
static int type_size_sort(const void *_a, const void *_b)
12491258
{
12501259
const struct object_entry *a = *(struct object_entry **)_a;
12511260
const struct object_entry *b = *(struct object_entry **)_b;
12521261

1253-
if (a->type < b->type)
1254-
return -1;
12551262
if (a->type > b->type)
1256-
return 1;
1257-
if (a->hash < b->hash)
12581263
return -1;
1259-
if (a->hash > b->hash)
1264+
if (a->type < b->type)
12601265
return 1;
1261-
if (a->preferred_base < b->preferred_base)
1266+
if (a->hash > b->hash)
12621267
return -1;
1263-
if (a->preferred_base > b->preferred_base)
1268+
if (a->hash < b->hash)
12641269
return 1;
1265-
if (a->size < b->size)
1270+
if (a->preferred_base > b->preferred_base)
12661271
return -1;
1272+
if (a->preferred_base < b->preferred_base)
1273+
return 1;
12671274
if (a->size > b->size)
1275+
return -1;
1276+
if (a->size < b->size)
12681277
return 1;
1269-
return a > b ? -1 : (a < b); /* newest last */
1278+
return a < b ? -1 : (a > b); /* newest first */
12701279
}
12711280

12721281
struct unpacked {
@@ -1317,14 +1326,6 @@ static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
13171326

13181327
#endif
13191328

1320-
/*
1321-
* We search for deltas _backwards_ in a list sorted by type and
1322-
* by size, so that we see progressively smaller and smaller files.
1323-
* That's because we prefer deltas to be from the bigger file
1324-
* to the smaller - deletes are potentially cheaper, but perhaps
1325-
* more importantly, the bigger file is likely the more recent
1326-
* one.
1327-
*/
13281329
static int try_delta(struct unpacked *trg, struct unpacked *src,
13291330
unsigned max_depth, unsigned long *mem_usage)
13301331
{
@@ -1481,7 +1482,7 @@ static unsigned long free_unpacked(struct unpacked *n)
14811482
static void find_deltas(struct object_entry **list, unsigned list_size,
14821483
int window, int depth, unsigned *processed)
14831484
{
1484-
uint32_t i = list_size, idx = 0, count = 0;
1485+
uint32_t i = 0, idx = 0, count = 0;
14851486
unsigned int array_size = window * sizeof(struct unpacked);
14861487
struct unpacked *array;
14871488
unsigned long mem_usage = 0;
@@ -1490,7 +1491,7 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
14901491
memset(array, 0, array_size);
14911492

14921493
do {
1493-
struct object_entry *entry = list[--i];
1494+
struct object_entry *entry = list[i++];
14941495
struct unpacked *n = array + idx;
14951496
int j, max_depth, best_base = -1;
14961497

@@ -1575,7 +1576,7 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
15751576
count++;
15761577
if (idx >= window)
15771578
idx = 0;
1578-
} while (i > 0);
1579+
} while (i < list_size);
15791580

15801581
for (i = 0; i < window; ++i) {
15811582
free_delta_index(array[i].index);

0 commit comments

Comments
 (0)