Skip to content

Commit b2896d2

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb27b33 commit b2896d2

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

cache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,15 @@ struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_s
410410
*/
411411
void validate_cache_entries(const struct index_state *istate);
412412

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+
413422
#ifdef USE_THE_INDEX_COMPATIBILITY_MACROS
414423
extern struct index_state the_index;
415424

read-cache.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "progress.h"
2828
#include "sparse-index.h"
2929
#include "csum-file.h"
30+
#include "promisor-remote.h"
3031

3132
/* Mask for the name length in ce_flags in the on-disk index */
3233

@@ -3657,3 +3658,25 @@ static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_ta
36573658
strbuf_add(sb, &buffer, sizeof(uint32_t));
36583659
}
36593660
}
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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ static void report_collided_checkout(struct index_state *index)
392392
string_list_clear(&list, 0);
393393
}
394394

395+
static int must_checkout(const struct cache_entry *ce)
396+
{
397+
return ce->ce_flags & CE_UPDATE;
398+
}
399+
395400
static int check_updates(struct unpack_trees_options *o,
396401
struct index_state *index)
397402
{
@@ -442,28 +447,12 @@ static int check_updates(struct unpack_trees_options *o,
442447
if (should_update_submodules())
443448
load_gitmodules_file(index, &state);
444449

445-
if (has_promisor_remote()) {
450+
if (has_promisor_remote())
446451
/*
447452
* Prefetch the objects that are to be checked out in the loop
448453
* below.
449454
*/
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);
467456

468457
get_parallel_checkout_configs(&pc_workers, &pc_threshold);
469458

@@ -473,7 +462,7 @@ static int check_updates(struct unpack_trees_options *o,
473462
for (i = 0; i < index->cache_nr; i++) {
474463
struct cache_entry *ce = index->cache[i];
475464

476-
if (ce->ce_flags & CE_UPDATE) {
465+
if (must_checkout(ce)) {
477466
size_t last_pc_queue_size = pc_queue_size();
478467

479468
if (ce->ce_flags & CE_WT_REMOVE)

0 commit comments

Comments
 (0)