Skip to content

Commit 8b01378

Browse files
kbleesgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1c8cca1 commit 8b01378

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

cache.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ struct stat_data {
131131
};
132132

133133
struct cache_entry {
134+
struct hashmap_entry ent;
134135
struct stat_data ce_stat_data;
135136
unsigned int ce_mode;
136137
unsigned int ce_flags;
137138
unsigned int ce_namelen;
138139
unsigned char sha1[20];
139-
struct cache_entry *next;
140140
char name[FLEX_ARRAY]; /* more */
141141
};
142142

@@ -203,7 +203,9 @@ static inline void copy_cache_entry(struct cache_entry *dst,
203203
unsigned int state = dst->ce_flags & CE_STATE_MASK;
204204

205205
/* 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));
207209

208210
/* Restore the hash state */
209211
dst->ce_flags = (dst->ce_flags & ~CE_STATE_MASK) | state;
@@ -278,7 +280,7 @@ struct index_state {
278280
struct cache_time timestamp;
279281
unsigned name_hash_initialized : 1,
280282
initialized : 1;
281-
struct hash_table name_hash;
283+
struct hashmap name_hash;
282284
struct hashmap dir_hash;
283285
};
284286

name-hash.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,11 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
100100

101101
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
102102
{
103-
void **pos;
104-
unsigned int hash;
105-
106103
if (ce->ce_flags & CE_HASHED)
107104
return;
108105
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);
116108

117109
if (ignore_case && !(ce->ce_flags & CE_UNHASHED))
118110
add_dir_entry(istate, ce);
@@ -124,8 +116,7 @@ static void lazy_init_name_hash(struct index_state *istate)
124116

125117
if (istate->name_hash_initialized)
126118
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);
129120
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, 0);
130121
for (nr = 0; nr < istate->cache_nr; nr++)
131122
hash_index_entry(istate, istate->cache[nr]);
@@ -221,18 +212,19 @@ struct cache_entry *index_dir_exists(struct index_state *istate, const char *nam
221212

222213
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
223214
{
224-
unsigned int hash = memihash(name, namelen);
225215
struct cache_entry *ce;
216+
struct hashmap_entry key;
226217

227218
lazy_init_name_hash(istate);
228-
ce = lookup_hash(hash, &istate->name_hash);
229219

220+
hashmap_entry_init(&key, memihash(name, namelen));
221+
ce = hashmap_get(&istate->name_hash, &key, NULL);
230222
while (ce) {
231223
if (!(ce->ce_flags & CE_UNHASHED)) {
232224
if (same_name(ce, name, namelen, icase))
233225
return ce;
234226
}
235-
ce = ce->next;
227+
ce = hashmap_get_next(&istate->name_hash, ce);
236228
}
237229
return NULL;
238230
}
@@ -250,6 +242,6 @@ void free_name_hash(struct index_state *istate)
250242
return;
251243
istate->name_hash_initialized = 0;
252244

253-
free_hash(&istate->name_hash);
245+
hashmap_free(&istate->name_hash, 0);
254246
hashmap_free(&istate->dir_hash, 1);
255247
}

unpack-trees.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ static void do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
110110
if (set & CE_REMOVE)
111111
set |= CE_WT_REMOVE;
112112

113-
ce->next = NULL;
114113
ce->ce_flags = (ce->ce_flags & ~clear) | set;
115114
add_index_entry(&o->result, ce,
116115
ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);

0 commit comments

Comments
 (0)