Skip to content

Commit 092e511

Browse files
newrengitster
authored andcommitted
merge-ort: reuse path strings in pool_alloc_filespec
pool_alloc_filespec() was written so that the code when pool != NULL mimicked the code from alloc_filespec(), which including allocating enough extra space for the path and then copying it. However, the path passed to pool_alloc_filespec() is always going to already be in the same memory pool, so we may as well reuse it instead of copying it. For the testcases mentioned in commit 557ac03 ("merge-ort: begin performance work; instrument with trace2_region_* calls", 2020-10-28), this change improves the performance as follows: Before After no-renames: 198.5 ms ± 3.4 ms 198.3 ms ± 2.9 ms mega-renames: 679.1 ms ± 5.6 ms 661.8 ms ± 5.9 ms just-one-mega: 271.9 ms ± 2.8 ms 264.6 ms ± 2.5 ms Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f239fff commit 092e511

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

merge-ort.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,17 +694,13 @@ static struct diff_filespec *pool_alloc_filespec(struct mem_pool *pool,
694694
const char *path)
695695
{
696696
struct diff_filespec *spec;
697-
size_t len;
698697

699698
if (!pool)
700699
return alloc_filespec(path);
701700

702-
/* Same code as alloc_filespec, except allocate from pool */
703-
len = strlen(path);
704-
705-
spec = mem_pool_calloc(pool, 1, st_add3(sizeof(*spec), len, 1));
706-
memcpy(spec+1, path, len);
707-
spec->path = (void*)(spec+1);
701+
/* Similar to alloc_filespec, but allocate from pool and reuse path */
702+
spec = mem_pool_calloc(pool, 1, sizeof(*spec));
703+
spec->path = (char*)path; /* spec won't modify it */
708704

709705
spec->count = 1;
710706
spec->is_binary = -1;
@@ -2904,6 +2900,25 @@ static void use_cached_pairs(struct merge_options *opt,
29042900
const char *new_name = entry->value;
29052901
if (!new_name)
29062902
new_name = old_name;
2903+
if (pool) {
2904+
/*
2905+
* cached_pairs has _copies* of old_name and new_name,
2906+
* because it has to persist across merges. When
2907+
* pool != NULL
2908+
* pool_alloc_filespec() will just re-use the existing
2909+
* filenames, which will also get re-used by
2910+
* opt->priv->paths if they become renames, and then
2911+
* get freed at the end of the merge, leaving the copy
2912+
* in cached_pairs dangling. Avoid this by making a
2913+
* copy here.
2914+
*
2915+
* When pool == NULL, pool_alloc_filespec() calls
2916+
* alloc_filespec(), which makes a copy; we don't want
2917+
* to add another.
2918+
*/
2919+
old_name = mem_pool_strdup(pool, old_name);
2920+
new_name = mem_pool_strdup(pool, new_name);
2921+
}
29072922

29082923
/* We don't care about oid/mode, only filenames and status */
29092924
one = pool_alloc_filespec(pool, old_name);

0 commit comments

Comments
 (0)