Tweaks to hostfile performance work.

This commit is contained in:
Simon Kelley
2012-01-11 22:00:48 +00:00
parent 205fafa577
commit 915363f976

View File

@@ -660,9 +660,7 @@ static void add_hosts_entry(struct crec *cache, struct all_addr *addr, int addrl
whilst reading hosts files: the buckets are then freed, and the whilst reading hosts files: the buckets are then freed, and the
->next pointer used for other things. ->next pointer used for other things.
We search and bail at the first matching address that came from Only insert each unique address one into this hashing structure.
a HOSTS file. Since the first host entry gets reverse, we know
then that it must exist without searching exhaustively for it.
This complexity avoids O(n^2) divergent CPU use whilst reading This complexity avoids O(n^2) divergent CPU use whilst reading
large (10000 entry) hosts files. */ large (10000 entry) hosts files. */
@@ -671,7 +669,9 @@ static void add_hosts_entry(struct crec *cache, struct all_addr *addr, int addrl
for (j = 0, i = 0; i < addrlen; i++) for (j = 0, i = 0; i < addrlen; i++)
j += ((unsigned char *)addr)[i] + (j << 6) + (j << 16) - j; j += ((unsigned char *)addr)[i] + (j << 6) + (j << 16) - j;
for (lookup = rhash[j % RHASHSIZE]; lookup; lookup = lookup->next) j = j % RHASHSIZE;
for (lookup = rhash[j]; lookup; lookup = lookup->next)
if ((lookup->flags & F_HOSTS) && if ((lookup->flags & F_HOSTS) &&
(lookup->flags & flags & (F_IPV4 | F_IPV6)) && (lookup->flags & flags & (F_IPV4 | F_IPV6)) &&
memcmp(&lookup->addr.addr, addr, addrlen) == 0) memcmp(&lookup->addr.addr, addr, addrlen) == 0)
@@ -680,15 +680,16 @@ static void add_hosts_entry(struct crec *cache, struct all_addr *addr, int addrl
break; break;
} }
/* maintain address hash chain, insert new unique address */
if (!lookup)
{
cache->next = rhash[j];
rhash[j] = cache;
}
cache->flags = flags; cache->flags = flags;
cache->uid = index; cache->uid = index;
memcpy(&cache->addr.addr, addr, addrlen);
/* maintain address has chain */
cache->next = rhash[j % RHASHSIZE];
rhash[j % RHASHSIZE] = cache;
memcpy(&cache->addr.addr, addr, addrlen);
cache_hash(cache); cache_hash(cache);
/* don't need to do alias stuff for second and subsequent addresses. */ /* don't need to do alias stuff for second and subsequent addresses. */