Skip to content

Commit 4173057

Browse files
pcloudsgitster
authored andcommitted
index-pack: reduce object_entry size to save memory
For each object in the input pack, we need one struct object_entry. On x86-64, this struct is 64 bytes long. Although: - The 8 bytes for delta_depth and base_object_no are only useful when show_stat is set. And it's never set unless someone is debugging. - The three fields hdr_size, type and real_type take 4 bytes each even though they never use more than 4 bits. By moving delta_depth and base_object_no out of struct object_entry and make the other 3 fields one byte long instead of 4, we shrink 25% of this struct. On a 3.4M object repo (*) that's about 53MB. The saving is less impressive compared to index-pack memory use for basic bookkeeping (**), about 16%. (*) linux-2.6.git already has 4M objects as of v3.19-rc7 so this is not an unrealistic number of objects that we have to deal with. (**) 3.4M * (sizeof(object_entry) + sizeof(delta_entry)) = 311MB Brought-up-by: Matthew Sporleder <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9874fca commit 4173057

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

builtin/index-pack.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ static const char index_pack_usage[] =
1818
struct object_entry {
1919
struct pack_idx_entry idx;
2020
unsigned long size;
21-
unsigned int hdr_size;
22-
enum object_type type;
23-
enum object_type real_type;
21+
unsigned char hdr_size;
22+
signed char type;
23+
signed char real_type;
24+
};
25+
26+
struct object_stat {
2427
unsigned delta_depth;
2528
int base_object_no;
2629
};
@@ -64,6 +67,7 @@ struct delta_entry {
6467
};
6568

6669
static struct object_entry *objects;
70+
static struct object_stat *obj_stat;
6771
static struct delta_entry *deltas;
6872
static struct thread_local nothread_data;
6973
static int nr_objects;
@@ -873,13 +877,15 @@ static void resolve_delta(struct object_entry *delta_obj,
873877
void *base_data, *delta_data;
874878

875879
if (show_stat) {
876-
delta_obj->delta_depth = base->obj->delta_depth + 1;
880+
int i = delta_obj - objects;
881+
int j = base->obj - objects;
882+
obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1;
877883
deepest_delta_lock();
878-
if (deepest_delta < delta_obj->delta_depth)
879-
deepest_delta = delta_obj->delta_depth;
884+
if (deepest_delta < obj_stat[i].delta_depth)
885+
deepest_delta = obj_stat[i].delta_depth;
880886
deepest_delta_unlock();
887+
obj_stat[i].base_object_no = j;
881888
}
882-
delta_obj->base_object_no = base->obj - objects;
883889
delta_data = get_data_from_pack(delta_obj);
884890
base_data = get_base_data(base);
885891
result->obj = delta_obj;
@@ -902,7 +908,7 @@ static void resolve_delta(struct object_entry *delta_obj,
902908
* "want"; if so, swap in "set" and return true. Otherwise, leave it untouched
903909
* and return false.
904910
*/
905-
static int compare_and_swap_type(enum object_type *type,
911+
static int compare_and_swap_type(signed char *type,
906912
enum object_type want,
907913
enum object_type set)
908914
{
@@ -1499,7 +1505,7 @@ static void show_pack_info(int stat_only)
14991505
struct object_entry *obj = &objects[i];
15001506

15011507
if (is_delta_type(obj->type))
1502-
chain_histogram[obj->delta_depth - 1]++;
1508+
chain_histogram[obj_stat[i].delta_depth - 1]++;
15031509
if (stat_only)
15041510
continue;
15051511
printf("%s %-6s %lu %lu %"PRIuMAX,
@@ -1508,8 +1514,8 @@ static void show_pack_info(int stat_only)
15081514
(unsigned long)(obj[1].idx.offset - obj->idx.offset),
15091515
(uintmax_t)obj->idx.offset);
15101516
if (is_delta_type(obj->type)) {
1511-
struct object_entry *bobj = &objects[obj->base_object_no];
1512-
printf(" %u %s", obj->delta_depth, sha1_to_hex(bobj->idx.sha1));
1517+
struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
1518+
printf(" %u %s", obj_stat[i].delta_depth, sha1_to_hex(bobj->idx.sha1));
15131519
}
15141520
putchar('\n');
15151521
}
@@ -1672,6 +1678,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16721678
curr_pack = open_pack_file(pack_name);
16731679
parse_pack_header();
16741680
objects = xcalloc(nr_objects + 1, sizeof(struct object_entry));
1681+
if (show_stat)
1682+
obj_stat = xcalloc(nr_objects + 1, sizeof(struct object_stat));
16751683
deltas = xcalloc(nr_objects, sizeof(struct delta_entry));
16761684
parse_pack_objects(pack_sha1);
16771685
resolve_deltas();

0 commit comments

Comments
 (0)