summaryrefslogtreecommitdiff
path: root/sparse-index.c
diff options
context:
space:
mode:
authorLibravatar Derrick Stolee <dstolee@microsoft.com>2021-04-12 21:08:16 +0000
committerLibravatar Junio C Hamano <gitster@pobox.com>2021-04-14 13:47:54 -0700
commit71f82d032f34f7aa3708b4cdaeb12db617449606 (patch)
tree1bee2c9735b6930ba12df0e06337ced48b71f253 /sparse-index.c
parentname-hash: don't add directories to name_hash (diff)
downloadtgif-71f82d032f34f7aa3708b4cdaeb12db617449606.tar.xz
sparse-index: expand_to_path()
Some users of the index API have a specific path they are looking for, but choose to use index_file_exists() to rely on the name-hash hashtable instead of doing binary search with index_name_pos(). These users only need to know a yes/no answer, not a position within the cache array. When the index is sparse, the name-hash hash table does not contain the full list of paths within sparse directories. It _does_ contain the directory names for the sparse-directory entries. Create a helper function, expand_to_path(), for intended use with the name-hash hashtable functions. The integration with name-hash.c will follow in a later change. The solution here is to use ensure_full_index() when we determine that the requested path is within a sparse directory entry. This will populate the name-hash hashtable as the index is recomputed from scratch. There may be cases where the caller is trying to find an untracked path that is not in the index but also is not within a sparse directory entry. We want to minimize the overhead for these requests. If we used index_name_pos() to find the insertion order of the path, then we could determine from that position if a sparse-directory exists. (In fact, just calling index_name_pos() in that case would lead to expanding the index to a full index.) However, this takes O(log N) time where N is the number of cache entries. To keep the performance of this call based mostly on the input string, use index_file_exists() to look for the ancestors of the path. Using the heuristic that a sparse directory is likely to have a small number of parent directories, we start from the bottom and build up. Use a string buffer to allow mutating the path name to terminate after each slash for each hashset test. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'sparse-index.c')
-rw-r--r--sparse-index.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/sparse-index.c b/sparse-index.c
index 95ea17174d..6f21397e2e 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -283,3 +283,76 @@ void ensure_full_index(struct index_state *istate)
trace2_region_leave("index", "ensure_full_index", istate->repo);
}
+
+/*
+ * This static global helps avoid infinite recursion between
+ * expand_to_path() and index_file_exists().
+ */
+static int in_expand_to_path = 0;
+
+void expand_to_path(struct index_state *istate,
+ const char *path, size_t pathlen, int icase)
+{
+ struct strbuf path_mutable = STRBUF_INIT;
+ size_t substr_len;
+
+ /* prevent extra recursion */
+ if (in_expand_to_path)
+ return;
+
+ if (!istate || !istate->sparse_index)
+ return;
+
+ if (!istate->repo)
+ istate->repo = the_repository;
+
+ in_expand_to_path = 1;
+
+ /*
+ * We only need to actually expand a region if the
+ * following are both true:
+ *
+ * 1. 'path' is not already in the index.
+ * 2. Some parent directory of 'path' is a sparse directory.
+ */
+
+ if (index_file_exists(istate, path, pathlen, icase))
+ goto cleanup;
+
+ strbuf_add(&path_mutable, path, pathlen);
+ strbuf_addch(&path_mutable, '/');
+
+ /* Check the name hash for all parent directories */
+ substr_len = 0;
+ while (substr_len < pathlen) {
+ char temp;
+ char *replace = strchr(path_mutable.buf + substr_len, '/');
+
+ if (!replace)
+ break;
+
+ /* replace the character _after_ the slash */
+ replace++;
+ temp = *replace;
+ *replace = '\0';
+ if (index_file_exists(istate, path_mutable.buf,
+ path_mutable.len, icase)) {
+ /*
+ * We found a parent directory in the name-hash
+ * hashtable, because only sparse directory entries
+ * have a trailing '/' character. Since "path" wasn't
+ * in the index, perhaps it exists within this
+ * sparse-directory. Expand accordingly.
+ */
+ ensure_full_index(istate);
+ break;
+ }
+
+ *replace = temp;
+ substr_len = replace - path_mutable.buf;
+ }
+
+cleanup:
+ strbuf_release(&path_mutable);
+ in_expand_to_path = 0;
+}