[svn:parrot] r47847 - in branches/hash_faster: include/parrot src src/pmc

whiteknight at svn.parrot.org whiteknight at svn.parrot.org
Sat Jun 26 00:18:02 UTC 2010


Author: whiteknight
Date: Sat Jun 26 00:18:02 2010
New Revision: 47847
URL: https://trac.parrot.org/parrot/changeset/47847

Log:
[hash] rename hash->bi to bucket_indices, and ->bs to buckets. The former is an array of pointers arranged as a modulus of the hash values. The later appears to be raw slab-like bucket storage

Modified:
   branches/hash_faster/include/parrot/hash.h
   branches/hash_faster/src/hash.c
   branches/hash_faster/src/packfile.c
   branches/hash_faster/src/pmc/callcontext.pmc
   branches/hash_faster/src/pmc/hashiterator.pmc
   branches/hash_faster/src/pmc/lexinfo.pmc
   branches/hash_faster/src/pmc/unmanagedstruct.pmc

Modified: branches/hash_faster/include/parrot/hash.h
==============================================================================
--- branches/hash_faster/include/parrot/hash.h	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/include/parrot/hash.h	Sat Jun 26 00:18:02 2010	(r47847)
@@ -47,18 +47,38 @@
 } HashBucket;
 
 struct _hash {
-    HashBucket *bs;             /* store of buckets */
-    HashBucket **bi;            /* list of Bucket pointers */
-    HashBucket *free_list;      /* empty buckets */
-    UINTVAL entries;            /* Number of values stored in hashtable */
-    UINTVAL mask;               /* alloced - 1 */
-    PMC *container;             /* The owner PMC */
-    Hash_key_type key_type;     /* the type of key object this hash uses */
-    PARROT_DATA_TYPE entry_type;/* type of value */
-    size_t seed;                /* randomizes the hash_key generation
-                                   updated for each new hash */
-    hash_comp_fn   compare;     /* compare two keys, 0 = equal */
-    hash_hash_key_fn hash_val;  /* generate a hash value for key */
+    /* Large slab store of buckets */
+    HashBucket *buckets;
+
+    /* List of Bucket pointers */
+    HashBucket **bucket_indices;
+
+    /* Store for empty buckets */
+    HashBucket *free_list;
+
+    /* Number of values stored in hashtable */
+    UINTVAL entries;
+
+    /* alloced - 1 */
+    UINTVAL mask;
+
+    /* The owner PMC */
+    PMC *container;
+
+    /* The type of key object this hash uses */
+    Hash_key_type key_type;
+
+    /* Type of value */
+    PARROT_DATA_TYPE entry_type;
+
+    /* Random seed value for seeding hash algorithms */
+    size_t seed;
+
+    /* Comparison function pointer. Returns 0 if elements are equal */
+    hash_comp_fn   compare;
+
+    /* Function pointer to generate a hash value for the object */
+    hash_hash_key_fn hash_val;
 };
 
 typedef void (*value_free)(ARGFREE(void *));

Modified: branches/hash_faster/src/hash.c
==============================================================================
--- branches/hash_faster/src/hash.c	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/src/hash.c	Sat Jun 26 00:18:02 2010	(r47847)
@@ -14,7 +14,7 @@
 hashing functions can be set.
 
 This hash implementation uses just one piece of malloced memory. The
-C<< hash->bs >> bucket store points to this region.
+C<< hash->buckets >> bucket store points to this region.
 
 This hash doesn't move during GC, therefore a lot of the old caveats
 don't apply.
@@ -357,7 +357,7 @@
     if (a == b)
         return 0;
 
-    /* PMCs of different types are differ */
+    /* PMCs of different types are different */
     if (a->vtable->base_type != b->vtable->base_type)
         return 1;
 
@@ -427,8 +427,8 @@
 
 =item C<void parrot_mark_hash(PARROT_INTERP, Hash *hash)>
 
-Marks the hash and its contents as live.  Assumes that key and value are non
-null in all buckets.
+Marks the hash and its contents as live.  Assumes that key and value are
+non-null in all buckets.
 
 =cut
 
@@ -482,7 +482,7 @@
     INTVAL  i;
 
     for (i = hash->mask; i >= 0; --i) {
-        HashBucket *bucket = hash->bi[i];
+        HashBucket *bucket = hash->bucket_indices[i];
 
         while (bucket) {
             if (++found > entries)
@@ -518,7 +518,7 @@
     INTVAL  i;
 
     for (i = hash->mask; i >= 0; --i) {
-        HashBucket *bucket = hash->bi[i];
+        HashBucket *bucket = hash->bucket_indices[i];
 
         while (bucket) {
             if (++found > entries)
@@ -554,7 +554,7 @@
     INTVAL  i;
 
     for (i = hash->mask; i >= 0; --i) {
-        HashBucket *bucket = hash->bi[i];
+        HashBucket *bucket = hash->bucket_indices[i];
 
         while (bucket) {
             if (++found > entries)
@@ -678,7 +678,7 @@
     size_t           i;
 
     for (i = 0; i < hash->entries; ++i) {
-        HashBucket * const b = hash->bs+i;
+        HashBucket * const b = hash->buckets + i;
 
         switch (hash->key_type) {
           case Hash_key_type_int:
@@ -787,7 +787,7 @@
     HashBucket   *bs, *b, *new_mem;
     HashBucket * const old_offset = (HashBucket *)((char *)hash + sizeof (Hash));
 
-    void * const  old_mem    = hash->bs;
+    void * const  old_mem    = hash->buckets;
     const UINTVAL old_size   = hash->mask + 1;
     const UINTVAL new_size   = old_size << 1;
     const UINTVAL old_nb     = N_BUCKETS(old_size);
@@ -798,10 +798,10 @@
        e.g. 3 buckets, 4 pointers:
 
          +---+---+---+-+-+-+-+
-         | --> bs    | -> bi |
+         | --> buckets |     |
          +---+---+---+-+-+-+-+
-         ^           ^
-         | old_mem   | hash->bi
+         ^             ^
+         | old_mem     | hash->bucket_indices
     */
 
     /* resize mem */
@@ -819,10 +819,10 @@
 
     /*
          +---+---+---+---+---+---+-+-+-+-+-+-+-+-+
-         |  bs       | old_bi    |  new_bi       |
+         |  buckets  | old_bi    |  new_bi       |
          +---+---+---+---+---+---+-+-+-+-+-+-+-+-+
-           ^                       ^
-         | new_mem                 | hash->bi
+         ^                       ^
+         | new_mem               | hash->bucket_indices
     */
     bs     = new_mem;
     old_bi = (HashBucket **)(bs + old_nb);
@@ -835,8 +835,8 @@
     mem_sys_memmove(new_bi, old_bi, old_size * sizeof (HashBucket *));
 
     /* update hash data */
-    hash->bi   = new_bi;
-    hash->bs   = bs;
+    hash->bucket_indices = new_bi;
+    hash->buckets        = bs;
     hash->mask = new_size - 1;
 
     /* clear freshly allocated bucket index */
@@ -1027,12 +1027,6 @@
     hash->entries    = 0;
     hash->container  = PMCNULL;
 
-    /*
-     * TODO if we have a significant amount of small hashes:
-     * - allocate a bigger hash structure e.g. 128 byte
-     * - use the bucket store and bi inside this structure
-     * - when reallocate copy this part
-     */
     bp = (HashBucket *)((char *)alloc + sizeof (Hash));
     hash->free_list = NULL;
 
@@ -1040,9 +1034,9 @@
      * buckets[i] directly in an OrderedHash, *if* nothing
      * was deleted */
 
-    hash->bs  = bp;
-    bp       += N_BUCKETS(INITIAL_BUCKETS);
-    hash->bi  = (HashBucket **)bp;
+    hash->buckets = bp;
+    bp += N_BUCKETS(INITIAL_BUCKETS);
+    hash->bucket_indices = (HashBucket **)bp;
 
     for (i = 0, --bp; i < N_BUCKETS(INITIAL_BUCKETS); ++i, --bp) {
         bp->next        = hash->free_list;
@@ -1052,7 +1046,6 @@
     return hash;
 }
 
-
 /*
 
 =item C<void parrot_hash_destroy(PARROT_INTERP, Hash *hash)>
@@ -1072,8 +1065,8 @@
 {
     ASSERT_ARGS(parrot_hash_destroy)
     HashBucket * const bp = (HashBucket*)((char*)hash + sizeof (Hash));
-    if (bp != hash->bs)
-        mem_gc_free(interp, hash->bs);
+    if (bp != hash->buckets)
+        mem_gc_free(interp, hash->buckets);
     mem_gc_free(interp, hash);
 }
 
@@ -1096,7 +1089,7 @@
     UINTVAL i;
 
     for (i = 0; i <= hash->mask; ++i) {
-        HashBucket *bucket = hash->bi[i];
+        HashBucket *bucket = hash->bucket_indices[i];
         while (bucket) {
             mem_gc_free(interp, bucket->key);
             mem_gc_free(interp, bucket->value);
@@ -1130,7 +1123,7 @@
     UINTVAL i;
 
     for (i = 0; i <= hash->mask; ++i) {
-        HashBucket *bucket = hash->bi[i];
+        HashBucket *bucket = hash->bucket_indices[i];
         while (bucket) {
             mem_gc_free(interp, bucket->key);
             func(bucket->value);
@@ -1209,7 +1202,7 @@
 
     res = NULL;
 
-    for (b = hash->bs + i; i < size ; ++i, ++b) {
+    for (b = hash->buckets + i; i < size ; ++i, ++b) {
         /* XXX int keys may be zero - use different iterator */
         if (b->key) {
             if (!res)
@@ -1258,7 +1251,7 @@
         UINTVAL        i;
 
         for (i = 0; i < entries; ++i) {
-            HashBucket * const bucket = hash->bs + i;
+            HashBucket * const bucket = hash->buckets + i;
 
             /* the hash->compare cost is too high for this fast path */
             if (bucket->key == key)
@@ -1269,7 +1262,7 @@
     /* if the fast search didn't work, try the normal hashing search */
     {
         const UINTVAL hashval = (hash->hash_val)(interp, key, hash->seed);
-        HashBucket   *bucket  = hash->bi[hashval & hash->mask];
+        HashBucket   *bucket  = hash->bucket_indices[hashval & hash->mask];
 
         while (bucket) {
             /* key equality is always a match, so it's worth checking */
@@ -1350,7 +1343,7 @@
 {
     ASSERT_ARGS(parrot_hash_put)
     const UINTVAL hashval = (hash->hash_val)(interp, key, hash->seed);
-    HashBucket   *bucket  = hash->bi[hashval & hash->mask];
+    HashBucket   *bucket  = hash->bucket_indices[hashval & hash->mask];
 
     /* When the hash is constant, check that the key and value are also
      * constant. */
@@ -1388,8 +1381,8 @@
         hash->free_list                = bucket->next;
         bucket->key                    = key;
         bucket->value                  = value;
-        bucket->next                   = hash->bi[hashval & hash->mask];
-        hash->bi[hashval & hash->mask] = bucket;
+        bucket->next = hash->bucket_indices[hashval & hash->mask];
+        hash->bucket_indices[hashval & hash->mask] = bucket;
     }
 
     return bucket;
@@ -1415,13 +1408,13 @@
     HashBucket   *prev    = NULL;
     const UINTVAL hashval = (hash->hash_val)(interp, key, hash->seed) & hash->mask;
 
-    for (bucket = hash->bi[hashval]; bucket; bucket = bucket->next) {
+    for (bucket = hash->bucket_indices[hashval]; bucket; bucket = bucket->next) {
         if ((hash->compare)(interp, key, bucket->key) == 0) {
 
             if (prev)
                 prev->next = bucket->next;
             else
-                hash->bi[hashval] = bucket->next;
+                hash->bucket_indices[hashval] = bucket->next;
 
             --hash->entries;
             bucket->next    = hash->free_list;
@@ -1477,7 +1470,7 @@
 
     for (i = 0; i < entries; ++i) {
         void         *valtmp;
-        HashBucket   *b   = hash->bs+i;
+        HashBucket   *b   = hash->buckets + i;
         void * const  key = b->key;
 
         switch (hash->entry_type) {

Modified: branches/hash_faster/src/packfile.c
==============================================================================
--- branches/hash_faster/src/packfile.c	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/src/packfile.c	Sat Jun 26 00:18:02 2010	(r47847)
@@ -3223,7 +3223,7 @@
         return;
 
     for (i = 0; i <= hash->mask; ++i) {
-        HashBucket *bucket = hash->bi[i];
+        HashBucket *bucket = hash->bucket_indices[i];
 
         while (bucket) {
             PackFile_ConstTable * const table      =

Modified: branches/hash_faster/src/pmc/callcontext.pmc
==============================================================================
--- branches/hash_faster/src/pmc/callcontext.pmc	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/src/pmc/callcontext.pmc	Sat Jun 26 00:18:02 2010	(r47847)
@@ -377,7 +377,7 @@
     INTVAL  i;
 
     for (i = h->mask; i >= 0; --i) {
-        HashBucket *b = h->bi[i];
+        HashBucket *b = h->bucket_indices[i];
 
         while (b) {
             Parrot_gc_mark_STRING_alive(interp, (STRING *)b->key);
@@ -403,7 +403,7 @@
         result = Parrot_pmc_new_init_int(interp, enum_class_FixedStringArray, hash->entries);
 
         for (i = 0; i <= hash->mask; ++i) {
-            HashBucket *b = hash->bi[i];
+            HashBucket *b = hash->bucket_indices[i];
 
             while (b) {
                 VTABLE_set_string_keyed_int(interp, result,
@@ -600,7 +600,7 @@
             UINTVAL i;
 
             for (i = 0; i <= hash->mask; ++i) {
-                HashBucket *b = hash->bi[i];
+                HashBucket *b = hash->bucket_indices[i];
 
                 while (b) {
                     FREE_CELL(INTERP, (Pcc_cell *)b->value);
@@ -638,7 +638,7 @@
             UINTVAL i;
 
             for (i = 0; i <= hash->mask; ++i) {
-                HashBucket *b = hash->bi[i];
+                HashBucket *b = hash->bucket_indices[i];
 
                 while (b) {
                     FREE_CELL(INTERP, (Pcc_cell *)b->value);

Modified: branches/hash_faster/src/pmc/hashiterator.pmc
==============================================================================
--- branches/hash_faster/src/pmc/hashiterator.pmc	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/src/pmc/hashiterator.pmc	Sat Jun 26 00:18:02 2010	(r47847)
@@ -87,7 +87,7 @@
         if (attrs->pos == attrs->total_buckets)
             break;
 
-        bucket = attrs->parrot_hash->bi[attrs->pos++];
+        bucket = attrs->parrot_hash->bucket_indices[attrs->pos++];
     }
     attrs->bucket = bucket;
     --attrs->elements;

Modified: branches/hash_faster/src/pmc/lexinfo.pmc
==============================================================================
--- branches/hash_faster/src/pmc/lexinfo.pmc	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/src/pmc/lexinfo.pmc	Sat Jun 26 00:18:02 2010	(r47847)
@@ -105,7 +105,7 @@
             INTVAL  i;
 
             for (i = hash->mask; i >= 0; --i) {
-                HashBucket *bucket = hash->bi[i];
+                HashBucket *bucket = hash->bucket_indices[i];
                 while (bucket) {
                     if (++found > entries)
                         Parrot_ex_throw_from_c_args(INTERP, NULL, 1,

Modified: branches/hash_faster/src/pmc/unmanagedstruct.pmc
==============================================================================
--- branches/hash_faster/src/pmc/unmanagedstruct.pmc	Sat Jun 26 00:17:59 2010	(r47846)
+++ branches/hash_faster/src/pmc/unmanagedstruct.pmc	Sat Jun 26 00:18:02 2010	(r47847)
@@ -241,7 +241,7 @@
                 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_KEY_NOT_FOUND,
                     "key doesn't exist");
 
-            ix = b - hash->bs;
+            ix = b - hash->buckets;
         }
         else
             Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,


More information about the parrot-commits mailing list