Skip to content

Commit b300293

Browse files
jeffhostetlerGit for Windows Build Agent
authored andcommitted
read-cache: speed up add_index_entry during checkout
Teach add_index_entry_with_check() and has_dir_name() 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. This is a performance optimization. During checkout, merge_working_tree() populates the new index in sorted order, so this change saves at least 2 lookups per file. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent fa175f7 commit b300293

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
572572
extern int discard_index(struct index_state *);
573573
extern int unmerged_index(const struct index_state *);
574574
extern int verify_path(const char *path);
575+
extern int strcmp_offset(const char *s1_in, const char *s2_in, int *first_change);
575576
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);
576577
extern void adjust_dirname_case(struct index_state *istate, char *name);
577578
extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);

read-cache.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,34 @@ static int has_file_name(struct index_state *istate,
894894
return retval;
895895
}
896896

897+
/*
898+
* Like strcmp(), but also return the offset of the first change.
899+
*/
900+
int strcmp_offset(const char *s1_in, const char *s2_in, int *first_change)
901+
{
902+
const unsigned char *s1 = (const unsigned char *)s1_in;
903+
const unsigned char *s2 = (const unsigned char *)s2_in;
904+
int diff = 0;
905+
int k;
906+
907+
*first_change = 0;
908+
for (k=0; s1[k]; k++)
909+
if ((diff = (s1[k] - s2[k])))
910+
goto found_it;
911+
if (!s2[k])
912+
return 0;
913+
diff = -1;
914+
915+
found_it:
916+
*first_change = k;
917+
if (diff > 0)
918+
return 1;
919+
else if (diff < 0)
920+
return -1;
921+
else
922+
return 0;
923+
}
924+
897925
/*
898926
* Do we have another file with a pathname that is a proper
899927
* subset of the name we're trying to add?
@@ -905,6 +933,21 @@ static int has_dir_name(struct index_state *istate,
905933
int stage = ce_stage(ce);
906934
const char *name = ce->name;
907935
const char *slash = name + ce_namelen(ce);
936+
int len_eq_last;
937+
int cmp_last = 0;
938+
939+
if (istate->cache_nr > 0) {
940+
/*
941+
* Compare the entry's full path with the last path in the index.
942+
* If it sorts AFTER the last entry in the index and they have no
943+
* common prefix, then there cannot be any F/D name conflicts.
944+
*/
945+
cmp_last = strcmp_offset(name,
946+
istate->cache[istate->cache_nr-1]->name,
947+
&len_eq_last);
948+
if (cmp_last > 0 && len_eq_last == 0)
949+
return retval;
950+
}
908951

909952
for (;;) {
910953
int len;
@@ -917,6 +960,24 @@ static int has_dir_name(struct index_state *istate,
917960
}
918961
len = slash - name;
919962

963+
if (cmp_last > 0) {
964+
/*
965+
* If this part of the directory prefix (including the trailing
966+
* slash) already appears in the path of the last entry in the
967+
* index, then we cannot also have a file with this prefix (or
968+
* any parent directory prefix).
969+
*/
970+
if (len+1 <= len_eq_last)
971+
return retval;
972+
/*
973+
* If this part of the directory prefix (excluding the trailing
974+
* slash) is longer than the known equal portions, then this part
975+
* of the prefix cannot collide with a file. Go on to the parent.
976+
*/
977+
if (len > len_eq_last)
978+
continue;
979+
}
980+
920981
pos = index_name_stage_pos(istate, name, len, stage);
921982
if (pos >= 0) {
922983
/*
@@ -1008,7 +1069,16 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
10081069

10091070
if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
10101071
cache_tree_invalidate_path(istate, ce->name);
1011-
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
1072+
1073+
/*
1074+
* If this entry's path sorts after the last entry in the index,
1075+
* we can avoid searching for it.
1076+
*/
1077+
if (istate->cache_nr > 0 &&
1078+
strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
1079+
pos = -istate->cache_nr - 1;
1080+
else
1081+
pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
10121082

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

0 commit comments

Comments
 (0)