Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 26478:a3f7e5461dbd
util.chunkbuffer: special case reading everything
The new code results in simpler logic within the while loop. It is also
faster since we avoid performing operations on the queue and buf
collections. However, there shouldn't be any super hot loops for this
since the whole point of chunkbuffer is to avoid reading large amounts
of data at once. This does, however, make it easier to optimize
chunkbuffer in a subsequent patch.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 05 Oct 2015 16:28:12 -0700 |
parents | 1138e1d05207 |
children | 46143f31290e |
comparison
equal
deleted
inserted
replaced
26477:d69245af4dbe | 26478:a3f7e5461dbd |
---|---|
1290 def read(self, l=None): | 1290 def read(self, l=None): |
1291 """Read L bytes of data from the iterator of chunks of data. | 1291 """Read L bytes of data from the iterator of chunks of data. |
1292 Returns less than L bytes if the iterator runs dry. | 1292 Returns less than L bytes if the iterator runs dry. |
1293 | 1293 |
1294 If size parameter is omitted, read everything""" | 1294 If size parameter is omitted, read everything""" |
1295 if l is None: | |
1296 return ''.join(self.iter) | |
1297 | |
1295 left = l | 1298 left = l |
1296 buf = [] | 1299 buf = [] |
1297 queue = self._queue | 1300 queue = self._queue |
1298 while left is None or left > 0: | 1301 while left > 0: |
1299 # refill the queue | 1302 # refill the queue |
1300 if not queue: | 1303 if not queue: |
1301 target = 2**18 | 1304 target = 2**18 |
1302 for chunk in self.iter: | 1305 for chunk in self.iter: |
1303 queue.append(chunk) | 1306 queue.append(chunk) |
1306 break | 1309 break |
1307 if not queue: | 1310 if not queue: |
1308 break | 1311 break |
1309 | 1312 |
1310 chunk = queue.popleft() | 1313 chunk = queue.popleft() |
1311 if left is not None: | 1314 left -= len(chunk) |
1312 left -= len(chunk) | 1315 if left < 0: |
1313 if left is not None and left < 0: | |
1314 queue.appendleft(chunk[left:]) | 1316 queue.appendleft(chunk[left:]) |
1315 buf.append(chunk[:left]) | 1317 buf.append(chunk[:left]) |
1316 else: | 1318 else: |
1317 buf.append(chunk) | 1319 buf.append(chunk) |
1318 | 1320 |