Skip to content

Commit 9cb76b8

Browse files
committed
lazy index hashing
This delays the hashing of index names until it becomes necessary for the first time. Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf55870 commit 9cb76b8

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ struct index_state {
191191
struct cache_tree *cache_tree;
192192
time_t timestamp;
193193
void *alloc;
194+
unsigned name_hash_initialized : 1;
194195
struct hash_table name_hash;
195196
};
196197

read-cache.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,36 @@ static unsigned int hash_name(const char *name, int namelen)
3434
return hash;
3535
}
3636

37-
static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
37+
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
3838
{
3939
void **pos;
4040
unsigned int hash = hash_name(ce->name, ce_namelen(ce));
4141

42-
istate->cache[nr] = ce;
4342
pos = insert_hash(hash, ce, &istate->name_hash);
4443
if (pos) {
4544
ce->next = *pos;
4645
*pos = ce;
4746
}
4847
}
4948

49+
static void lazy_init_name_hash(struct index_state *istate)
50+
{
51+
int nr;
52+
53+
if (istate->name_hash_initialized)
54+
return;
55+
for (nr = 0; nr < istate->cache_nr; nr++)
56+
hash_index_entry(istate, istate->cache[nr]);
57+
istate->name_hash_initialized = 1;
58+
}
59+
60+
static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
61+
{
62+
istate->cache[nr] = ce;
63+
if (istate->name_hash_initialized)
64+
hash_index_entry(istate, ce);
65+
}
66+
5067
/*
5168
* We don't actually *remove* it, we can just mark it invalid so that
5269
* we won't find it in lookups.
@@ -75,7 +92,10 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache
7592
int index_name_exists(struct index_state *istate, const char *name, int namelen)
7693
{
7794
unsigned int hash = hash_name(name, namelen);
78-
struct cache_entry *ce = lookup_hash(hash, &istate->name_hash);
95+
struct cache_entry *ce;
96+
97+
lazy_init_name_hash(istate);
98+
ce = lookup_hash(hash, &istate->name_hash);
7999

80100
while (ce) {
81101
if (!(ce->ce_flags & CE_UNHASHED)) {

0 commit comments

Comments
 (0)