Skip to content

Commit 3d77a8b

Browse files
jeffhostetlerdscho
authored andcommitted
name-hash: precompute hash values during preload-index
Precompute the istate.name_hash and istate.dir_hash values for each cache-entry during the preload-index phase. Move the expensive memihash() calculations from lazy_init_name_hash() to the multi-threaded preload-index phase. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 35e4137 commit 3d77a8b

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

cache.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ struct cache_entry {
173173
unsigned int ce_flags;
174174
unsigned int ce_namelen;
175175
unsigned int index; /* for link extension */
176+
unsigned int precompute_hash_state;
177+
unsigned int precompute_hash_name;
178+
unsigned int precompute_hash_dir;
176179
struct object_id oid;
177180
char name[FLEX_ARRAY]; /* more */
178181
};
@@ -229,6 +232,19 @@ struct cache_entry {
229232
#error "CE_EXTENDED_FLAGS out of range"
230233
#endif
231234

235+
/*
236+
* Bit set if preload-index precomputed the hash value(s)
237+
* for this cache-entry.
238+
*/
239+
#define CE_PRECOMPUTE_HASH_STATE__SET (1 << 0)
240+
/*
241+
* Bit set if precompute-index also precomputed the hash value
242+
* for the parent directory.
243+
*/
244+
#define CE_PRECOMPUTE_HASH_STATE__DIR (1 << 1)
245+
246+
void precompute_istate_hashes(struct cache_entry *ce);
247+
232248
/* Forward structure decls */
233249
struct pathspec;
234250
struct child_process;

name-hash.c

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
5050
*/
5151
struct dir_entry *dir;
5252
unsigned int hash;
53+
int use_precomputed_dir_hash = 0;
54+
55+
if (ce->precompute_hash_state & CE_PRECOMPUTE_HASH_STATE__SET) {
56+
if (!(ce->precompute_hash_state & CE_PRECOMPUTE_HASH_STATE__DIR))
57+
return NULL; /* item does not have a parent directory */
58+
if (namelen == ce_namelen(ce)) {
59+
/* dir hash only valid for outer-most call (not recursive ones) */
60+
use_precomputed_dir_hash = 1;
61+
hash = ce->precompute_hash_dir;
62+
}
63+
}
5364

5465
/* get length of parent directory */
5566
while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
@@ -59,7 +70,8 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
5970
namelen--;
6071

6172
/* lookup existing entry for that directory */
62-
hash = memihash(ce->name, namelen);
73+
if (!use_precomputed_dir_hash)
74+
hash = memihash(ce->name, namelen);
6375
dir = find_dir_entry__hash(istate, ce->name, namelen, hash);
6476
if (!dir) {
6577
/* not found, create it and add to hash table */
@@ -99,10 +111,18 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
99111

100112
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
101113
{
114+
unsigned int h;
115+
102116
if (ce->ce_flags & CE_HASHED)
103117
return;
104118
ce->ce_flags |= CE_HASHED;
105-
hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
119+
120+
if (ce->precompute_hash_state & CE_PRECOMPUTE_HASH_STATE__SET)
121+
h = ce->precompute_hash_name;
122+
else
123+
h = memihash(ce->name, ce_namelen(ce));
124+
125+
hashmap_entry_init(ce, h);
106126
hashmap_add(&istate->name_hash, ce);
107127

108128
if (ignore_case)
@@ -244,3 +264,45 @@ void free_name_hash(struct index_state *istate)
244264
hashmap_free(&istate->name_hash, 0);
245265
hashmap_free(&istate->dir_hash, 1);
246266
}
267+
268+
/*
269+
* Precompute the hash values for this cache_entry
270+
* for use in the istate.name_hash and istate.dir_hash.
271+
*
272+
* If the item is in the root directory, just compute the
273+
* hash value (for istate.name_hash) on the full path.
274+
*
275+
* If the item is in a subdirectory, first compute the
276+
* hash value for the immediate parent directory (for
277+
* istate.dir_hash) and then the hash value for the full
278+
* path by continuing the computation.
279+
*
280+
* Note that these hashes will be used by
281+
* wt_status_collect_untracked() as it scans the worktree
282+
* and maps observed paths back to the index (optionally
283+
* ignoring case). Therefore, we probably only *NEED* to
284+
* precompute this for non-skip-worktree items (since
285+
* status should not observe skipped items), but because
286+
* lazy_init_name_hash() hashes everything, we force it
287+
* here.
288+
*/
289+
void precompute_istate_hashes(struct cache_entry *ce)
290+
{
291+
int namelen = ce_namelen(ce);
292+
293+
while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
294+
namelen--;
295+
296+
if (namelen <= 0) {
297+
ce->precompute_hash_name = memihash(ce->name, ce_namelen(ce));
298+
ce->precompute_hash_state = CE_PRECOMPUTE_HASH_STATE__SET;
299+
} else {
300+
namelen--;
301+
ce->precompute_hash_dir = memihash(ce->name, namelen);
302+
ce->precompute_hash_name = memihash_cont(
303+
ce->precompute_hash_dir, &ce->name[namelen],
304+
ce_namelen(ce) - namelen);
305+
ce->precompute_hash_state =
306+
CE_PRECOMPUTE_HASH_STATE__SET | CE_PRECOMPUTE_HASH_STATE__DIR;
307+
}
308+
}

preload-index.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static void *preload_thread(void *_data)
4747
struct cache_entry *ce = *cep++;
4848
struct stat st;
4949

50+
precompute_istate_hashes(ce);
51+
5052
if (ce_stage(ce))
5153
continue;
5254
if (S_ISGITLINK(ce->ce_mode))

read-cache.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n
7373
copy_cache_entry(new, old);
7474
new->ce_flags &= ~CE_HASHED;
7575
new->ce_namelen = namelen;
76+
new->precompute_hash_state = 0;
7677
new->index = 0;
7778
memcpy(new->name, new_name, namelen + 1);
7879

@@ -615,6 +616,7 @@ static struct cache_entry *create_alias_ce(struct index_state *istate,
615616
new = xcalloc(1, cache_entry_size(len));
616617
memcpy(new->name, alias->name, len);
617618
copy_cache_entry(new, ce);
619+
new->precompute_hash_state = 0;
618620
save_or_free_index_entry(istate, ce);
619621
return new;
620622
}
@@ -1447,6 +1449,7 @@ static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *on
14471449
ce->ce_stat_data.sd_size = get_be32(&ondisk->size);
14481450
ce->ce_flags = flags & ~CE_NAMEMASK;
14491451
ce->ce_namelen = len;
1452+
ce->precompute_hash_state = 0;
14501453
ce->index = 0;
14511454
hashcpy(ce->oid.hash, ondisk->sha1);
14521455
memcpy(ce->name, name, len);

0 commit comments

Comments
 (0)