pack-objects: rewrite add_descendants_to_write_order() iteratively

This removes the need to call this function recursively, shinking the code size slightly and netting a small performance increase. Signed-off-by: Dan McGee <dpmcgee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dan McGee committed Oct 18, 2011 at 00:21 UTC f380872f0abc7fe98022696996d346df99c53f1a
1 file changed +37 -7
builtin/pack-objects.c
+37 -7
@@ -468,12 +468,43 @@ static void add_descendants_to_write_order(struct object_entry **wo,
468 unsigned int *endp,
469 struct object_entry *e)
470 {
471 - struct object_entry *child;
472 -
473 - for (child = e->delta_child; child; child = child->delta_sibling)
474 - add_to_write_order(wo, endp, child);
475 - for (child = e->delta_child; child; child = child->delta_sibling)
476 - add_descendants_to_write_order(wo, endp, child);
471 + int add_to_order = 1;
472 + while (e) {
473 + if (add_to_order) {
474 + struct object_entry *s;
475 + /* add this node... */
476 + add_to_write_order(wo, endp, e);
477 + /* all its siblings... */
478 + for (s = e->delta_sibling; s; s = s->delta_sibling) {
479 + add_to_write_order(wo, endp, s);
480 + }
481 + }
482 + /* drop down a level to add left subtree nodes if possible */
483 + if (e->delta_child) {
484 + add_to_order = 1;
485 + e = e->delta_child;
486 + } else {
487 + add_to_order = 0;
488 + /* our sibling might have some children, it is next */
489 + if (e->delta_sibling) {
490 + e = e->delta_sibling;
491 + continue;
492 + }
493 + /* go back to our parent node */
494 + e = e->delta;
495 + while (e && !e->delta_sibling) {
496 + /* we're on the right side of a subtree, keep
497 + * going up until we can go right again */
498 + e = e->delta;
499 + }
500 + if (!e) {
501 + /* done- we hit our original root node */
502 + return;
503 + }
504 + /* pass it off to sibling at this level */
505 + e = e->delta_sibling;
506 + }
507 + };
508 }
509
510 static void add_family_to_write_order(struct object_entry **wo,
@@ -484,7 +515,6 @@ static void add_family_to_write_order(struct object_entry **wo,
515
516 for (root = e; root->delta; root = root->delta)
517 ; /* nothing */
487 - add_to_write_order(wo, endp, root);
518 add_descendants_to_write_order(wo, endp, root);
519 }
520