[pypy-commit] stmgc c7: over-allocate lists

Remi Meier noreply at buildbot.pypy.org
Thu Jan 30 15:24:28 CET 2014


Author: Remi Meier
Branch: c7
Changeset: r691:0a4507399ac1
Date: 2014-01-30 15:24 +0100
http://bitbucket.org/pypy/stmgc/changeset/0a4507399ac1/

Log:	over-allocate lists

diff --git a/duhton/listobject.c b/duhton/listobject.c
--- a/duhton/listobject.c
+++ b/duhton/listobject.c
@@ -8,6 +8,7 @@
 typedef TLPREFIX struct DuTupleObject_s {
     DuOBJECT_HEAD1
     int ob_count;
+    int ob_capacity;
     DuObject *ob_items[1];
 } DuTupleObject;
 
@@ -27,7 +28,7 @@
 
 size_t tuple_bytesize(struct DuTupleObject_s *ob)
 {
-    return sizeof(DuTupleObject) + (ob->ob_count - 1) * sizeof(DuObject *);
+    return sizeof(DuTupleObject) + (ob->ob_capacity - 1) * sizeof(DuObject *);
 }
 
 void list_trace(struct DuListObject_s *ob, void visit(object_t **))
@@ -35,6 +36,7 @@
     visit((object_t **)&ob->ob_tuple);
 }
 
+
 void list_print(DuListObject *ob)
 {
     int i;
@@ -71,9 +73,15 @@
     ob = (DuTupleObject *)stm_allocate(size);
     ob->ob_base.type_id = DUTYPE_TUPLE;
     ob->ob_count = length;
+    ob->ob_capacity = length;
     return ob;
 }
 
+int overallocated_size(int size)
+{
+    return size + (size >> 3) + (size < 9 ? 3 : 6);
+}
+
 void _list_append(DuListObject *ob, DuObject *x)
 {
     _du_read1(ob);
@@ -82,17 +90,24 @@
     _du_read1(olditems);
     int i, newcount = olditems->ob_count + 1;
 
-    _du_save3(ob, x, olditems);
-    DuTupleObject *newitems = DuTuple_New(newcount);
-    _du_restore3(ob, x, olditems);
+    if (newcount <= olditems->ob_capacity) {
+        _du_write1(olditems);
+        olditems->ob_items[newcount-1] = x;
+        olditems->ob_count = newcount;
+    } else {                    /* allocate new one */
+        _du_save3(ob, x, olditems);
+        DuTupleObject *newitems = DuTuple_New(overallocated_size(newcount));
+        newitems->ob_count = newcount;
+        _du_restore3(ob, x, olditems);
+        
+        _du_write1(ob);
 
-    _du_write1(ob);
+        for (i=0; i<newcount-1; i++)
+            newitems->ob_items[i] = olditems->ob_items[i];
+        newitems->ob_items[newcount-1] = x;
 
-    for (i=0; i<newcount-1; i++)
-        newitems->ob_items[i] = olditems->ob_items[i];
-    newitems->ob_items[newcount-1] = x;
-
-    ob->ob_tuple = newitems;
+        ob->ob_tuple = newitems;
+    } 
 }
 
 void DuList_Append(DuObject *ob, DuObject *item)
@@ -194,6 +209,7 @@
         stm_allocate_prebuilt(sizeof(DuTupleObject));
     du_empty_tuple->ob_base.type_id = DUTYPE_TUPLE;
     du_empty_tuple->ob_count = 0;
+    du_empty_tuple->ob_capacity = 0;
 }
 
 DuObject *DuList_New()


More information about the pypy-commit mailing list