unpack-trees: refactor prefetching code

Refactor the prefetching code in unpack-trees.c into its own function, because it will be used elsewhere in a subsequent commit. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jul 23, 2021 at 11:52 UTC b2896d27391afcbc990439e63972bb33693a7d6b
3 files changed +40 -19
cache.h
+9
@@ -410,6 +410,15 @@ struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_s
410 */
411 void validate_cache_entries(const struct index_state *istate);
412
413 +/*
414 + * Bulk prefetch all missing cache entries that are not GITLINKs and that match
415 + * the given predicate. This function should only be called if
416 + * has_promisor_remote() returns true.
417 + */
418 +typedef int (*must_prefetch_predicate)(const struct cache_entry *);
419 +void prefetch_cache_entries(const struct index_state *istate,
420 + must_prefetch_predicate must_prefetch);
421 +
422 #ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
423 extern struct index_state the_index;
424
read-cache.c
+23
@@ -27,6 +27,7 @@
27 #include "progress.h"
28 #include "sparse-index.h"
29 #include "csum-file.h"
30 +#include "promisor-remote.h"
31
32 /* Mask for the name length in ce_flags in the on-disk index */
33
@@ -3657,3 +3658,25 @@ static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_ta
3658 strbuf_add(sb, &buffer, sizeof(uint32_t));
3659 }
3660 }
3661 +
3662 +void prefetch_cache_entries(const struct index_state *istate,
3663 + must_prefetch_predicate must_prefetch)
3664 +{
3665 + int i;
3666 + struct oid_array to_fetch = OID_ARRAY_INIT;
3667 +
3668 + for (i = 0; i < istate->cache_nr; i++) {
3669 + struct cache_entry *ce = istate->cache[i];
3670 +
3671 + if (S_ISGITLINK(ce->ce_mode) || !must_prefetch(ce))
3672 + continue;
3673 + if (!oid_object_info_extended(the_repository, &ce->oid,
3674 + NULL,
3675 + OBJECT_INFO_FOR_PREFETCH))
3676 + continue;
3677 + oid_array_append(&to_fetch, &ce->oid);
3678 + }
3679 + promisor_remote_get_direct(the_repository,
3680 + to_fetch.oid, to_fetch.nr);
3681 + oid_array_clear(&to_fetch);
3682 +}
unpack-trees.c
+8 -19
@@ -392,6 +392,11 @@ static void report_collided_checkout(struct index_state *index)
392 string_list_clear(&list, 0);
393 }
394
395 +static int must_checkout(const struct cache_entry *ce)
396 +{
397 + return ce->ce_flags & CE_UPDATE;
398 +}
399 +
400 static int check_updates(struct unpack_trees_options *o,
401 struct index_state *index)
402 {
@@ -442,28 +447,12 @@ static int check_updates(struct unpack_trees_options *o,
447 if (should_update_submodules())
448 load_gitmodules_file(index, &state);
449
445 - if (has_promisor_remote()) {
450 + if (has_promisor_remote())
451 /*
452 * Prefetch the objects that are to be checked out in the loop
453 * below.
454 */
450 - struct oid_array to_fetch = OID_ARRAY_INIT;
451 - for (i = 0; i < index->cache_nr; i++) {
452 - struct cache_entry *ce = index->cache[i];
453 -
454 - if (!(ce->ce_flags & CE_UPDATE) ||
455 - S_ISGITLINK(ce->ce_mode))
456 - continue;
457 - if (!oid_object_info_extended(the_repository, &ce->oid,
458 - NULL,
459 - OBJECT_INFO_FOR_PREFETCH))
460 - continue;
461 - oid_array_append(&to_fetch, &ce->oid);
462 - }
463 - promisor_remote_get_direct(the_repository,
464 - to_fetch.oid, to_fetch.nr);
465 - oid_array_clear(&to_fetch);
466 - }
455 + prefetch_cache_entries(index, must_checkout);
456
457 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
458
@@ -473,7 +462,7 @@ static int check_updates(struct unpack_trees_options *o,
462 for (i = 0; i < index->cache_nr; i++) {
463 struct cache_entry *ce = index->cache[i];
464
476 - if (ce->ce_flags & CE_UPDATE) {
465 + if (must_checkout(ce)) {
466 size_t last_pc_queue_size = pc_queue_size();
467
468 if (ce->ce_flags & CE_WT_REMOVE)