Skip to content

Commit e549463

Browse files
jeffhostetlergitster
authored andcommitted
read-cache: speed up add_index_entry during checkout
Teach add_index_entry_with_check() to see if the path of the new item is greater than the last path in the index array before attempting to search for it. During checkout, merge_working_tree() populates the new index in sorted order, so this change will save a binary lookups per file. This preserves the original behavior but simply checks the last element before starting the search. This helps performance on very large repositories. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 350d870 commit e549463

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

read-cache.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,16 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
10211021

10221022
if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
10231023
cache_tree_invalidate_path(istate, ce->name);
1024-
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
1024+
1025+
/*
1026+
* If this entry's path sorts after the last entry in the index,
1027+
* we can avoid searching for it.
1028+
*/
1029+
if (istate->cache_nr > 0 &&
1030+
strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
1031+
pos = -istate->cache_nr - 1;
1032+
else
1033+
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
10251034

10261035
/* existing match? Just replace it. */
10271036
if (pos >= 0) {

0 commit comments

Comments
 (0)