@matheusbsbs / matheus-bagliano / commits / 9441b61dc5
Fork 0

index-pack: rationalize delta resolution code

Instead of having strange loops for walking unresolved deltas with the same base duplicated in many places, let's rework the code so this is done in a single place instead. This simplifies callers quite a bit too. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Pitre committed Oct 17, 2008 at 15:57 UTC 9441b61dc5c3f1f984114ec8bd470dc20c55dfe0
1 file changed +58 -65
index-pack.c
+58 -65
@@ -244,7 +244,8 @@ static void link_base_data(struct base_data *base, struct base_data *c)
244
245 c->base = base;
246 c->child = NULL;
247 - base_cache_used += c->size;
247 + if (c->data)
248 + base_cache_used += c->size;
249 prune_base_data(c);
250 }
251
@@ -494,8 +495,10 @@ static void *get_base_data(struct base_data *c)
495 free(raw);
496 if (!c->data)
497 bad_object(obj->idx.offset, "failed to apply delta");
497 - } else
498 + } else {
499 c->data = get_data_from_pack(obj);
500 + c->size = obj->size;
501 + }
502
503 base_cache_used += c->size;
504 prune_base_data(c);
@@ -504,49 +507,73 @@ static void *get_base_data(struct base_data *c)
507 }
508
509 static void resolve_delta(struct object_entry *delta_obj,
507 - struct base_data *base_obj, enum object_type type)
510 + struct base_data *base, struct base_data *result)
511 {
512 void *delta_data;
513 unsigned long delta_size;
511 - union delta_base delta_base;
512 - int j, first, last;
513 - struct base_data result;
514
515 - delta_obj->real_type = type;
515 + delta_obj->real_type = base->obj->type;
516 delta_data = get_data_from_pack(delta_obj);
517 delta_size = delta_obj->size;
518 - result.data = patch_delta(get_base_data(base_obj), base_obj->size,
519 - delta_data, delta_size,
520 - &result.size);
518 + result->obj = delta_obj;
519 + result->data = patch_delta(get_base_data(base), base->obj->size,
520 + delta_data, delta_size, &result->size);
521 free(delta_data);
522 - if (!result.data)
522 + if (!result->data)
523 bad_object(delta_obj->idx.offset, "failed to apply delta");
524 - sha1_object(result.data, result.size, type, delta_obj->idx.sha1);
524 + sha1_object(result->data, result->size, delta_obj->real_type,
525 + delta_obj->idx.sha1);
526 nr_resolved_deltas++;
527 +}
528 +
529 +static void find_unresolved_deltas(struct base_data *base,
530 + struct base_data *prev_base)
531 +{
532 + int i, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
533 +
534 + /*
535 + * This is a recursive function. Those brackets should help reducing
536 + * stack usage by limiting the scope of the delta_base union.
537 + */
538 + {
539 + union delta_base base_spec;
540 +
541 + hashcpy(base_spec.sha1, base->obj->idx.sha1);
542 + ref = !find_delta_children(&base_spec, &ref_first, &ref_last);
543 +
544 + memset(&base_spec, 0, sizeof(base_spec));
545 + base_spec.offset = base->obj->idx.offset;
546 + ofs = !find_delta_children(&base_spec, &ofs_first, &ofs_last);
547 + }
548 +
549 + if (!ref && !ofs)
550 + return;
551
527 - result.obj = delta_obj;
528 - link_base_data(base_obj, &result);
552 + link_base_data(prev_base, base);
553
530 - hashcpy(delta_base.sha1, delta_obj->idx.sha1);
531 - if (!find_delta_children(&delta_base, &first, &last)) {
532 - for (j = first; j <= last; j++) {
533 - struct object_entry *child = objects + deltas[j].obj_no;
534 - if (child->real_type == OBJ_REF_DELTA)
535 - resolve_delta(child, &result, type);
554 + if (ref) {
555 + for (i = ref_first; i <= ref_last; i++) {
556 + struct object_entry *child = objects + deltas[i].obj_no;
557 + if (child->real_type == OBJ_REF_DELTA) {
558 + struct base_data result;
559 + resolve_delta(child, base, &result);
560 + find_unresolved_deltas(&result, base);
561 + }
562 }
563 }
564
539 - memset(&delta_base, 0, sizeof(delta_base));
540 - delta_base.offset = delta_obj->idx.offset;
541 - if (!find_delta_children(&delta_base, &first, &last)) {
542 - for (j = first; j <= last; j++) {
543 - struct object_entry *child = objects + deltas[j].obj_no;
544 - if (child->real_type == OBJ_OFS_DELTA)
545 - resolve_delta(child, &result, type);
565 + if (ofs) {
566 + for (i = ofs_first; i <= ofs_last; i++) {
567 + struct object_entry *child = objects + deltas[i].obj_no;
568 + if (child->real_type == OBJ_OFS_DELTA) {
569 + struct base_data result;
570 + resolve_delta(child, base, &result);
571 + find_unresolved_deltas(&result, base);
572 + }
573 }
574 }
575
549 - unlink_base_data(&result);
576 + unlink_base_data(base);
577 }
578
579 static int compare_delta_entry(const void *a, const void *b)
@@ -622,37 +649,13 @@ static void parse_pack_objects(unsigned char *sha1)
649 progress = start_progress("Resolving deltas", nr_deltas);
650 for (i = 0; i < nr_objects; i++) {
651 struct object_entry *obj = &objects[i];
625 - union delta_base base;
626 - int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
652 struct base_data base_obj;
653
654 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
655 continue;
631 - hashcpy(base.sha1, obj->idx.sha1);
632 - ref = !find_delta_children(&base, &ref_first, &ref_last);
633 - memset(&base, 0, sizeof(base));
634 - base.offset = obj->idx.offset;
635 - ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
636 - if (!ref && !ofs)
637 - continue;
638 - base_obj.data = get_data_from_pack(obj);
639 - base_obj.size = obj->size;
656 base_obj.obj = obj;
641 - link_base_data(NULL, &base_obj);
642 -
643 - if (ref)
644 - for (j = ref_first; j <= ref_last; j++) {
645 - struct object_entry *child = objects + deltas[j].obj_no;
646 - if (child->real_type == OBJ_REF_DELTA)
647 - resolve_delta(child, &base_obj, obj->type);
648 - }
649 - if (ofs)
650 - for (j = ofs_first; j <= ofs_last; j++) {
651 - struct object_entry *child = objects + deltas[j].obj_no;
652 - if (child->real_type == OBJ_OFS_DELTA)
653 - resolve_delta(child, &base_obj, obj->type);
654 - }
655 - unlink_base_data(&base_obj);
657 + base_obj.data = NULL;
658 + find_unresolved_deltas(&base_obj, NULL);
659 display_progress(progress, nr_resolved_deltas);
660 }
661 }
@@ -745,7 +748,6 @@ static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
748 for (i = 0; i < n; i++) {
749 struct delta_entry *d = sorted_by_pos[i];
750 enum object_type type;
748 - int j, first, last;
751 struct base_data base_obj;
752
753 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
@@ -759,16 +761,7 @@ static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
761 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
762 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
763 base_obj.data, base_obj.size, type);
762 - link_base_data(NULL, &base_obj);
763 -
764 - find_delta_children(&d->base, &first, &last);
765 - for (j = first; j <= last; j++) {
766 - struct object_entry *child = objects + deltas[j].obj_no;
767 - if (child->real_type == OBJ_REF_DELTA)
768 - resolve_delta(child, &base_obj, type);
769 - }
770 -
771 - unlink_base_data(&base_obj);
764 + find_unresolved_deltas(&base_obj, NULL);
765 display_progress(progress, nr_resolved_deltas);
766 }
767 free(sorted_by_pos);