This patch changes a variety of small things related to our pkghash
implementation with an eye toward performance, especially on native
32-bit systems.
* Use `unsigned int` rather than `size_t` for hash sizes. We already
return ERANGE for any attempted creation of a hash greater than 1
million elements, so unsigned int is more than large enough for our
purposes. Switching to this type allows 32 bit systems to do native
math without helper functions from libgcc.
* _alpm_pkghash_create() now internally adds extra padding for
additional array elements, rather than that being the responsibility of
the caller.
* #define values are moved into static const values in pkghash.c; a new
`stride` value is also extracted (but remains set at 1).
* Division and modulus operators are removed from the normal find and
add paths if possible. We store the upper limit of the number of
elements in the hash so we no longer need to calculate this every
element addition. When doing wraparound position calculations, we only
apply the modulus operator if the value is greater than the number of
buckets.
-/* Allocate a hash table with at least "size" buckets */
-alpm_pkghash_t *_alpm_pkghash_create(size_t size)
+/* How far forward do we look when linear probing for a spot? */
+static const unsigned int stride = 1;
+/* What is the maximum load percentage of our hash table? */
+static const double max_hash_load = 0.68;
+/* Initial load percentage given a certain size */
+static const double initial_hash_load = 0.58;
+
+/* Allocate a hash table with space for at least "size" elements */
+alpm_pkghash_t *_alpm_pkghash_create(unsigned int size)
{
alpm_pkghash_t *hash = NULL;
- size_t i, loopsize;
+ unsigned int i, loopsize;
/* Hash tables will need resized in two cases:
* - adding packages to the local database
@@ -129,8 +142,7 @@ static alpm_pkghash_t *rehash(alpm_pkghash_t *oldhash)
for(i = 0; i < oldhash->buckets; i++) {
if(oldhash->hash_table[i] != NULL) {
alpm_pkg_t *package = oldhash->hash_table[i]->data;
-
- position = get_hash_position(package->name_hash, newhash);
+ unsigned int position = get_hash_position(package->name_hash, newhash);
-static size_t move_one_entry(alpm_pkghash_t *hash, size_t start, size_t end)
+static unsigned int move_one_entry(alpm_pkghash_t *hash,
+ unsigned int start, unsigned int end)
{
/* Iterate backwards from 'end' to 'start', seeing if any of the items
* would hash to 'start'. If we find one, we move it there and break. If
@@ -201,7 +215,7 @@ static size_t move_one_entry(alpm_pkghash_t *hash, size_t start, size_t end)
while(end != start) {
alpm_list_t *i = hash->hash_table[end];
alpm_pkg_t *info = i->data;
- size_t new_position = get_hash_position(info->name_hash, hash);
+ unsigned int new_position = get_hash_position(info->name_hash, hash);
if(new_position == start) {
hash->hash_table[start] = i;
@@ -212,7 +226,7 @@ static size_t move_one_entry(alpm_pkghash_t *hash, size_t start, size_t end)
/* the odd math ensures we are always positive, e.g.
* e.g. (0 - 1) % 47 == -1
* e.g. (47 + 0 - 1) % 47 == 46 */
- end = (hash->buckets + end - 1) % hash->buckets;
+ end = (hash->buckets + end - stride) % hash->buckets;
}
return end;
}
@@ -230,7 +244,7 @@ alpm_pkghash_t *_alpm_pkghash_remove(alpm_pkghash_t *hash, alpm_pkg_t *pkg,
alpm_pkg_t **data)
{
alpm_list_t *i;
- size_t position;
+ unsigned int position;
/* remove from list and hash */
hash->list = alpm_list_remove_item(hash->list, i);
@@ -260,11 +274,17 @@ alpm_pkghash_t *_alpm_pkghash_remove(alpm_pkghash_t *hash, alpm_pkg_t *pkg,
/* Potentially move entries following removed entry to keep open
* addressing collision resolution working. We start by finding the
* next null bucket to know how far we have to look. */
- stop = (position + 1) % hash->buckets;
+ stop = position + stride;
+ if(stop >= hash->buckets) {
+ stop %= hash->buckets;
+ }
while(hash->hash_table[stop] != NULL && stop != position) {
- stop = (stop + 1) % hash->buckets;
+ stop += stride;
+ if(stop >= hash->buckets) {
+ stop %= hash->buckets;
+ }
}
- stop = (hash->buckets + stop - 1) % hash->buckets;
+ stop = (hash->buckets + stop - stride) % hash->buckets;
/* We now search backwards from stop to position. If we find an
* item that now hashes to position, we will move it, and then try
@@ -277,7 +297,10 @@ alpm_pkghash_t *_alpm_pkghash_remove(alpm_pkghash_t *hash, alpm_pkg_t *pkg,
return hash;
}
- position = (position + 1) % hash->buckets;
+ position += stride;
+ if(position >= hash->buckets) {
+ position %= hash->buckets;
+ }
}
- position = (position + 1) % hash->buckets;
+ position += stride;
+ if(position >= hash->buckets) {
+ position %= hash->buckets;
+ }
}
return NULL;
diff --git a/lib/libalpm/pkghash.h b/lib/libalpm/pkghash.h
index edd500e..64a32b2 100644
--- a/lib/libalpm/pkghash.h
+++ b/lib/libalpm/pkghash.h
@@ -35,17 +35,19 @@
struct __alpm_pkghash_t {
/** data held by the hash table */
alpm_list_t **hash_table;
- /** number of buckets in hash table */
- size_t buckets;
- /** number of entries in hash table */
- size_t entries;
/** head node of the hash table data in normal list format */
alpm_list_t *list;
+ /** number of buckets in hash table */
+ unsigned int buckets;
+ /** number of entries in hash table */
+ unsigned int entries;
+ /** max number of entries before a resize is needed */
+ unsigned int limit;
};
typedef struct __alpm_pkghash_t alpm_pkghash_t;
-alpm_pkghash_t *_alpm_pkghash_create(size_t size);
+alpm_pkghash_t *_alpm_pkghash_create(unsigned int size);