Patrick Mézard avatar Patrick Mézard committed e1dbd96

svnwrap: use custom StringIO class in get_file()

The wrappers were calling ra.get_file() with a cStringIO object.
Empirically, svn 1.7.5 is writing 16kB blocks to the stream object, and
cStringIO reallocates its internal buffer and doubles its size whenever
it is filled. With large committed files this requires two large
memory blocks at the same time.

SimpleStringIO implements the mimimum StringIO interface used by
ra.get_file() but instead stores all the blocks and "join" them at the
end. It means more fragmentation but requires only one large block,
without overallocation. Also, 16kB blocks should be friendly to most

In practice, this simple change let me convert a revision containing
multiple moderately large files, the largest being around 450MB, with a
32-bits Windows setup, python 2.7, swig svn 1.7.5, in stupid mode, while
it was previously aborting with "not enough memory". The same revision
still fails in replay mode.

Comments (0)

Files changed (3)


         properties[prop.strip()] = value
     return properties
+class SimpleStringIO(object):
+    """SimpleStringIO can replace a StringIO in write mode.
+    cStringIO reallocates and doubles the size of its internal buffer
+    when it needs to append new data which requires two large blocks for
+    large inputs. SimpleStringIO stores each individual blocks and joins
+    them once done. This might cause more memory fragmentation but
+    requires only one large block. In practice, ra.get_file() seems to
+    write in 16kB blocks (svn 1.7.5) which should be friendly to memory
+    allocators.
+    """
+    def __init__(self):
+        self._blocks = []
+    def write(self, s):
+        self._blocks.append(s)
+    def getvalue(self):
+        return ''.join(self._blocks)
+    def close(self):
+        del self._blocks


         mode = ''
-            out = cStringIO.StringIO()
+            out = common.SimpleStringIO()
             rev, info = self.remote.get_file(path, out, revision)
             data = out.getvalue()


         assert not path.startswith('/')
         mode = ''
-            out = cStringIO.StringIO()
+            out = common.SimpleStringIO()
             info = ra.get_file(self.ra, path, revision, out)
             data = out.getvalue()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.