Skip to content

Commit f380872

Browse files
toofishesgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 92bef1a commit f380872

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

builtin/pack-objects.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,43 @@ static void add_descendants_to_write_order(struct object_entry **wo,
468468
unsigned int *endp,
469469
struct object_entry *e)
470470
{
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+
};
477508
}
478509

479510
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,
484515

485516
for (root = e; root->delta; root = root->delta)
486517
; /* nothing */
487-
add_to_write_order(wo, endp, root);
488518
add_descendants_to_write_order(wo, endp, root);
489519
}
490520

0 commit comments

Comments
 (0)