@matheusbsbs / matheus-bagliano / commits / b4718cae51
Fork 0

index-pack: calculate {ref,ofs}_{first,last} early

This is refactoring 2 of 2 to simplify struct base_data. Whenever we make a struct base_data, immediately calculate its delta children. This eliminates confusion as to when the {ref,ofs}_{first,last} fields are initialized. Before this patch, the delta children were calculated at the last possible moment. This allowed the members of struct base_data to be populated in any order, superficially useful when we have the object contents before the struct object_entry. But this makes reasoning about the state of struct base_data more complicated, hence this patch. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 24, 2020 at 12:16 UTC b4718cae51f9630f2901c452ca4e4dfbf68f47ef
1 file changed +60 -63
builtin/index-pack.c
+60 -63
@@ -33,12 +33,15 @@ struct object_stat {
33 };
34
35 struct base_data {
36 + /* Initialized by make_base(). */
37 struct base_data *base;
38 struct object_entry *obj;
38 - void *data;
39 - unsigned long size;
39 int ref_first, ref_last;
40 int ofs_first, ofs_last;
41 +
42 + /* Not initialized by make_base(). */
43 + void *data;
44 + unsigned long size;
45 };
46
47 struct thread_local {
@@ -362,14 +365,6 @@ static void set_thread_data(struct thread_local *data)
365 pthread_setspecific(key, data);
366 }
367
365 -static struct base_data *alloc_base_data(void)
366 -{
367 - struct base_data *base = xcalloc(1, sizeof(struct base_data));
368 - base->ref_last = -1;
369 - base->ofs_last = -1;
370 - return base;
371 -}
372 -
368 static void free_base_data(struct base_data *c)
369 {
370 if (c->data) {
@@ -406,19 +401,6 @@ static void prune_base_data(struct base_data *youngest_child)
401 free(ancestry);
402 }
403
409 -static void link_base_data(struct base_data *base, struct base_data *c)
410 -{
411 - c->base = base;
412 - if (c->data)
413 - get_thread_data()->base_cache_used += c->size;
414 - prune_base_data(c);
415 -}
416 -
417 -static void unlink_base_data(struct base_data *c)
418 -{
419 - free_base_data(c);
420 -}
421 -
404 static int is_delta_type(enum object_type type)
405 {
406 return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
@@ -929,10 +911,25 @@ static void *get_base_data(struct base_data *c)
911 return c->data;
912 }
913
932 -static void resolve_delta(struct object_entry *delta_obj,
933 - struct base_data *base, struct base_data *result)
914 +static struct base_data *make_base(struct object_entry *obj,
915 + struct base_data *parent)
916 {
935 - void *base_data, *delta_data;
917 + struct base_data *base = xcalloc(1, sizeof(struct base_data));
918 + base->base = parent;
919 + base->obj = obj;
920 + find_ref_delta_children(&obj->idx.oid,
921 + &base->ref_first, &base->ref_last);
922 + find_ofs_delta_children(obj->idx.offset,
923 + &base->ofs_first, &base->ofs_last);
924 + return base;
925 +}
926 +
927 +static struct base_data *resolve_delta(struct object_entry *delta_obj,
928 + struct base_data *base)
929 +{
930 + void *base_data, *delta_data, *result_data;
931 + struct base_data *result;
932 + unsigned long result_size;
933
934 if (show_stat) {
935 int i = delta_obj - objects;
@@ -946,19 +943,31 @@ static void resolve_delta(struct object_entry *delta_obj,
943 }
944 delta_data = get_data_from_pack(delta_obj);
945 base_data = get_base_data(base);
949 - result->obj = delta_obj;
950 - result->data = patch_delta(base_data, base->size,
951 - delta_data, delta_obj->size, &result->size);
946 + result_data = patch_delta(base_data, base->size,
947 + delta_data, delta_obj->size, &result_size);
948 free(delta_data);
953 - if (!result->data)
949 + if (!result_data)
950 bad_object(delta_obj->idx.offset, _("failed to apply delta"));
955 - hash_object_file(the_hash_algo, result->data, result->size,
951 + hash_object_file(the_hash_algo, result_data, result_size,
952 type_name(delta_obj->real_type), &delta_obj->idx.oid);
957 - sha1_object(result->data, NULL, result->size, delta_obj->real_type,
953 + sha1_object(result_data, NULL, result_size, delta_obj->real_type,
954 &delta_obj->idx.oid);
955 +
956 + result = make_base(delta_obj, base);
957 + if (result->ref_last == -1 && result->ofs_last == -1) {
958 + free(result_data);
959 + } else {
960 + result->data = result_data;
961 + result->size = result_size;
962 + get_thread_data()->base_cache_used += result->size;
963 + prune_base_data(result);
964 + }
965 +
966 counter_lock();
967 nr_resolved_deltas++;
968 counter_unlock();
969 +
970 + return result;
971 }
972
973 /*
@@ -984,24 +993,9 @@ static int compare_and_swap_type(signed char *type,
993 static struct base_data *find_unresolved_deltas_1(struct base_data *base,
994 struct base_data *prev_base)
995 {
987 - if (base->ref_last == -1 && base->ofs_last == -1) {
988 - find_ref_delta_children(&base->obj->idx.oid,
989 - &base->ref_first, &base->ref_last);
990 -
991 - find_ofs_delta_children(base->obj->idx.offset,
992 - &base->ofs_first, &base->ofs_last);
993 -
994 - if (base->ref_last == -1 && base->ofs_last == -1) {
995 - free(base->data);
996 - return NULL;
997 - }
998 -
999 - link_base_data(prev_base, base);
1000 - }
1001 -
996 if (base->ref_first <= base->ref_last) {
997 struct object_entry *child = objects + ref_deltas[base->ref_first].obj_no;
1004 - struct base_data *result = alloc_base_data();
998 + struct base_data *result;
999
1000 if (!compare_and_swap_type(&child->real_type, OBJ_REF_DELTA,
1001 base->obj->real_type))
@@ -1009,7 +1003,7 @@ static struct base_data *find_unresolved_deltas_1(struct base_data *base,
1003 (uintmax_t)child->idx.offset,
1004 oid_to_hex(&base->obj->idx.oid));
1005
1012 - resolve_delta(child, base, result);
1006 + result = resolve_delta(child, base);
1007 if (base->ref_first == base->ref_last && base->ofs_last == -1)
1008 free_base_data(base);
1009
@@ -1019,11 +1013,11 @@ static struct base_data *find_unresolved_deltas_1(struct base_data *base,
1013
1014 if (base->ofs_first <= base->ofs_last) {
1015 struct object_entry *child = objects + ofs_deltas[base->ofs_first].obj_no;
1022 - struct base_data *result = alloc_base_data();
1016 + struct base_data *result;
1017
1018 assert(child->real_type == OBJ_OFS_DELTA);
1019 child->real_type = base->obj->real_type;
1026 - resolve_delta(child, base, result);
1020 + result = resolve_delta(child, base);
1021 if (base->ofs_first == base->ofs_last)
1022 free_base_data(base);
1023
@@ -1031,7 +1025,7 @@ static struct base_data *find_unresolved_deltas_1(struct base_data *base,
1025 return result;
1026 }
1027
1034 - unlink_base_data(base);
1028 + free_base_data(base);
1029 return NULL;
1030 }
1031
@@ -1074,9 +1068,8 @@ static int compare_ref_delta_entry(const void *a, const void *b)
1068
1069 static void resolve_base(struct object_entry *obj)
1070 {
1077 - struct base_data *base_obj = alloc_base_data();
1078 - base_obj->obj = obj;
1079 - base_obj->data = NULL;
1071 + struct base_data *base_obj = make_base(obj, NULL);
1072 +
1073 find_unresolved_deltas(base_obj);
1074 }
1075
@@ -1369,22 +1362,26 @@ static void fix_unresolved_deltas(struct hashfile *f)
1362 for (i = 0; i < nr_ref_deltas; i++) {
1363 struct ref_delta_entry *d = sorted_by_pos[i];
1364 enum object_type type;
1372 - struct base_data *base_obj = alloc_base_data();
1365 + struct base_data *base;
1366 + void *data;
1367 + unsigned long size;
1368 + struct object_entry *obj;
1369
1370 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
1371 continue;
1376 - base_obj->data = read_object_file(&d->oid, &type,
1377 - &base_obj->size);
1378 - if (!base_obj->data)
1372 + data = read_object_file(&d->oid, &type, &size);
1373 + if (!data)
1374 continue;
1375
1376 if (check_object_signature(the_repository, &d->oid,
1382 - base_obj->data, base_obj->size,
1377 + data, size,
1378 type_name(type)))
1379 die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
1385 - base_obj->obj = append_obj_to_pack(f, d->oid.hash,
1386 - base_obj->data, base_obj->size, type);
1387 - find_unresolved_deltas(base_obj);
1380 + obj = append_obj_to_pack(f, d->oid.hash, data, size, type);
1381 + base = make_base(obj, NULL);
1382 + base->data = data;
1383 + base->size = size;
1384 + find_unresolved_deltas(base);
1385 display_progress(progress, nr_resolved_deltas);
1386 }
1387 free(sorted_by_pos);