[pypy-commit] pypy default: lazily construct the StringBuilder for RStringIO

bdkearns noreply at buildbot.pypy.org
Thu Mar 21 09:53:21 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r62607:594d075cf003
Date: 2013-03-21 03:48 -0400
http://bitbucket.org/pypy/pypy/changeset/594d075cf003/

Log:	lazily construct the StringBuilder for RStringIO

diff --git a/rpython/rlib/rStringIO.py b/rpython/rlib/rStringIO.py
--- a/rpython/rlib/rStringIO.py
+++ b/rpython/rlib/rStringIO.py
@@ -15,7 +15,7 @@
         #  * the list of characters self.bigbuffer;
         #  * each of the strings in self.strings.
         #
-        self.strings = StringBuilder()
+        self.strings = None
         self.bigbuffer = []
         self.pos = AT_END
 
@@ -24,7 +24,7 @@
         self.bigbuffer = None
 
     def is_closed(self):
-        return self.strings is None
+        return self.bigbuffer is None
 
     def getvalue(self):
         """If self.strings contains more than 1 string, join all the
@@ -32,18 +32,21 @@
         if len(self.bigbuffer):
             self.copy_into_bigbuffer()
             return ''.join(self.bigbuffer)
-        return self.strings.build()
+        if self.strings is not None:
+            return self.strings.build()
+        return ''
 
     def getsize(self):
         result = len(self.bigbuffer)
-        result += self.strings.getlength()
+        if self.strings is not None:
+            result += self.strings.getlength()
         return result
 
     def copy_into_bigbuffer(self):
         """Copy all the data into the list of characters self.bigbuffer."""
-        if self.strings.getlength():
+        if self.strings is not None:
             self.bigbuffer += self.strings.build()
-            self.strings = StringBuilder()
+            self.strings = None
 
     def write(self, buffer):
         # Idea: for the common case of a sequence of write() followed
@@ -80,6 +83,8 @@
                     self.bigbuffer += '\x00' * (-fitting)
                     self.pos = AT_END      # fall-through to the fast path
         # Fast path.
+        if self.strings is None:
+            self.strings = StringBuilder()
         self.strings.append(buffer)
 
     def seek(self, position, mode=0):
@@ -151,8 +156,8 @@
             self.copy_into_bigbuffer()
         else:
             # we can drop all extra strings
-            if self.strings.getlength():
-                self.strings = StringBuilder()
+            if self.strings is not None:
+                self.strings = None
         if size < len(self.bigbuffer):
             del self.bigbuffer[size:]
         self.pos = AT_END


More information about the pypy-commit mailing list