name-hash.c: use new hash map implementation for cache entries

Note: the "ce->next = NULL;" in unpack-trees.c::do_add_entry can safely be removed, as ce->next (now ce->ent.next) is always properly initialized in name-hash.c::hash_index_entry. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Nov 14, 2013 at 20:21 UTC 8b013788a14b96b8d20b1f6bc76a42f9733aefad
3 files changed +13 -20
cache.h
+5 -3
@@ -131,12 +131,12 @@ struct stat_data {
131 };
132
133 struct cache_entry {
134 + struct hashmap_entry ent;
135 struct stat_data ce_stat_data;
136 unsigned int ce_mode;
137 unsigned int ce_flags;
138 unsigned int ce_namelen;
139 unsigned char sha1[20];
139 - struct cache_entry *next;
140 char name[FLEX_ARRAY]; /* more */
141 };
142
@@ -203,7 +203,9 @@ static inline void copy_cache_entry(struct cache_entry *dst,
203 unsigned int state = dst->ce_flags & CE_STATE_MASK;
204
205 /* Don't copy hash chain and name */
206 - memcpy(dst, src, offsetof(struct cache_entry, next));
206 + memcpy(&dst->ce_stat_data, &src->ce_stat_data,
207 + offsetof(struct cache_entry, name) -
208 + offsetof(struct cache_entry, ce_stat_data));
209
210 /* Restore the hash state */
211 dst->ce_flags = (dst->ce_flags & ~CE_STATE_MASK) | state;
@@ -278,7 +280,7 @@ struct index_state {
280 struct cache_time timestamp;
281 unsigned name_hash_initialized : 1,
282 initialized : 1;
281 - struct hash_table name_hash;
283 + struct hashmap name_hash;
284 struct hashmap dir_hash;
285 };
286
name-hash.c
+8 -16
@@ -100,19 +100,11 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
100
101 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
102 {
103 - void **pos;
104 - unsigned int hash;
105 -
103 if (ce->ce_flags & CE_HASHED)
104 return;
105 ce->ce_flags |= CE_HASHED;
109 - ce->next = NULL;
110 - hash = memihash(ce->name, ce_namelen(ce));
111 - pos = insert_hash(hash, ce, &istate->name_hash);
112 - if (pos) {
113 - ce->next = *pos;
114 - *pos = ce;
115 - }
106 + hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
107 + hashmap_add(&istate->name_hash, ce);
108
109 if (ignore_case && !(ce->ce_flags & CE_UNHASHED))
110 add_dir_entry(istate, ce);
@@ -124,8 +116,7 @@ static void lazy_init_name_hash(struct index_state *istate)
116
117 if (istate->name_hash_initialized)
118 return;
127 - if (istate->cache_nr)
128 - preallocate_hash(&istate->name_hash, istate->cache_nr);
119 + hashmap_init(&istate->name_hash, NULL, istate->cache_nr);
120 hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, 0);
121 for (nr = 0; nr < istate->cache_nr; nr++)
122 hash_index_entry(istate, istate->cache[nr]);
@@ -221,18 +212,19 @@ struct cache_entry *index_dir_exists(struct index_state *istate, const char *nam
212
213 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
214 {
224 - unsigned int hash = memihash(name, namelen);
215 struct cache_entry *ce;
216 + struct hashmap_entry key;
217
218 lazy_init_name_hash(istate);
228 - ce = lookup_hash(hash, &istate->name_hash);
219
220 + hashmap_entry_init(&key, memihash(name, namelen));
221 + ce = hashmap_get(&istate->name_hash, &key, NULL);
222 while (ce) {
223 if (!(ce->ce_flags & CE_UNHASHED)) {
224 if (same_name(ce, name, namelen, icase))
225 return ce;
226 }
235 - ce = ce->next;
227 + ce = hashmap_get_next(&istate->name_hash, ce);
228 }
229 return NULL;
230 }
@@ -250,6 +242,6 @@ void free_name_hash(struct index_state *istate)
242 return;
243 istate->name_hash_initialized = 0;
244
253 - free_hash(&istate->name_hash);
245 + hashmap_free(&istate->name_hash, 0);
246 hashmap_free(&istate->dir_hash, 1);
247 }
unpack-trees.c
-1
@@ -110,7 +110,6 @@ static void do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
110 if (set & CE_REMOVE)
111 set |= CE_WT_REMOVE;
112
113 - ce->next = NULL;
113 ce->ce_flags = (ce->ce_flags & ~clear) | set;
114 add_index_entry(&o->result, ce,
115 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);