Skip to content

Commit 06b6d81

Browse files
jeffhostetlergitster
authored andcommitted
read-cache: speed up has_dir_name (part 1)
Teach 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. has_dir_name() is looking for file/directory collisions in the index and has to consider each sub-directory prefix in turn. This can cause multiple binary searches for each path. During operations like checkout, merge_working_tree() populates the new index in sorted order, so we expect to be able to append in many cases. This commit is part 1 of 2. This commit handles the top of has_dir_name() and the trivial optimization. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e549463 commit 06b6d81

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

read-cache.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,9 @@ int strcmp_offset(const char *s1, const char *s2, size_t *first_change)
910910
/*
911911
* Do we have another file with a pathname that is a proper
912912
* subset of the name we're trying to add?
913+
*
914+
* That is, is there another file in the index with a path
915+
* that matches a sub-directory in the given entry?
913916
*/
914917
static int has_dir_name(struct index_state *istate,
915918
const struct cache_entry *ce, int pos, int ok_to_replace)
@@ -918,6 +921,48 @@ static int has_dir_name(struct index_state *istate,
918921
int stage = ce_stage(ce);
919922
const char *name = ce->name;
920923
const char *slash = name + ce_namelen(ce);
924+
size_t len_eq_last;
925+
int cmp_last = 0;
926+
927+
/*
928+
* We are frequently called during an iteration on a sorted
929+
* list of pathnames and while building a new index. Therefore,
930+
* there is a high probability that this entry will eventually
931+
* be appended to the index, rather than inserted in the middle.
932+
* If we can confirm that, we can avoid binary searches on the
933+
* components of the pathname.
934+
*
935+
* Compare the entry's full path with the last path in the index.
936+
*/
937+
if (istate->cache_nr > 0) {
938+
cmp_last = strcmp_offset(name,
939+
istate->cache[istate->cache_nr - 1]->name,
940+
&len_eq_last);
941+
if (cmp_last > 0) {
942+
if (len_eq_last == 0) {
943+
/*
944+
* The entry sorts AFTER the last one in the
945+
* index and their paths have no common prefix,
946+
* so there cannot be a F/D conflict.
947+
*/
948+
return retval;
949+
} else {
950+
/*
951+
* The entry sorts AFTER the last one in the
952+
* index, but has a common prefix. Fall through
953+
* to the loop below to disect the entry's path
954+
* and see where the difference is.
955+
*/
956+
}
957+
} else if (cmp_last == 0) {
958+
/*
959+
* The entry exactly matches the last one in the
960+
* index, but because of multiple stage and CE_REMOVE
961+
* items, we fall through and let the regular search
962+
* code handle it.
963+
*/
964+
}
965+
}
921966

922967
for (;;) {
923968
int len;

0 commit comments

Comments
 (0)