comparison mercurial/util.py @ 5449:17a4b20eda7b

chunkiter: handle large reads more efficiently - for large reads, don't attempt to read more than necessary - if we've gathered the exact number of bytes needed, avoid a string copy
author Matt Mackall <mpm@selenic.com>
date Thu, 11 Oct 2007 00:46:52 -0500
parents 56591846f819
children c728424d44c6
comparison
equal deleted inserted replaced
5448:e038738714fd 5449:17a4b20eda7b
1406 def read(self, l): 1406 def read(self, l):
1407 """Read L bytes of data from the iterator of chunks of data. 1407 """Read L bytes of data from the iterator of chunks of data.
1408 Returns less than L bytes if the iterator runs dry.""" 1408 Returns less than L bytes if the iterator runs dry."""
1409 if l > len(self.buf) and self.iter: 1409 if l > len(self.buf) and self.iter:
1410 # Clamp to a multiple of self.targetsize 1410 # Clamp to a multiple of self.targetsize
1411 targetsize = self.targetsize * ((l // self.targetsize) + 1) 1411 targetsize = max(l, self.targetsize)
1412 collector = cStringIO.StringIO() 1412 collector = cStringIO.StringIO()
1413 collector.write(self.buf) 1413 collector.write(self.buf)
1414 collected = len(self.buf) 1414 collected = len(self.buf)
1415 for chunk in self.iter: 1415 for chunk in self.iter:
1416 collector.write(chunk) 1416 collector.write(chunk)
1418 if collected >= targetsize: 1418 if collected >= targetsize:
1419 break 1419 break
1420 if collected < targetsize: 1420 if collected < targetsize:
1421 self.iter = False 1421 self.iter = False
1422 self.buf = collector.getvalue() 1422 self.buf = collector.getvalue()
1423 s, self.buf = self.buf[:l], buffer(self.buf, l) 1423 if len(self.buf) == l:
1424 s, self.buf = self.buf, ''
1425 else:
1426 s, self.buf = self.buf[:l], buffer(self.buf, l)
1424 return s 1427 return s
1425 1428
1426 def filechunkiter(f, size=65536, limit=None): 1429 def filechunkiter(f, size=65536, limit=None):
1427 """Create a generator that produces the data in the file size 1430 """Create a generator that produces the data in the file size
1428 (default 65536) bytes at a time, up to optional limit (default is 1431 (default 65536) bytes at a time, up to optional limit (default is