Skip to contentSkip to conversation

split-index: add and use unshare_split_index()

PATCH New Nguyễn Thái Ngọc Duy 1 participant 1 message Apr 19, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 split-index: add and use unshare_split_index()

When split-index is being used, we have two cache_entry arrays in index_state->cache[] and index_state->split_index->base->cache[].

index_state->cache[] may share the same entries with base->cache[] so we can quickly determine what entries are shared. This makes memory management tricky, we can't free base->cache[] until we know index_state->cache[] does not point to any of those entries.

unshare_split_index() is added for this purpose, to find shared entries and either duplicate them in index_state->cache[], or discard them. Either way it should be safe to free base->cache[] after unshare_split_index().

---
Sorry for the delay. Somehow I put this commit on a branch named "custom-decorate" and couldn't find it in "git branch" listing for obvious reason. Luckily "git log --patch --all" came and saved the day.

The idea is well explained in the commit message, I hope. I'll have to look at it again (it's been six months since it's written) to see if it still makes sense.

Anyway Christian, feel free to take over ;-)

 read-cache.c  | 10 ++--------
 split-index.c | 57 ++++++++++++++++++++++++++++++++++++++++++++-------------
 split-index.h |  1 +
 3 files changed, 47 insertions(+), 21 deletions(-)
read-cache.c +2 -8
index e447751823..ae2d02a687 100644
@@ -1734,15 +1734,9 @@ int discard_index(struct index_state *istate)
 {
 	int i;
 
-	for (i = 0; i < istate->cache_nr; i++) {
-		if (istate->cache[i]->index &&
-		    istate->split_index &&
-		    istate->split_index->base &&
-		    istate->cache[i]->index <= istate->split_index->base->cache_nr &&
-		    istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index - 1])
-			continue;
+	unshare_split_index(istate, 1);
+	for (i = 0; i < istate->cache_nr; i++)
 		free(istate->cache[i]);
-	}
 	resolve_undo_clear_index(istate);
 	istate->cache_nr = 0;
 	istate->cache_changed = 0;
split-index.c +44 -13

79 lines not on this page · raw message

split-index.h +1 -0
index df91c1bda8..65c0f09b2b 100644
@@ -33,5 +33,6 @@ void finish_writing_split_index(struct index_state *istate);
 void discard_split_index(struct index_state *istate);
 void add_split_index(struct index_state *istate);
 void remove_split_index(struct index_state *istate);
+void unshare_split_index(struct index_state *istate, int discard);
 
 #endif
Signature · 1 line
2.11.0.157.gd943d85