@matheusbsbs / matheus-bagliano / commits / 9cb76b8cdc
Fork 0

lazy index hashing

This delays the hashing of index names until it becomes necessary for the first time. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 22, 2008 at 23:01 UTC 9cb76b8cdc8ac62a77080595f6443613fd64bab3
2 files changed +24 -3
cache.h
+1
@@ -191,6 +191,7 @@ struct index_state {
191 struct cache_tree *cache_tree;
192 time_t timestamp;
193 void *alloc;
194 + unsigned name_hash_initialized : 1;
195 struct hash_table name_hash;
196 };
197
read-cache.c
+23 -3
@@ -34,12 +34,11 @@ static unsigned int hash_name(const char *name, int namelen)
34 return hash;
35 }
36
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)
38 {
39 void **pos;
40 unsigned int hash = hash_name(ce->name, ce_namelen(ce));
41
42 - istate->cache[nr] = ce;
42 pos = insert_hash(hash, ce, &istate->name_hash);
43 if (pos) {
44 ce->next = *pos;
@@ -47,6 +46,24 @@ static void set_index_entry(struct index_state *istate, int nr, struct cache_ent
46 }
47 }
48
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 +
67 /*
68 * We don't actually *remove* it, we can just mark it invalid so that
69 * we won't find it in lookups.
@@ -75,7 +92,10 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache
92 int index_name_exists(struct index_state *istate, const char *name, int namelen)
93 {
94 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);
99
100 while (ce) {
101 if (!(ce->ce_flags & CE_UNHASHED)) {