Skip to content

Commit b4718ca

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a7f7e84 commit b4718ca

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

builtin/index-pack.c

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ struct object_stat {
3333
};
3434

3535
struct base_data {
36+
/* Initialized by make_base(). */
3637
struct base_data *base;
3738
struct object_entry *obj;
38-
void *data;
39-
unsigned long size;
4039
int ref_first, ref_last;
4140
int ofs_first, ofs_last;
41+
42+
/* Not initialized by make_base(). */
43+
void *data;
44+
unsigned long size;
4245
};
4346

4447
struct thread_local {
@@ -362,14 +365,6 @@ static void set_thread_data(struct thread_local *data)
362365
pthread_setspecific(key, data);
363366
}
364367

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-
373368
static void free_base_data(struct base_data *c)
374369
{
375370
if (c->data) {
@@ -406,19 +401,6 @@ static void prune_base_data(struct base_data *youngest_child)
406401
free(ancestry);
407402
}
408403

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-
422404
static int is_delta_type(enum object_type type)
423405
{
424406
return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
@@ -929,10 +911,25 @@ static void *get_base_data(struct base_data *c)
929911
return c->data;
930912
}
931913

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)
934916
{
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;
936933

937934
if (show_stat) {
938935
int i = delta_obj - objects;
@@ -946,19 +943,31 @@ static void resolve_delta(struct object_entry *delta_obj,
946943
}
947944
delta_data = get_data_from_pack(delta_obj);
948945
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);
952948
free(delta_data);
953-
if (!result->data)
949+
if (!result_data)
954950
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,
956952
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,
958954
&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+
959966
counter_lock();
960967
nr_resolved_deltas++;
961968
counter_unlock();
969+
970+
return result;
962971
}
963972

964973
/*
@@ -984,32 +993,17 @@ static int compare_and_swap_type(signed char *type,
984993
static struct base_data *find_unresolved_deltas_1(struct base_data *base,
985994
struct base_data *prev_base)
986995
{
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-
1002996
if (base->ref_first <= base->ref_last) {
1003997
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;
1005999

10061000
if (!compare_and_swap_type(&child->real_type, OBJ_REF_DELTA,
10071001
base->obj->real_type))
10081002
die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
10091003
(uintmax_t)child->idx.offset,
10101004
oid_to_hex(&base->obj->idx.oid));
10111005

1012-
resolve_delta(child, base, result);
1006+
result = resolve_delta(child, base);
10131007
if (base->ref_first == base->ref_last && base->ofs_last == -1)
10141008
free_base_data(base);
10151009

@@ -1019,19 +1013,19 @@ static struct base_data *find_unresolved_deltas_1(struct base_data *base,
10191013

10201014
if (base->ofs_first <= base->ofs_last) {
10211015
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;
10231017

10241018
assert(child->real_type == OBJ_OFS_DELTA);
10251019
child->real_type = base->obj->real_type;
1026-
resolve_delta(child, base, result);
1020+
result = resolve_delta(child, base);
10271021
if (base->ofs_first == base->ofs_last)
10281022
free_base_data(base);
10291023

10301024
base->ofs_first++;
10311025
return result;
10321026
}
10331027

1034-
unlink_base_data(base);
1028+
free_base_data(base);
10351029
return NULL;
10361030
}
10371031

@@ -1074,9 +1068,8 @@ static int compare_ref_delta_entry(const void *a, const void *b)
10741068

10751069
static void resolve_base(struct object_entry *obj)
10761070
{
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+
10801073
find_unresolved_deltas(base_obj);
10811074
}
10821075

@@ -1369,22 +1362,26 @@ static void fix_unresolved_deltas(struct hashfile *f)
13691362
for (i = 0; i < nr_ref_deltas; i++) {
13701363
struct ref_delta_entry *d = sorted_by_pos[i];
13711364
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;
13731369

13741370
if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
13751371
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)
13791374
continue;
13801375

13811376
if (check_object_signature(the_repository, &d->oid,
1382-
base_obj->data, base_obj->size,
1377+
data, size,
13831378
type_name(type)))
13841379
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);
13881385
display_progress(progress, nr_resolved_deltas);
13891386
}
13901387
free(sorted_by_pos);

0 commit comments

Comments
 (0)