Skip to content

Commit f1e11c6

Browse files
pcloudsgitster
authored andcommitted
unpack-trees: reduce malloc in cache-tree walk
This is a micro optimization that probably only shines on repos with deep directory structure. Instead of allocating and freeing a new cache_entry in every iteration, we reuse the last one and only update the parts that are new each iteration. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b4da373 commit f1e11c6

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

unpack-trees.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,8 @@ static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
685685
{
686686
struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
687687
struct unpack_trees_options *o = info->data;
688+
struct cache_entry *tree_ce = NULL;
689+
int ce_len = 0;
688690
int i, d;
689691

690692
if (!o->merge)
@@ -699,30 +701,39 @@ static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
699701
* get here in the first place.
700702
*/
701703
for (i = 0; i < nr_entries; i++) {
702-
struct cache_entry *tree_ce;
703-
int len, rc;
704+
int new_ce_len, len, rc;
704705

705706
src[0] = o->src_index->cache[pos + i];
706707

707708
len = ce_namelen(src[0]);
708-
tree_ce = xcalloc(1, cache_entry_size(len));
709+
new_ce_len = cache_entry_size(len);
710+
711+
if (new_ce_len > ce_len) {
712+
new_ce_len <<= 1;
713+
tree_ce = xrealloc(tree_ce, new_ce_len);
714+
memset(tree_ce, 0, new_ce_len);
715+
ce_len = new_ce_len;
716+
717+
tree_ce->ce_flags = create_ce_flags(0);
718+
719+
for (d = 1; d <= nr_names; d++)
720+
src[d] = tree_ce;
721+
}
709722

710723
tree_ce->ce_mode = src[0]->ce_mode;
711-
tree_ce->ce_flags = create_ce_flags(0);
712724
tree_ce->ce_namelen = len;
713725
oidcpy(&tree_ce->oid, &src[0]->oid);
714726
memcpy(tree_ce->name, src[0]->name, len + 1);
715727

716-
for (d = 1; d <= nr_names; d++)
717-
src[d] = tree_ce;
718-
719728
rc = call_unpack_fn((const struct cache_entry * const *)src, o);
720-
free(tree_ce);
721-
if (rc < 0)
729+
if (rc < 0) {
730+
free(tree_ce);
722731
return rc;
732+
}
723733

724734
mark_ce_used(src[0], o);
725735
}
736+
free(tree_ce);
726737
if (o->debug_unpack)
727738
printf("Unpacked %d entries from %s to %s using cache-tree\n",
728739
nr_entries,

0 commit comments

Comments
 (0)