Skip to content

Commit

Permalink
lazy index hashing
Browse files Browse the repository at this point in the history
This delays the hashing of index names until it becomes necessary for
the first time.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Jan 23, 2008
1 parent cf55870 commit 9cb76b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ struct index_state {
struct cache_tree *cache_tree;
time_t timestamp;
void *alloc;
unsigned name_hash_initialized : 1;
struct hash_table name_hash;
};

Expand Down
26 changes: 23 additions & 3 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,36 @@ static unsigned int hash_name(const char *name, int namelen)
return hash;
}

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

istate->cache[nr] = ce;
pos = insert_hash(hash, ce, &istate->name_hash);
if (pos) {
ce->next = *pos;
*pos = ce;
}
}

static void lazy_init_name_hash(struct index_state *istate)
{
int nr;

if (istate->name_hash_initialized)
return;
for (nr = 0; nr < istate->cache_nr; nr++)
hash_index_entry(istate, istate->cache[nr]);
istate->name_hash_initialized = 1;
}

static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
{
istate->cache[nr] = ce;
if (istate->name_hash_initialized)
hash_index_entry(istate, ce);
}

/*
* We don't actually *remove* it, we can just mark it invalid so that
* we won't find it in lookups.
Expand Down Expand Up @@ -75,7 +92,10 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache
int index_name_exists(struct index_state *istate, const char *name, int namelen)
{
unsigned int hash = hash_name(name, namelen);
struct cache_entry *ce = lookup_hash(hash, &istate->name_hash);
struct cache_entry *ce;

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

while (ce) {
if (!(ce->ce_flags & CE_UNHASHED)) {
Expand Down

0 comments on commit 9cb76b8

Please sign in to comment.