[svn:parrot] r39594 - trunk/src

Infinoid at svn.parrot.org Infinoid at svn.parrot.org
Tue Jun 16 21:58:13 UTC 2009


Author: Infinoid
Date: Tue Jun 16 21:58:11 2009
New Revision: 39594
URL: https://trac.parrot.org/parrot/changeset/39594

Log:
[core] Cut the number of Hash internal structure allocations in half.  (This is the patch from TT #726.)

Modified:
   trunk/src/hash.c

Modified: trunk/src/hash.c
==============================================================================
--- trunk/src/hash.c	Tue Jun 16 20:20:37 2009	(r39593)
+++ trunk/src/hash.c	Tue Jun 16 21:58:11 2009	(r39594)
@@ -720,6 +720,7 @@
     HashBucket   *bs, *b;
 
     void * const  old_mem  = hash->bs;
+    HashBucket *old_offset = (HashBucket*)((char*)hash + sizeof (Hash));
     const UINTVAL old_size = hash->mask + 1;
     const UINTVAL new_size = old_size << 1;
     const UINTVAL old_nb   = N_BUCKETS(old_size);
@@ -737,8 +738,16 @@
     */
 
     /* resize mem */
-    HashBucket * const new_mem =
-        (HashBucket *)mem_sys_realloc(old_mem, HASH_ALLOC_SIZE(new_size));
+    HashBucket *new_mem;
+    if (old_offset != old_mem) {
+        /* This buffer has been reallocated at least once before. */
+        new_mem = (HashBucket *)mem_sys_realloc(old_mem, HASH_ALLOC_SIZE(new_size));
+    }
+    else {
+        /* Allocate a new buffer. */
+        new_mem = (HashBucket *)mem_sys_allocate(HASH_ALLOC_SIZE(new_size));
+        memcpy(new_mem, old_mem, HASH_ALLOC_SIZE(old_size));
+    }
 
     /*
          +---+---+---+---+---+---+-+-+-+-+-+-+-+-+
@@ -954,7 +963,8 @@
 {
     ASSERT_ARGS(parrot_create_hash)
     HashBucket  *bp;
-    Hash * const hash = mem_allocate_typed(Hash);
+    void        *alloc = mem_sys_allocate(sizeof (Hash) + HASH_ALLOC_SIZE(INITIAL_BUCKETS));
+    Hash * const hash  = (Hash*)alloc;
     size_t       i;
 
     PARROT_ASSERT(INITIAL_BUCKETS % 4 == 0);
@@ -974,7 +984,7 @@
      * - use the bucket store and bi inside this structure
      * - when reallocate copy this part
      */
-    bp = (HashBucket *)mem_sys_allocate(HASH_ALLOC_SIZE(INITIAL_BUCKETS));
+    bp = (HashBucket *)((char*)alloc + sizeof (Hash));
     hash->free_list = NULL;
 
     /* fill free_list from hi addresses so that we can use
@@ -1015,7 +1025,9 @@
 parrot_hash_destroy(SHIM_INTERP, ARGMOD(Hash *hash))
 {
     ASSERT_ARGS(parrot_hash_destroy)
-    mem_sys_free(hash->bs);
+    HashBucket *bp = (HashBucket*)((char*)hash + sizeof (Hash));
+    if (bp != hash->bs)
+        mem_sys_free(hash->bs);
     mem_sys_free(hash);
 }
 


More information about the parrot-commits mailing list