Skip to content

Commit

Permalink
Preallocate hash tables when the number of inserts are known in advance
Browse files Browse the repository at this point in the history
This avoids unnecessary re-allocations and reinsertions. On webkit.git
(i.e. about 182k inserts to the name hash table), this reduces about
100ms out of 3s user time.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pclouds authored and gitster committed Mar 17, 2013
1 parent 03a0fb0 commit c735928
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions diffcore-rename.c
Expand Up @@ -389,6 +389,7 @@ static int find_exact_renames(struct diff_options *options)
struct hash_table file_table;

init_hash(&file_table);
preallocate_hash(&file_table, rename_src_nr + rename_dst_nr);
for (i = 0; i < rename_src_nr; i++)
insert_file_table(&file_table, -1, i, rename_src[i].p->one);

Expand Down
7 changes: 7 additions & 0 deletions hash.h
Expand Up @@ -40,4 +40,11 @@ static inline void init_hash(struct hash_table *table)
table->array = NULL;
}

static inline void preallocate_hash(struct hash_table *table, unsigned int elts)
{
assert(table->size == 0 && table->nr == 0 && table->array == NULL);
table->size = elts * 2;
table->array = xcalloc(sizeof(struct hash_table_entry), table->size);
}

#endif
2 changes: 2 additions & 0 deletions name-hash.c
Expand Up @@ -92,6 +92,8 @@ static void lazy_init_name_hash(struct index_state *istate)

if (istate->name_hash_initialized)
return;
if (istate->cache_nr)
preallocate_hash(&istate->name_hash, istate->cache_nr);
for (nr = 0; nr < istate->cache_nr; nr++)
hash_index_entry(istate, istate->cache[nr]);
istate->name_hash_initialized = 1;
Expand Down

0 comments on commit c735928

Please sign in to comment.