From 5bdf6d4ac0d06817ce22322aa0172b49e6026544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Thu, 12 Jan 2023 13:55:26 +0100 Subject: read-cache.c: refactor set_new_index_sparsity() for subsequent commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor code added to set_new_index_sparsity() in [1] to eliminate indentation resulting from putting the body of his function within the "if" block. Let's instead return early if we have no istate->repo. This trivial change makes the subsequent commit's diff smaller. 1. 491df5f679f (read-cache: set sparsity when index is new, 2022-05-10) Signed-off-by: Ævar Arnfjörð Bjarmason Acked-by: Derrick Stolee Signed-off-by: Junio C Hamano --- read-cache.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'read-cache.c') diff --git a/read-cache.c b/read-cache.c index 1ff518b2a7..1782e94035 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2292,12 +2292,12 @@ static void set_new_index_sparsity(struct index_state *istate) * If the index's repo exists, mark it sparse according to * repo settings. */ - if (istate->repo) { - prepare_repo_settings(istate->repo); - if (!istate->repo->settings.command_requires_full_index && - is_sparse_index_allowed(istate, 0)) - istate->sparse_index = 1; - } + if (!istate->repo) + return; + prepare_repo_settings(istate->repo); + if (!istate->repo->settings.command_requires_full_index && + is_sparse_index_allowed(istate, 0)) + istate->sparse_index = 1; } /* remember to discard_cache() before reading a different cache! */ -- cgit v1.2.3 From 2f6b1eb794ee1f152c1a4b519e3b6dcecd0b487f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Thu, 12 Jan 2023 13:55:27 +0100 Subject: cache API: add a "INDEX_STATE_INIT" macro/function, add release_index() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hopefully in some not so distant future, we'll get advantages from always initializing the "repo" member of the "struct index_state". To make that easier let's introduce an initialization macro & function. The various ad-hoc initialization of the structure can then be changed over to it, and we can remove the various "0" assignments in discard_index() in favor of calling index_state_init() at the end. While not strictly necessary, let's also change the CALLOC_ARRAY() of various "struct index_state *" to use an ALLOC_ARRAY() followed by index_state_init() instead. We're then adding the release_index() function and converting some callers (including some of these allocations) over to it if they either won't need to use their "struct index_state" again, or are just about to call index_state_init(). Signed-off-by: Ævar Arnfjörð Bjarmason Acked-by: Derrick Stolee Signed-off-by: Junio C Hamano --- read-cache.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'read-cache.c') diff --git a/read-cache.c b/read-cache.c index 1782e94035..a91ec90f59 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2490,9 +2490,10 @@ int read_index_from(struct index_state *istate, const char *path, trace_performance_enter(); if (split_index->base) - discard_index(split_index->base); + release_index(split_index->base); else - CALLOC_ARRAY(split_index->base, 1); + ALLOC_ARRAY(split_index->base, 1); + index_state_init(split_index->base); base_oid_hex = oid_to_hex(&split_index->base_oid); base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex); @@ -2531,7 +2532,13 @@ int is_index_unborn(struct index_state *istate) return (!istate->cache_nr && !istate->timestamp.sec); } -void discard_index(struct index_state *istate) +void index_state_init(struct index_state *istate) +{ + struct index_state blank = INDEX_STATE_INIT; + memcpy(istate, &blank, sizeof(*istate)); +} + +void release_index(struct index_state *istate) { /* * Cache entries in istate->cache[] should have been allocated @@ -2543,20 +2550,12 @@ void discard_index(struct index_state *istate) validate_cache_entries(istate); resolve_undo_clear_index(istate); - istate->cache_nr = 0; - istate->cache_changed = 0; - istate->timestamp.sec = 0; - istate->timestamp.nsec = 0; free_name_hash(istate); cache_tree_free(&(istate->cache_tree)); - istate->initialized = 0; - istate->fsmonitor_has_run_once = 0; - FREE_AND_NULL(istate->fsmonitor_last_update); - FREE_AND_NULL(istate->cache); - istate->cache_alloc = 0; + free(istate->fsmonitor_last_update); + free(istate->cache); discard_split_index(istate); free_untracked_cache(istate->untracked); - istate->untracked = NULL; if (istate->sparse_checkout_patterns) { clear_pattern_list(istate->sparse_checkout_patterns); @@ -2569,6 +2568,12 @@ void discard_index(struct index_state *istate) } } +void discard_index(struct index_state *istate) +{ + release_index(istate); + index_state_init(istate); +} + /* * Validate the cache entries of this index. * All cache entries associated with this index -- cgit v1.2.3