TransRead: limit the amount of bytes we read at a time
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 21 Feb 2013 11:06:23 +0000 (13:06 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 21 Feb 2013 11:37:06 +0000 (13:37 +0200)
In the function which implements fake seek forward we first calculate the
amount of bytes we have to read from the file to seek forward to the requisted
position, and then read that data in one go. However, the seek may be really
far forward, and we'll end up reading really a lot of data in one go, which
leads to high memory consumption.

This patch fixes the issue by limiting the amount of data we read in one go to
1MiB.

Change-Id: Idfdde2e432c216de34b4675e47c859f5e68a2928
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/TransRead.py

index c19869f487f7e2d38ff518af2f02ebe86d3852dd..37573d0a3b15b8e0fd884335d29949931dc775d5 100644 (file)
@@ -30,7 +30,8 @@ def _fake_seek_forward(file_obj, cur_pos, offset, whence = os.SEEK_SET):
     length = new_pos - cur_pos
     to_read = length
     while to_read > 0:
-        buf = file_obj.read(to_read)
+        chunk_size = min(to_read, 1024 * 1024)
+        buf = file_obj.read(chunk_size)
         if not buf:
             break
         to_read -= len(buf)