From c73592812d36ccc7eccfd0489177970d246fa522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 17 Mar 2013 10:28:06 +0700 Subject: Preallocate hash tables when the number of inserts are known in advance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano diff --git a/diffcore-rename.c b/diffcore-rename.c index 512d0ac..6c7a72f 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -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); diff --git a/hash.h b/hash.h index b875ce6..1d43ac0 100644 --- a/hash.h +++ b/hash.h @@ -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 diff --git a/name-hash.c b/name-hash.c index d8d25c2..2a1f108 100644 --- a/name-hash.c +++ b/name-hash.c @@ -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; -- cgit v0.10.2-6-g49f6