Skip to content

Commit e382638

Browse files
committed
cache-tree: retire cache_tree_fully_valid() API function
All callers of this helper use it like so: if (!cache_tree_fully_valid(...)) cache_tree_update(...); to optimize out the call to cache_tree_update() when the cache-tree structure is already known to be valid. However, cache_tree_update() function itself already has an equivalent check that short-cuts the recomputation of the tree object name for each subdirectory. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b0e9f9 commit e382638

File tree

7 files changed

+19
-43
lines changed

7 files changed

+19
-43
lines changed

builtin/checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
580580
if (!active_cache_tree)
581581
active_cache_tree = cache_tree();
582582

583-
if (!cache_tree_fully_valid(active_cache_tree))
584-
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
583+
cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
585584

586585
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
587586
die(_("unable to write new index file"));

builtin/commit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
405405
if (!only && !pathspec.nr) {
406406
hold_locked_index(&index_lock, 1);
407407
refresh_cache_or_die(refresh_flags);
408-
if (!cache_tree_fully_valid(active_cache_tree))
409-
update_main_cache_tree(WRITE_TREE_SILENT);
408+
update_main_cache_tree(WRITE_TREE_SILENT);
410409
if (active_cache_changed) {
411410
if (write_locked_index(&the_index, &index_lock,
412411
COMMIT_LOCK))

cache-tree.c

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,6 @@ static void discard_unused_subtrees(struct cache_tree *it)
220220
}
221221
}
222222

223-
int cache_tree_fully_valid(struct cache_tree *it)
224-
{
225-
int i;
226-
if (!it)
227-
return 0;
228-
if (it->entry_count < 0 || !has_sha1_file(it->sha1))
229-
return 0;
230-
for (i = 0; i < it->subtree_nr; i++) {
231-
if (!cache_tree_fully_valid(it->down[i]->cache_tree))
232-
return 0;
233-
}
234-
return 1;
235-
}
236-
237223
static int update_one(struct cache_tree *it,
238224
struct cache_entry **cache,
239225
int entries,
@@ -592,7 +578,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
592578

593579
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
594580
{
595-
int entries, was_valid, newfd;
581+
int entries, newfd;
596582
struct lock_file *lock_file;
597583

598584
/*
@@ -612,21 +598,18 @@ int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, co
612598
if (!index_state->cache_tree)
613599
index_state->cache_tree = cache_tree();
614600

615-
was_valid = cache_tree_fully_valid(index_state->cache_tree);
616-
if (!was_valid) {
617-
if (cache_tree_update(index_state, flags) < 0)
618-
return WRITE_TREE_UNMERGED_INDEX;
619-
if (0 <= newfd) {
620-
if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
621-
newfd = -1;
622-
}
623-
/* Not being able to write is fine -- we are only interested
624-
* in updating the cache-tree part, and if the next caller
625-
* ends up using the old index with unupdated cache-tree part
626-
* it misses the work we did here, but that is just a
627-
* performance penalty and not a big deal.
628-
*/
601+
if (cache_tree_update(index_state, flags) < 0)
602+
return WRITE_TREE_UNMERGED_INDEX;
603+
if (0 <= newfd) {
604+
if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
605+
newfd = -1;
629606
}
607+
/* Not being able to write is fine -- we are only interested
608+
* in updating the cache-tree part, and if the next caller
609+
* ends up using the old index with unupdated cache-tree part
610+
* it misses the work we did here, but that is just a
611+
* performance penalty and not a big deal.
612+
*/
630613

631614
if (prefix) {
632615
struct cache_tree *subtree;

cache-tree.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
2929
void cache_tree_write(struct strbuf *, struct cache_tree *root);
3030
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3131

32-
int cache_tree_fully_valid(struct cache_tree *);
3332
int cache_tree_update(struct index_state *, int);
3433

3534
int update_main_cache_tree(int);

merge-recursive.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ struct tree *write_tree_from_memory(struct merge_options *o)
265265
if (!active_cache_tree)
266266
active_cache_tree = cache_tree();
267267

268-
if (!cache_tree_fully_valid(active_cache_tree) &&
269-
cache_tree_update(&the_index, 0) < 0)
268+
if (cache_tree_update(&the_index, 0) < 0)
270269
die(_("error building trees"));
271270

272271
result = lookup_tree(active_cache_tree->sha1);

sequencer.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,8 @@ static int is_index_unchanged(void)
333333
if (!active_cache_tree)
334334
active_cache_tree = cache_tree();
335335

336-
if (!cache_tree_fully_valid(active_cache_tree))
337-
if (cache_tree_update(&the_index, 0))
338-
return error(_("Unable to update cache tree\n"));
336+
if (cache_tree_update(&the_index, 0))
337+
return error(_("Unable to update cache tree\n"));
339338

340339
return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash);
341340
}

unpack-trees.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
12441244
if (!ret) {
12451245
if (!o->result.cache_tree)
12461246
o->result.cache_tree = cache_tree();
1247-
if (!cache_tree_fully_valid(o->result.cache_tree))
1248-
cache_tree_update(&o->result,
1249-
WRITE_TREE_SILENT |
1250-
WRITE_TREE_REPAIR);
1247+
cache_tree_update(&o->result,
1248+
WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
12511249
}
12521250
discard_index(o->dst_index);
12531251
*o->dst_index = o->result;

0 commit comments

Comments
 (0)