[pypy-commit] pypy faster-rstruct-2: WIP: break the world, and start to refactor pack_* to build the result inside a MutableStringBuffer instead of a StringBuilder

antocuni pypy.commits at gmail.com
Wed May 10 19:05:27 EDT 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91227:b6e312499e57
Date: 2017-05-10 11:48 +0200
http://bitbucket.org/pypy/pypy/changeset/b6e312499e57/

Log:	WIP: break the world, and start to refactor pack_* to build the
	result inside a MutableStringBuffer instead of a StringBuilder

diff --git a/rpython/rlib/mutbuffer.py b/rpython/rlib/mutbuffer.py
--- a/rpython/rlib/mutbuffer.py
+++ b/rpython/rlib/mutbuffer.py
@@ -23,8 +23,12 @@
         self.readonly = False
         # rstr.mallocstr does not pass zero=True, so we call lltype.malloc
         # directly
+        self.size = size
         self.ll_val = lltype.malloc(STR, size, zero=True)
 
+    def getlength(self):
+        return self.size
+
     def finish(self):
         if not self.ll_val:
             raise ValueError("Cannot call finish() twice")
diff --git a/rpython/rlib/rstruct/standardfmttable.py b/rpython/rlib/rstruct/standardfmttable.py
--- a/rpython/rlib/rstruct/standardfmttable.py
+++ b/rpython/rlib/rstruct/standardfmttable.py
@@ -13,7 +13,6 @@
 from rpython.rlib.rstruct.error import StructError, StructOverflowError
 from rpython.rlib.unroll import unrolling_iterable
 from rpython.rlib.buffer import StringBuffer
-#from rpython.rlib.strstorage import str_storage_getitem
 from rpython.rlib import rarithmetic
 from rpython.rlib.buffer import CannotRead
 from rpython.rtyper.lltypesystem import rffi
@@ -29,7 +28,8 @@
     if len(string) != 1:
         raise StructError("expected a string of length 1")
     c = string[0]   # string->char conversion for the annotator
-    fmtiter.result.append(c)
+    fmtiter.result.setitem(fmtiter.pos, c)
+    fmtiter.advance(1)
 
 def pack_bool(fmtiter):
     c = '\x01' if fmtiter.accept_bool_arg() else '\x00'
diff --git a/rpython/rlib/rstruct/test/test_pack.py b/rpython/rlib/rstruct/test/test_pack.py
--- a/rpython/rlib/rstruct/test/test_pack.py
+++ b/rpython/rlib/rstruct/test/test_pack.py
@@ -5,11 +5,15 @@
 
 class FakeFormatIter(object):
 
-    def __init__(self, bigendian, value):
+    def __init__(self, bigendian, size, value):
         from rpython.rlib.rstring import StringBuilder
         self.value = value
         self.bigendian = bigendian
-        self.result = StringBuilder(8)
+        self.result = MutableStringBuffer(size)
+        self.pos = 0
+
+    def advance(self, count):
+        self.pos += count
 
     def _accept_arg(self):
         return self.value
@@ -32,11 +36,12 @@
 
     def mypack(self, fmt, value):
         bigendian = self.endianess == '>'
-        fake_fmtiter = FakeFormatIter(bigendian, value)
+        size = struct.calcsize(fmt)
+        fake_fmtiter = FakeFormatIter(bigendian, size, value)
         attrs = self.fmttable[fmt]
         pack = attrs['pack']
         pack(fake_fmtiter)
-        return fake_fmtiter.result.build()
+        return fake_fmtiter.result.finish()
 
     def check(self, fmt, value):
         expected = struct.pack(self.endianess+fmt, value)


More information about the pypy-commit mailing list