Skip to content

Commit ba40579

Browse files
jeffhostetlerGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
name-hash: remember previous dir_entry during lazy_init_name_hash
Teach hash_dir_entry() to remember the previously found dir_entry during lazy_init_name_hash() iteration. This is a performance optimization. Since items in the index array are sorted by full pathname, adjacent items are likely to be in the same directory. This can save memihash() computations and HashMap lookups. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 821b7f6 commit ba40579

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

name-hash.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static struct dir_entry *find_dir_entry(struct index_state *istate,
3939
}
4040

4141
static struct dir_entry *hash_dir_entry(struct index_state *istate,
42-
struct cache_entry *ce, int namelen)
42+
struct cache_entry *ce, int namelen, struct dir_entry **p_previous_dir)
4343
{
4444
/*
4545
* Throw each directory component in the hash for quick lookup
@@ -70,9 +70,21 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
7070
namelen--;
7171

7272
/* lookup existing entry for that directory */
73-
if (!use_precomputed_dir_hash)
74-
hash = memihash(ce->name, namelen);
75-
dir = find_dir_entry__hash(istate, ce->name, namelen, hash);
73+
if (p_previous_dir && *p_previous_dir
74+
&& namelen == (*p_previous_dir)->namelen
75+
&& memcmp(ce->name, (*p_previous_dir)->name, namelen) == 0) {
76+
/*
77+
* When our caller is sequentially iterating thru the index,
78+
* items in the same directory will be sequential, and therefore
79+
* refer to the same dir_entry.
80+
*/
81+
dir = *p_previous_dir;
82+
} else {
83+
if (!use_precomputed_dir_hash)
84+
hash = memihash(ce->name, namelen);
85+
dir = find_dir_entry__hash(istate, ce->name, namelen, hash);
86+
}
87+
7688
if (!dir) {
7789
/* not found, create it and add to hash table */
7890
FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
@@ -81,15 +93,20 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
8193
hashmap_add(&istate->dir_hash, dir);
8294

8395
/* recursively add missing parent directories */
84-
dir->parent = hash_dir_entry(istate, ce, namelen);
96+
dir->parent = hash_dir_entry(istate, ce, namelen, NULL);
8597
}
98+
99+
if (p_previous_dir)
100+
*p_previous_dir = dir;
101+
86102
return dir;
87103
}
88104

89-
static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
105+
static void add_dir_entry(struct index_state *istate, struct cache_entry *ce,
106+
struct dir_entry **p_previous_dir)
90107
{
91108
/* Add reference to the directory entry (and parents if 0). */
92-
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
109+
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce), p_previous_dir);
93110
while (dir && !(dir->nr++))
94111
dir = dir->parent;
95112
}
@@ -100,7 +117,7 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
100117
* Release reference to the directory entry. If 0, remove and continue
101118
* with parent directory.
102119
*/
103-
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
120+
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce), NULL);
104121
while (dir && !(--dir->nr)) {
105122
struct dir_entry *parent = dir->parent;
106123
hashmap_remove(&istate->dir_hash, dir, NULL);
@@ -109,7 +126,8 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
109126
}
110127
}
111128

112-
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
129+
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce,
130+
struct dir_entry **p_previous_dir)
113131
{
114132
unsigned int h;
115133

@@ -126,7 +144,7 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
126144
hashmap_add(&istate->name_hash, ce);
127145

128146
if (ignore_case)
129-
add_dir_entry(istate, ce);
147+
add_dir_entry(istate, ce, p_previous_dir);
130148
}
131149

132150
static int cache_entry_cmp(const struct cache_entry *ce1,
@@ -142,6 +160,7 @@ static int cache_entry_cmp(const struct cache_entry *ce1,
142160

143161
static void lazy_init_name_hash(struct index_state *istate)
144162
{
163+
struct dir_entry *previous_dir = NULL;
145164
int nr;
146165

147166
if (istate->name_hash_initialized)
@@ -151,14 +170,14 @@ static void lazy_init_name_hash(struct index_state *istate)
151170
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
152171
istate->cache_nr);
153172
for (nr = 0; nr < istate->cache_nr; nr++)
154-
hash_index_entry(istate, istate->cache[nr]);
173+
hash_index_entry(istate, istate->cache[nr], &previous_dir);
155174
istate->name_hash_initialized = 1;
156175
}
157176

158177
void add_name_hash(struct index_state *istate, struct cache_entry *ce)
159178
{
160179
if (istate->name_hash_initialized)
161-
hash_index_entry(istate, ce);
180+
hash_index_entry(istate, ce, NULL);
162181
}
163182

164183
void remove_name_hash(struct index_state *istate, struct cache_entry *ce)

0 commit comments

Comments
 (0)