sparse-index: avoid unnecessary cache tree clearing
When converting a full index to sparse, clear and recreate the cache tree only if the cache tree is not fully valid. The convert_to_sparse operation should exit silently if a cache tree update cannot be successfully completed (e.g., due to a conflicted entry state). However, because this failure scenario only occurs when at least a portion of the cache tree is invalid, we can save ourselves the cost of clearing and recreating the cache tree by skipping the check when the cache tree is fully valid. Helped-by: Derrick Stolee <dstolee@microsoft.com> Co-authored-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Victoria Dye <vdye@github.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Victoria Dye committed
Nov 23, 2021 at 00:20 UTC
13f69f30826001d6a98a36854d0c92a61d0dfcb8
1 file changed
+14
-11
sparse-index.c
+14
-11
@@ -175,17 +175,20 @@ int convert_to_sparse(struct index_state *istate, int flags)
175
if (index_has_unmerged_entries(istate))
176
return 0;
177
178
- /* Clear and recompute the cache-tree */
179
- cache_tree_free(&istate->cache_tree);
180
- /*
181
- * Silently return if there is a problem with the cache tree update,
182
- * which might just be due to a conflict state in some entry.
183
- *
184
- * This might create new tree objects, so be sure to use
185
- * WRITE_TREE_MISSING_OK.
186
- */
187
- if (cache_tree_update(istate, WRITE_TREE_MISSING_OK))
188
- return 0;
178
+ if (!cache_tree_fully_valid(istate->cache_tree)) {
179
+ /* Clear and recompute the cache-tree */
180
+ cache_tree_free(&istate->cache_tree);
181
+
182
+ /*
183
+ * Silently return if there is a problem with the cache tree update,
184
+ * which might just be due to a conflict state in some entry.
185
+ *
186
+ * This might create new tree objects, so be sure to use
187
+ * WRITE_TREE_MISSING_OK.
188
+ */
189
+ if (cache_tree_update(istate, WRITE_TREE_MISSING_OK))
190
+ return 0;
191
+ }
192
193
remove_fsmonitor(istate);
194