diff options
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2014-06-13 19:19:40 +0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-06-13 11:49:40 -0700 |
commit | 96a1d8d34c16be8fc5acd7ceb4d34573d8e2a76b (patch) | |
tree | 91116aa899d1c5d131a36cc852d21b6a28ac7971 | |
parent | 078a58e825d14987df82af9d19c00ddc8eb82481 (diff) | |
download | git-96a1d8d34c16be8fc5acd7ceb4d34573d8e2a76b.tar.gz |
split-index: the writing part
prepare_to_write_split_index() does the major work, classifying
deleted, updated and added entries. write_link_extension() then just
writes it down.
An observation is, deleting an entry, then adding it back is recorded
as "entry X is deleted, entry X is added", not "entry X is replaced".
This is simpler, with small overhead: a replaced entry is stored
without its path, a new entry is store with its path.
A note about unpack_trees() and the deduplication code inside
prepare_to_write_split_index(). Usually tracking updated/removed
entries via read-cache API is enough. unpack_trees() manipulates the
index in a different way: it throws the entire source index out,
builds up a new one, copying/duplicating entries (using dup_entry)
from the source index over if necessary, then returns the new index.
A naive solution would be marking the entire source index "deleted"
and add their duplicates as new. That could bring $GIT_DIR/index back
to the original size. So we try harder and memcmp() between the
original and the duplicate to see if it needs updating.
We could avoid memcmp() too, by avoiding duplicating the original
entry in dup_entry(). The performance gain this way is within noise
level and it complicates unpack-trees.c. So memcmp() is the preferred
way to deal with deduplication.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | split-index.c | 101 | ||||
-rw-r--r-- | split-index.h | 4 |
2 files changed, 103 insertions, 2 deletions
diff --git a/split-index.c b/split-index.c index b36c73b3aa..57088071fc 100644 --- a/split-index.c +++ b/split-index.c @@ -1,5 +1,6 @@ #include "cache.h" #include "split-index.h" +#include "ewah/ewok.h" struct split_index *init_split_index(struct index_state *istate) { @@ -26,11 +27,22 @@ int read_link_extension(struct index_state *istate, return 0; } +static int write_strbuf(void *user_data, const void *data, size_t len) +{ + struct strbuf *sb = user_data; + strbuf_add(sb, data, len); + return len; +} + int write_link_extension(struct strbuf *sb, struct index_state *istate) { struct split_index *si = istate->split_index; strbuf_add(sb, si->base_sha1, 20); + if (!si->delete_bitmap && !si->replace_bitmap) + return 0; + ewah_serialize_to(si->delete_bitmap, write_strbuf, sb); + ewah_serialize_to(si->replace_bitmap, write_strbuf, sb); return 0; } @@ -62,14 +74,99 @@ void merge_base_index(struct index_state *istate) void prepare_to_write_split_index(struct index_state *istate) { struct split_index *si = init_split_index(istate); - /* take cache[] out temporarily */ + struct cache_entry **entries = NULL, *ce; + int i, nr_entries = 0, nr_alloc = 0; + + si->delete_bitmap = ewah_new(); + si->replace_bitmap = ewah_new(); + + if (si->base) { + /* Go through istate->cache[] and mark CE_MATCHED to + * entry with positive index. We'll go through + * base->cache[] later to delete all entries in base + * that are not marked eith either CE_MATCHED or + * CE_UPDATE_IN_BASE. If istate->cache[i] is a + * duplicate, deduplicate it. + */ + for (i = 0; i < istate->cache_nr; i++) { + struct cache_entry *base; + /* namelen is checked separately */ + const unsigned int ondisk_flags = + CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS; + unsigned int ce_flags, base_flags, ret; + ce = istate->cache[i]; + if (!ce->index) + continue; + if (ce->index > si->base->cache_nr) { + ce->index = 0; + continue; + } + ce->ce_flags |= CE_MATCHED; /* or "shared" */ + base = si->base->cache[ce->index - 1]; + if (ce == base) + continue; + if (ce->ce_namelen != base->ce_namelen || + strcmp(ce->name, base->name)) { + ce->index = 0; + continue; + } + ce_flags = ce->ce_flags; + base_flags = base->ce_flags; + /* only on-disk flags matter */ + ce->ce_flags &= ondisk_flags; + base->ce_flags &= ondisk_flags; + ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data, + offsetof(struct cache_entry, name) - + offsetof(struct cache_entry, ce_stat_data)); + ce->ce_flags = ce_flags; + base->ce_flags = base_flags; + if (ret) + ce->ce_flags |= CE_UPDATE_IN_BASE; + free(base); + si->base->cache[ce->index - 1] = ce; + } + for (i = 0; i < si->base->cache_nr; i++) { + ce = si->base->cache[i]; + if ((ce->ce_flags & CE_REMOVE) || + !(ce->ce_flags & CE_MATCHED)) + ewah_set(si->delete_bitmap, i); + else if (ce->ce_flags & CE_UPDATE_IN_BASE) { + ewah_set(si->replace_bitmap, i); + ALLOC_GROW(entries, nr_entries+1, nr_alloc); + entries[nr_entries++] = ce; + } + } + } + + for (i = 0; i < istate->cache_nr; i++) { + ce = istate->cache[i]; + if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) { + ALLOC_GROW(entries, nr_entries+1, nr_alloc); + entries[nr_entries++] = ce; + } + ce->ce_flags &= ~CE_MATCHED; + } + + /* + * take cache[] out temporarily, put entries[] in its place + * for writing + */ + si->saved_cache = istate->cache; si->saved_cache_nr = istate->cache_nr; - istate->cache_nr = 0; + istate->cache = entries; + istate->cache_nr = nr_entries; } void finish_writing_split_index(struct index_state *istate) { struct split_index *si = init_split_index(istate); + + ewah_free(si->delete_bitmap); + ewah_free(si->replace_bitmap); + si->delete_bitmap = NULL; + si->replace_bitmap = NULL; + free(istate->cache); + istate->cache = si->saved_cache; istate->cache_nr = si->saved_cache_nr; } diff --git a/split-index.h b/split-index.h index 812e510f7e..53b778fa61 100644 --- a/split-index.h +++ b/split-index.h @@ -3,10 +3,14 @@ struct index_state; struct strbuf; +struct ewah_bitmap; struct split_index { unsigned char base_sha1[20]; struct index_state *base; + struct ewah_bitmap *delete_bitmap; + struct ewah_bitmap *replace_bitmap; + struct cache_entry **saved_cache; unsigned int saved_cache_nr; int refcount; }; |