Skip to content

Commit

Permalink
name-hash: remember previous dir_entry during lazy_init_name_hash
Browse files Browse the repository at this point in the history
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 <jeffhost@microsoft.com>
  • Loading branch information
jeffhostetler authored and dscho committed Nov 24, 2016
1 parent 4a64b91 commit 19aaf30
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions name-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static struct dir_entry *find_dir_entry(struct index_state *istate,
}

static struct dir_entry *hash_dir_entry(struct index_state *istate,
struct cache_entry *ce, int namelen)
struct cache_entry *ce, int namelen, struct dir_entry **p_previous_dir)
{
/*
* Throw each directory component in the hash for quick lookup
Expand Down Expand Up @@ -70,9 +70,21 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
namelen--;

/* lookup existing entry for that directory */
if (!use_precomputed_dir_hash)
hash = memihash(ce->name, namelen);
dir = find_dir_entry__hash(istate, ce->name, namelen, hash);
if (p_previous_dir && *p_previous_dir
&& namelen == (*p_previous_dir)->namelen
&& memcmp(ce->name, (*p_previous_dir)->name, namelen) == 0) {
/*
* When our caller is sequentially iterating thru the index,
* items in the same directory will be sequential, and therefore
* refer to the same dir_entry.
*/
dir = *p_previous_dir;
} else {
if (!use_precomputed_dir_hash)
hash = memihash(ce->name, namelen);
dir = find_dir_entry__hash(istate, ce->name, namelen, hash);
}

if (!dir) {
/* not found, create it and add to hash table */
FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
Expand All @@ -81,15 +93,20 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
hashmap_add(&istate->dir_hash, dir);

/* recursively add missing parent directories */
dir->parent = hash_dir_entry(istate, ce, namelen);
dir->parent = hash_dir_entry(istate, ce, namelen, NULL);
}

if (p_previous_dir)
*p_previous_dir = dir;

return dir;
}

static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
static void add_dir_entry(struct index_state *istate, struct cache_entry *ce,
struct dir_entry **p_previous_dir)
{
/* Add reference to the directory entry (and parents if 0). */
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce), p_previous_dir);
while (dir && !(dir->nr++))
dir = dir->parent;
}
Expand All @@ -100,7 +117,7 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
* Release reference to the directory entry. If 0, remove and continue
* with parent directory.
*/
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce), NULL);
while (dir && !(--dir->nr)) {
struct dir_entry *parent = dir->parent;
hashmap_remove(&istate->dir_hash, dir, NULL);
Expand All @@ -109,7 +126,8 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
}
}

static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce,
struct dir_entry **p_previous_dir)
{
unsigned int h;

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

if (ignore_case)
add_dir_entry(istate, ce);
add_dir_entry(istate, ce, p_previous_dir);
}

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

static void lazy_init_name_hash(struct index_state *istate)
{
struct dir_entry *previous_dir = NULL;
int nr;

if (istate->name_hash_initialized)
Expand All @@ -151,14 +170,14 @@ static void lazy_init_name_hash(struct index_state *istate)
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
istate->cache_nr);
for (nr = 0; nr < istate->cache_nr; nr++)
hash_index_entry(istate, istate->cache[nr]);
hash_index_entry(istate, istate->cache[nr], &previous_dir);
istate->name_hash_initialized = 1;
}

void add_name_hash(struct index_state *istate, struct cache_entry *ce)
{
if (istate->name_hash_initialized)
hash_index_entry(istate, ce);
hash_index_entry(istate, ce, NULL);
}

void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
Expand Down

0 comments on commit 19aaf30

Please sign in to comment.