Defer allocation as long as possible.  Make logic parallel.

Index: stacks.c
===================================================================
RCS file: /home/perlcvs/parrot/stacks.c,v
retrieving revision 1.23
diff -u -r1.23 stacks.c
--- stacks.c    8 Mar 2002 03:04:03 -0000       1.23
+++ stacks.c    22 Mar 2002 06:45:39 -0000
@@ -108,7 +108,21 @@
            void *thing, INTVAL type, stack_cleanup_method_t cleanup)
 {
     Stack_Chunk chunk = stack->prev;
-    Stack_Entry entry = &chunk->entry[chunk->used];
+    Stack_Entry entry;
+
+    /* Do we need a new chunk? */
+    if (chunk->used == STACK_CHUNK_DEPTH) {
+        /* Need to add a new chunk */
+        Stack_Chunk new_chunk = mem_allocate_aligned(sizeof(*new_chunk));
+        new_chunk->used = 0;
+        new_chunk->next = stack;
+        new_chunk->prev = chunk;
+        chunk->next = new_chunk;
+        stack->prev = new_chunk;
+        chunk = new_chunk;
+    }
+
+    entry = &chunk->entry[chunk->used];

     /* Remember the type */
     entry->entry_type = type;
@@ -139,16 +153,7 @@
         break;
     }

-    /* Register the new entry */
-    if (++chunk->used == STACK_CHUNK_DEPTH) {
-        /* Need to add a new chunk */
-        Stack_Chunk new_chunk = mem_allocate_aligned(sizeof(*new_chunk));
-        new_chunk->used = 0;
-        new_chunk->next = stack;
-        new_chunk->prev = chunk;
-        chunk->next = new_chunk;
-        stack->prev = new_chunk;
-    }
+    chunk->used++;
 }

 /* Pop off an entry and return a pointer to the contents */
@@ -176,7 +181,10 @@
         internal_exception(ERROR_STACK_EMPTY, "No entries on stack!\n");
     }

-    entry = &chunk->entry[chunk->used - 1];
+    /* Now decrement the SP */
+    chunk->used--;
+
+    entry = &chunk->entry[chunk->used];

     /* Types of 0 mean we don't care */
     if (type && entry->entry_type != type) {
@@ -189,8 +197,6 @@
         (*entry->cleanup) (entry);
     }

-    /* Now decrement the SP */
-    chunk->used--;

     /* Sometimes the caller doesn't care what the value was */
     if (where == NULL)
-- 
Bryan C. Warnock
[EMAIL PROTECTED]

Reply via email to