builtin/checkout.c: complete parallel checkout support

Pathspec-limited checkouts (like `git checkout *.txt`) are performed by a code path that doesn't yet support parallel checkout because it calls checkout_entry() directly, instead of unpack_trees(). Let's add parallel checkout support for this code path too. The transient cache entries allocated in checkout_merged() are now allocated in a mem_pool which is only discarded after parallel checkout finishes. This is done because the entries need to be valid when run_parallel_checkout() is called. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed May 4, 2021 at 13:27 UTC 60539506329d60c80610ccbbfe1d18c746c4ae52
1 file changed +17 -4
builtin/checkout.c
+17 -4
@@ -27,6 +27,7 @@
27 #include "wt-status.h"
28 #include "xdiff-interface.h"
29 #include "entry.h"
30 +#include "parallel-checkout.h"
31
32 static const char * const checkout_usage[] = {
33 N_("git checkout [<options>] <branch>"),
@@ -230,7 +231,8 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
231 return error(_("path '%s' does not have their version"), ce->name);
232 }
233
233 -static int checkout_merged(int pos, const struct checkout *state, int *nr_checkouts)
234 +static int checkout_merged(int pos, const struct checkout *state,
235 + int *nr_checkouts, struct mem_pool *ce_mem_pool)
236 {
237 struct cache_entry *ce = active_cache[pos];
238 const char *path = ce->name;
@@ -291,11 +293,10 @@ static int checkout_merged(int pos, const struct checkout *state, int *nr_checko
293 if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid))
294 die(_("Unable to add merge result for '%s'"), path);
295 free(result_buf.ptr);
294 - ce = make_transient_cache_entry(mode, &oid, path, 2, NULL);
296 + ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);
297 if (!ce)
298 die(_("make_cache_entry failed for path '%s'"), path);
299 status = checkout_entry(ce, state, NULL, nr_checkouts);
298 - discard_cache_entry(ce);
300 return status;
301 }
302
@@ -359,16 +360,23 @@ static int checkout_worktree(const struct checkout_opts *opts,
360 int nr_checkouts = 0, nr_unmerged = 0;
361 int errs = 0;
362 int pos;
363 + int pc_workers, pc_threshold;
364 + struct mem_pool ce_mem_pool;
365
366 state.force = 1;
367 state.refresh_cache = 1;
368 state.istate = &the_index;
369
370 + mem_pool_init(&ce_mem_pool, 0);
371 + get_parallel_checkout_configs(&pc_workers, &pc_threshold);
372 init_checkout_metadata(&state.meta, info->refname,
373 info->commit ? &info->commit->object.oid : &info->oid,
374 NULL);
375
376 enable_delayed_checkout(&state);
377 + if (pc_workers > 1)
378 + init_parallel_checkout();
379 +
380 for (pos = 0; pos < active_nr; pos++) {
381 struct cache_entry *ce = active_cache[pos];
382 if (ce->ce_flags & CE_MATCHED) {
@@ -384,10 +392,15 @@ static int checkout_worktree(const struct checkout_opts *opts,
392 &nr_checkouts, opts->overlay_mode);
393 else if (opts->merge)
394 errs |= checkout_merged(pos, &state,
387 - &nr_unmerged);
395 + &nr_unmerged,
396 + &ce_mem_pool);
397 pos = skip_same_name(ce, pos) - 1;
398 }
399 }
400 + if (pc_workers > 1)
401 + errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
402 + NULL, NULL);
403 + mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
404 remove_marked_cache_entries(&the_index, 1);
405 remove_scheduled_dirs();
406 errs |= finish_delayed_checkout(&state, &nr_checkouts);