[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[hurd] 44/61: libihash: add hurd_ihash_get_load
From: |
Samuel Thibault |
Subject: |
[hurd] 44/61: libihash: add hurd_ihash_get_load |
Date: |
Tue, 27 May 2014 08:32:13 +0000 |
This is an automated email from the git hooks/post-receive script.
sthibault pushed a commit to branch upstream
in repository hurd.
commit 689b3f9b8fb68218515c05b3b7b13ff4e21c6c76
Author: Justus Winter <address@hidden>
Date: Thu May 15 13:53:16 2014 +0200
libihash: add hurd_ihash_get_load
* libihash/ihash.c (hurd_ihash_add): Move the code computing the load
factor of the hash table...
* libihash/ihash.h (hurd_ihash_get_load): ... here, together with the
comment describing the method and the rationale for chosing binary
percent.
---
libihash/ihash.c | 20 ++------------------
libihash/ihash.h | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+), 18 deletions(-)
diff --git a/libihash/ihash.c b/libihash/ihash.c
index f20ba61..4d9cc18 100644
--- a/libihash/ihash.c
+++ b/libihash/ihash.c
@@ -273,24 +273,8 @@ hurd_ihash_add (hurd_ihash_t ht, hurd_ihash_key_t key,
hurd_ihash_value_t item)
if (ht->size)
{
- /* Only fill the hash table up to its maximum load factor given
- as "binary percent", where 128b% corresponds to 100%. As the
- size is always a power of two, and 128 is also, the quotient
- of both is also a power of two. Therefore, we can use bit
- shifts to scale the number of items.
-
- load = nr_items * 128 / size
- = nr_items * 2^{log2 (128) - log2 (size)}
- = nr_items >> (log2 (size) - log2 (128))
- -- if size >= 128
- = nr_items << (log2 (128) - log2 (size))
- -- otherwise
- */
- int d = __builtin_ctzl (ht->size) - 7;
- unsigned int load = d >= 0
- ? ht->nr_items >> d
- : ht->nr_items << -d;
- if (load <= ht->max_load)
+ /* Only fill the hash table up to its maximum load factor. */
+ if (hurd_ihash_get_load (ht) <= ht->max_load)
if (add_one (ht, key, item))
return 0;
}
diff --git a/libihash/ihash.h b/libihash/ihash.h
index 809166f..345630d 100644
--- a/libihash/ihash.h
+++ b/libihash/ihash.h
@@ -160,6 +160,30 @@ void hurd_ihash_set_cleanup (hurd_ihash_t ht,
hurd_ihash_cleanup_t cleanup,
void hurd_ihash_set_max_load (hurd_ihash_t ht, unsigned int max_load);
+/* Get the current load factor of HT in binary percent, where 128b%
+ corresponds to 100%. The reason we do this is that it is so
+ efficient to compute:
+
+ As the size is always a power of two, and 128 is also, the quotient
+ of both is also a power of two. Therefore, we can use bit shifts
+ to scale the number of items.
+
+ load = nr_items * 128 / size
+ = nr_items * 2^{log2 (128) - log2 (size)}
+ = nr_items >> (log2 (size) - log2 (128))
+ -- if size >= 128
+ = nr_items << (log2 (128) - log2 (size))
+ -- otherwise
+
+ If you want to convert this to percent, just divide by 1.28. */
+static inline unsigned int
+hurd_ihash_get_load (hurd_ihash_t ht)
+{
+ int d = __builtin_ctzl (ht->size) - 7;
+ return d >= 0 ? ht->nr_items >> d : ht->nr_items << -d;
+}
+
+
/* Add ITEM to the hash table HT under the key KEY. If there already
is an item under this key, call the cleanup function (if any) for
it before overriding the value. If a memory allocation error
--
Alioth's /usr/local/bin/git-commit-notice on
/srv/git.debian.org/git/pkg-hurd/hurd.git
- [hurd] 41/61: trans/fakeroot: use C99-style struct initialization, (continued)
- [hurd] 41/61: trans/fakeroot: use C99-style struct initialization, Samuel Thibault, 2014/05/27
- [hurd] 40/61: trans/fakeroot: fix comparison between signed and unsigned, Samuel Thibault, 2014/05/27
- [hurd] 32/61: fatfs: use two distinct pager buckets for the disk and file pager, Samuel Thibault, 2014/05/27
- [hurd] 24/61: ext2fs: simplify expression, Samuel Thibault, 2014/05/27
- [hurd] 47/61: include: install refcount.h, Samuel Thibault, 2014/05/27
- [hurd] 50/61: exec: add missing includes, Samuel Thibault, 2014/05/27
- [hurd] 23/61: ext2fs: fix type of inum, Samuel Thibault, 2014/05/27
- [hurd] 22/61: exec: abbreviate the task name if necessary, Samuel Thibault, 2014/05/27
- [hurd] 46/61: trans/fakeroot: override fshelp_isowner, Samuel Thibault, 2014/05/27
- [hurd] 37/61: libihash: use linear probing and fast modulo operation, Samuel Thibault, 2014/05/27
- [hurd] 44/61: libihash: add hurd_ihash_get_load,
Samuel Thibault <=
- [hurd] 52/61: libdiskfs: fix type of dir_cache_id, node_cache_id, Samuel Thibault, 2014/05/27
- [hurd] 54/61: term: fix memory leak, Samuel Thibault, 2014/05/27
- [hurd] 48/61: Merge remote-tracking branch 'upstream/master' into upstream, Samuel Thibault, 2014/05/27
- [hurd] 25/61: libports: reduce malloc overhead in _ports_bucket_class_iterate, Samuel Thibault, 2014/05/27
- [hurd] 42/61: proc: move translation functions to mig-decls.h, Samuel Thibault, 2014/05/27
- [hurd] 31/61: libports: unlock _ports_lock on malloc failure, Samuel Thibault, 2014/05/27
- [hurd] 61/61: Merge remote-tracking branch 'upstream/master' into upstream, Samuel Thibault, 2014/05/27
- [hurd] 43/61: libihash: fix typo, Samuel Thibault, 2014/05/27
- [hurd] 56/61: ext2fs: fix diskfs_pager_users, Samuel Thibault, 2014/05/27
- [hurd] 57/61: trans/mtab: fix initialization, Samuel Thibault, 2014/05/27