summaryrefslogtreecommitdiff
path: root/name-hash.c
diff options
context:
space:
mode:
authorKarsten Blees <karsten.blees@gmail.com>2013-11-14 20:22:27 +0100
committerJunio C Hamano <gitster@pobox.com>2013-11-18 13:04:24 -0800
commit419a597f6408d5b5cffe3e278258f58b47d15ad3 (patch)
tree511ee6ee942cb1c10a3cfb3030ec255ddbed2fb3 /name-hash.c
parent8b013788a14b96b8d20b1f6bc76a42f9733aefad (diff)
downloadgit-419a597f6408d5b5cffe3e278258f58b47d15ad3.tar.gz
name-hash.c: remove cache entries instead of marking them CE_UNHASHED
The new hashmap implementation supports remove, so really remove unused cache entries from the name hashmap instead of just marking them. The CE_UNHASHED flag and CE_STATE_MASK are no longer needed. Keep the CE_HASHED flag to prevent adding entries twice. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'name-hash.c')
-rw-r--r--name-hash.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/name-hash.c b/name-hash.c
index 488eccf2f9..9a3bd3f9a6 100644
--- a/name-hash.c
+++ b/name-hash.c
@@ -106,17 +106,29 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
hashmap_add(&istate->name_hash, ce);
- if (ignore_case && !(ce->ce_flags & CE_UNHASHED))
+ if (ignore_case)
add_dir_entry(istate, ce);
}
+static int cache_entry_cmp(const struct cache_entry *ce1,
+ const struct cache_entry *ce2, const void *remove)
+{
+ /*
+ * For remove_name_hash, find the exact entry (pointer equality); for
+ * index_name_exists, find all entries with matching hash code and
+ * decide whether the entry matches in same_name.
+ */
+ return remove ? !(ce1 == ce2) : 0;
+}
+
static void lazy_init_name_hash(struct index_state *istate)
{
int nr;
if (istate->name_hash_initialized)
return;
- hashmap_init(&istate->name_hash, NULL, istate->cache_nr);
+ hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
+ istate->cache_nr);
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, 0);
for (nr = 0; nr < istate->cache_nr; nr++)
hash_index_entry(istate, istate->cache[nr]);
@@ -125,31 +137,19 @@ static void lazy_init_name_hash(struct index_state *istate)
void add_name_hash(struct index_state *istate, struct cache_entry *ce)
{
- /* if already hashed, add reference to directory entries */
- if (ignore_case && (ce->ce_flags & CE_STATE_MASK) == CE_STATE_MASK)
- add_dir_entry(istate, ce);
-
- ce->ce_flags &= ~CE_UNHASHED;
if (istate->name_hash_initialized)
hash_index_entry(istate, ce);
}
-/*
- * We don't actually *remove* it, we can just mark it invalid so that
- * we won't find it in lookups.
- *
- * Not only would we have to search the lists (simple enough), but
- * we'd also have to rehash other hash buckets in case this makes the
- * hash bucket empty (common). So it's much better to just mark
- * it.
- */
void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
{
- /* if already hashed, release reference to directory entries */
- if (ignore_case && (ce->ce_flags & CE_STATE_MASK) == CE_HASHED)
- remove_dir_entry(istate, ce);
+ if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
+ return;
+ ce->ce_flags &= ~CE_HASHED;
+ hashmap_remove(&istate->name_hash, ce, ce);
- ce->ce_flags |= CE_UNHASHED;
+ if (ignore_case)
+ remove_dir_entry(istate, ce);
}
static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
@@ -220,10 +220,8 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
hashmap_entry_init(&key, memihash(name, namelen));
ce = hashmap_get(&istate->name_hash, &key, NULL);
while (ce) {
- if (!(ce->ce_flags & CE_UNHASHED)) {
- if (same_name(ce, name, namelen, icase))
- return ce;
- }
+ if (same_name(ce, name, namelen, icase))
+ return ce;
ce = hashmap_get_next(&istate->name_hash, ce);
}
return NULL;