[pypy-svn] r70843 - in pypy/branch/stringbuilder2/pypy/rpython: lltypesystem test

arigo at codespeak.net arigo at codespeak.net
Mon Jan 25 18:23:13 CET 2010


Author: arigo
Date: Mon Jan 25 18:23:13 2010
New Revision: 70843

Modified:
   pypy/branch/stringbuilder2/pypy/rpython/lltypesystem/rbuilder.py
   pypy/branch/stringbuilder2/pypy/rpython/test/test_rbuilder.py
Log:
(fijal, arigo)
Use the new interface provided by rlib.rgc.


Modified: pypy/branch/stringbuilder2/pypy/rpython/lltypesystem/rbuilder.py
==============================================================================
--- pypy/branch/stringbuilder2/pypy/rpython/lltypesystem/rbuilder.py	(original)
+++ pypy/branch/stringbuilder2/pypy/rpython/lltypesystem/rbuilder.py	Mon Jan 25 18:23:13 2010
@@ -1,6 +1,6 @@
 
 from pypy.rpython.rbuilder import AbstractStringBuilderRepr
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, rstr
 from pypy.rpython.lltypesystem.rstr import STR, UNICODE, char_repr,\
      string_repr, unichar_repr, unicode_repr
 from pypy.rpython.annlowlevel import llstr
@@ -9,9 +9,12 @@
 from pypy.rpython.lltypesystem.lltype import staticAdtMethod
 from pypy.tool.sourcetools import func_with_new_name
 
+# Think about heuristics below, maybe we can come up with something
+# better or at least compare it with list heuristics
+
 GROW_FAST_UNTIL = 100*1024*1024      # 100 MB
 
-def new_grow_func(name):
+def new_grow_func(name, mallocfn, copycontentsfn):
     def stringbuilder_grow(ll_builder, needed):
         allocated = ll_builder.allocated
         #if allocated < GROW_FAST_UNTIL:
@@ -20,31 +23,31 @@
         extra_size = allocated >> 2
         try:
             new_allocated = ovfcheck(allocated + extra_size)
-        except OverflowError:
-            raise MemoryError
-        try:
             new_allocated = ovfcheck(new_allocated + needed)
         except OverflowError:
             raise MemoryError
-        ll_builder.buf = rgc.resize_buffer(ll_builder.buf, ll_builder.allocated,
-                                           new_allocated)
+        newbuf = mallocfn(new_allocated)
+        copycontentsfn(ll_builder.buf, newbuf, 0, 0, ll_builder.allocated)
+        ll_builder.buf = newbuf
         ll_builder.allocated = new_allocated
     return func_with_new_name(stringbuilder_grow, name)
 
-stringbuilder_grow = new_grow_func('stringbuilder_grow')
-unicodebuilder_grow = new_grow_func('unicodebuilder_grow')
+stringbuilder_grow = new_grow_func('stringbuilder_grow', rstr.mallocstr,
+                                   rstr.copy_string_contents)
+unicodebuilder_grow = new_grow_func('unicodebuilder_grow', rstr.mallocunicode,
+                                    rstr.copy_unicode_contents)
 
 STRINGBUILDER = lltype.GcStruct('stringbuilder',
-                              ('allocated', lltype.Signed),
-                              ('used', lltype.Signed),
-                              ('buf', lltype.Ptr(STR)),
-                                adtmeths={'grow':staticAdtMethod(stringbuilder_grow)})
+                               ('allocated', lltype.Signed),
+                               ('used', lltype.Signed),
+                               ('buf', lltype.Ptr(STR)),
+                               adtmeths={'grow':staticAdtMethod(stringbuilder_grow)})
 
 UNICODEBUILDER = lltype.GcStruct('unicodebuilder',
                                  ('allocated', lltype.Signed),
                                  ('used', lltype.Signed),
                                  ('buf', lltype.Ptr(UNICODE)),
-                                 adtmeths={'grow':staticAdtMethod(unicodebuilder_grow)})
+                              adtmeths={'grow':staticAdtMethod(unicodebuilder_grow)})
 
 MAX = 16*1024*1024
 
@@ -56,7 +59,7 @@
         ll_builder = lltype.malloc(cls.lowleveltype.TO)
         ll_builder.allocated = init_size
         ll_builder.used = 0
-        ll_builder.buf = rgc.resizable_buffer_of_shape(cls.basetp, init_size)
+        ll_builder.buf = lltype.malloc(cls.basetp, init_size)
         return ll_builder
 
     @staticmethod
@@ -99,8 +102,7 @@
     @staticmethod
     def ll_build(ll_builder):
         final_size = ll_builder.used
-        return rgc.finish_building_buffer(ll_builder.buf, ll_builder.allocated,
-                                          final_size)
+        return rgc.ll_shrink_array(ll_builder.buf, final_size)
 
 class StringBuilderRepr(BaseStringBuilderRepr):
     lowleveltype = lltype.Ptr(STRINGBUILDER)

Modified: pypy/branch/stringbuilder2/pypy/rpython/test/test_rbuilder.py
==============================================================================
--- pypy/branch/stringbuilder2/pypy/rpython/test/test_rbuilder.py	(original)
+++ pypy/branch/stringbuilder2/pypy/rpython/test/test_rbuilder.py	Mon Jan 25 18:23:13 2010
@@ -1,7 +1,21 @@
 
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
+from pypy.rpython.lltypesystem.rbuilder import *
+from pypy.rpython.annlowlevel import llstr, hlstr
 from pypy.rlib.rstring import StringBuilder, UnicodeBuilder
 
+
+class TestStringBuilderDirect(object):
+    def test_simple(self):
+        sb = StringBuilderRepr.ll_new(3)
+        StringBuilderRepr.ll_append_char(sb, 'x')
+        StringBuilderRepr.ll_append(sb, llstr("abc"))
+        StringBuilderRepr.ll_append_slice(sb, llstr("foobar"), 2, 5)
+        StringBuilderRepr.ll_append_multiple_char(sb, 'y', 3)
+        s = StringBuilderRepr.ll_build(sb)
+        assert hlstr(s) == "xabcobayyy"
+
+
 class BaseTestStringBuilder(BaseRtypingTest):
     def test_simple(self):
         def func():
@@ -37,7 +51,6 @@
         assert res == 'aabcabcdefbuuuu'
         assert isinstance(res, unicode)
 
-
 class TestLLtype(BaseTestStringBuilder, LLRtypeMixin):
     pass
 



More information about the Pypy-commit mailing list