Skip to content

Commit e3d8379

Browse files
szedergitster
authored andcommitted
split-index: don't compare cached data of entries already marked for split index
When unpack_trees() constructs a new index, it copies cache entries from the original index [1]. prepare_to_write_split_index() has to deal with this, and it has a dedicated code path for copied entries that are present in the shared index, where it compares the cached data in the corresponding copied and original entries. If the cached data matches, then they are considered the same; if it differs, then the copied entry will be marked for inclusion as a replacement entry in the just about to be written split index by setting the CE_UPDATE_IN_BASE flag. However, a cache entry already has its CE_UPDATE_IN_BASE flag set upon reading the split index, if the entry already has a replacement entry there, or upon refreshing the cached stat data, if the corresponding file was modified. The state of this flag is then preserved when unpack_trees() copies a cache entry from the shared index. So modify prepare_to_write_split_index() to check the copied cache entries' CE_UPDATE_IN_BASE flag first, and skip the thorough comparison of cached data if the flag is already set. Those couple of lines comparing the cached data would then have too many levels of indentation, so extract them into a helper function. Note that comparing the cached data in copied and original entries in the shared index might actually be entirely unnecessary. In theory all code paths refreshing the cached stat data of an entry in the shared index should set the CE_UPDATE_IN_BASE flag in that entry, and unpack_trees() should preserve this flag when copying cache entries. This means that the cached data is only ever changed if the CE_UPDATE_IN_BASE flag is set as well. Our test suite seems to confirm this: instrumenting the conditions in question and running the test suite repeatedly with 'GIT_TEST_SPLIT_INDEX=yes' showed that the cached data in a copied entry differs from the data in the shared entry only if its CE_UPDATE_IN_BASE flag is indeed set. In practice, however, our test suite doesn't have 100% coverage, GIT_TEST_SPLIT_INDEX is inherently random, and I certainly can't claim to possess complete understanding of what goes on in unpack_trees()... Therefore I kept the comparison of the cached data when CE_UPDATE_IN_BASE is not set, just in case that an unnoticed or future code path were to accidentally miss setting this flag upon refreshing the cached stat data or unpack_trees() were to drop this flag while copying a cache entry. [1] Note that when unpack_trees() constructs the new index and decides that a cache entry should now refer to different content than what was recorded in the original index (e.g. 'git read-tree -m HEAD^'), then that can't really be considered a copy of the original, but rather the creation of a new entry. Notably and pertinent to the split index feature, such a new entry doesn't have a reference to the original's shared index entry anymore, i.e. its 'index' field is set to 0. Consequently, such an entry is treated by prepare_to_write_split_index() as an entry not present in the shared index and it will be added to the new split index, while the original entry will be marked as deleted, and neither the above discussion nor the changes in this patch apply to them. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2034e84 commit e3d8379

File tree

1 file changed

+72
-17
lines changed

1 file changed

+72
-17
lines changed

split-index.c

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,30 @@ void merge_base_index(struct index_state *istate)
188188
si->saved_cache_nr = 0;
189189
}
190190

191+
/*
192+
* Compare most of the fields in two cache entries, i.e. all except the
193+
* hashmap_entry and the name.
194+
*/
195+
static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
196+
{
197+
const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
198+
CE_EXTENDED_FLAGS;
199+
unsigned int ce_flags = a->ce_flags;
200+
unsigned int base_flags = b->ce_flags;
201+
int ret;
202+
203+
/* only on-disk flags matter */
204+
a->ce_flags &= ondisk_flags;
205+
b->ce_flags &= ondisk_flags;
206+
ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
207+
offsetof(struct cache_entry, name) -
208+
offsetof(struct cache_entry, ce_stat_data));
209+
a->ce_flags = ce_flags;
210+
b->ce_flags = base_flags;
211+
212+
return ret;
213+
}
214+
191215
void prepare_to_write_split_index(struct index_state *istate)
192216
{
193217
struct split_index *si = init_split_index(istate);
@@ -207,13 +231,28 @@ void prepare_to_write_split_index(struct index_state *istate)
207231
*/
208232
for (i = 0; i < istate->cache_nr; i++) {
209233
struct cache_entry *base;
210-
/* namelen is checked separately */
211-
const unsigned int ondisk_flags =
212-
CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
213-
unsigned int ce_flags, base_flags, ret;
214234
ce = istate->cache[i];
215-
if (!ce->index)
235+
if (!ce->index) {
236+
/*
237+
* During simple update index operations this
238+
* is a cache entry that is not present in
239+
* the shared index. It will be added to the
240+
* split index.
241+
*
242+
* However, it might also represent a file
243+
* that already has a cache entry in the
244+
* shared index, but a new index has just
245+
* been constructed by unpack_trees(), and
246+
* this entry now refers to different content
247+
* than what was recorded in the original
248+
* index, e.g. during 'read-tree -m HEAD^' or
249+
* 'checkout HEAD^'. In this case the
250+
* original entry in the shared index will be
251+
* marked as deleted, and this entry will be
252+
* added to the split index.
253+
*/
216254
continue;
255+
}
217256
if (ce->index > si->base->cache_nr) {
218257
ce->index = 0;
219258
continue;
@@ -227,18 +266,34 @@ void prepare_to_write_split_index(struct index_state *istate)
227266
ce->index = 0;
228267
continue;
229268
}
230-
ce_flags = ce->ce_flags;
231-
base_flags = base->ce_flags;
232-
/* only on-disk flags matter */
233-
ce->ce_flags &= ondisk_flags;
234-
base->ce_flags &= ondisk_flags;
235-
ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
236-
offsetof(struct cache_entry, name) -
237-
offsetof(struct cache_entry, ce_stat_data));
238-
ce->ce_flags = ce_flags;
239-
base->ce_flags = base_flags;
240-
if (ret)
241-
ce->ce_flags |= CE_UPDATE_IN_BASE;
269+
/*
270+
* This is the copy of a cache entry that is present
271+
* in the shared index, created by unpack_trees()
272+
* while it constructed a new index.
273+
*/
274+
if (ce->ce_flags & CE_UPDATE_IN_BASE) {
275+
/*
276+
* Already marked for inclusion in the split
277+
* index, either because the corresponding
278+
* file was modified and the cached stat data
279+
* was refreshed, or because the original
280+
* entry already had a replacement entry in
281+
* the split index.
282+
* Nothing to do.
283+
*/
284+
} else {
285+
/*
286+
* Thoroughly compare the cached data to see
287+
* whether it should be marked for inclusion
288+
* in the split index.
289+
*
290+
* This comparison might be unnecessary, as
291+
* code paths modifying the cached data do
292+
* set CE_UPDATE_IN_BASE as well.
293+
*/
294+
if (compare_ce_content(ce, base))
295+
ce->ce_flags |= CE_UPDATE_IN_BASE;
296+
}
242297
discard_cache_entry(base);
243298
si->base->cache[ce->index - 1] = ce;
244299
}

0 commit comments

Comments
 (0)