merge-ort: set up a memory pool

merge-ort has a lot of data structures, and they all tend to be freed together in clear_or_reinit_internal_opts(). Set up a memory pool to allow us to make these allocations and deallocations faster. Future commits will adjust various callers to make use of this memory pool. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Jul 30, 2021 at 11:47 UTC 4137c54b902faa640c485dc6d20eaac946e503a5
1 file changed +25
merge-ort.c
+25
@@ -37,6 +37,8 @@
37 #include "unpack-trees.h"
38 #include "xdiff-interface.h"
39
40 +#define USE_MEMORY_POOL 1 /* faster, but obscures memory leak hunting */
41 +
42 /*
43 * We have many arrays of size 3. Whenever we have such an array, the
44 * indices refer to one of the sides of the three-way merge. This is so
@@ -339,6 +341,17 @@ struct merge_options_internal {
341 */
342 struct strmap conflicted;
343
344 + /*
345 + * pool: memory pool for fast allocation/deallocation
346 + *
347 + * We allocate room for lots of filenames and auxiliary data
348 + * structures in merge_options_internal, and it tends to all be
349 + * freed together too. Using a memory pool for these provides a
350 + * nice speedup.
351 + */
352 + struct mem_pool internal_pool;
353 + struct mem_pool *pool; /* NULL, or pointer to internal_pool */
354 +
355 /*
356 * paths_to_free: additional list of strings to free
357 *
@@ -603,6 +616,12 @@ static void clear_or_reinit_internal_opts(struct merge_options_internal *opti,
616 strmap_clear(&opti->output, 0);
617 }
618
619 +#if USE_MEMORY_POOL
620 + mem_pool_discard(&opti->internal_pool, 0);
621 + if (!reinitialize)
622 + opti->pool = NULL;
623 +#endif
624 +
625 /* Clean out callback_data as well. */
626 FREE_AND_NULL(renames->callback_data);
627 renames->callback_data_nr = renames->callback_data_alloc = 0;
@@ -4381,6 +4400,12 @@ static void merge_start(struct merge_options *opt, struct merge_result *result)
4400
4401 /* Initialization of various renames fields */
4402 renames = &opt->priv->renames;
4403 +#if USE_MEMORY_POOL
4404 + mem_pool_init(&opt->priv->internal_pool, 0);
4405 + opt->priv->pool = &opt->priv->internal_pool;
4406 +#else
4407 + opt->priv->pool = NULL;
4408 +#endif
4409 for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) {
4410 strintmap_init_with_options(&renames->dirs_removed[i],
4411 NOT_RELEVANT, NULL, 0);