[PATCH 1 of 3] util.chunkbuffer: special case reading everything

Gregory Szorc gregory.szorc at gmail.com
Tue Oct 6 00:45:24 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1444087692 25200
#      Mon Oct 05 16:28:12 2015 -0700
# Node ID 23a211653e6585ce9f65660d41a6cb8a538aa69b
# Parent  4464cf29d797d2369f00aa13b640029ebae15334
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.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1291,12 +1291,15 @@ class chunkbuffer(object):
         """Read L bytes of data from the iterator of chunks of data.
         Returns less than L bytes if the iterator runs dry.
 
         If size parameter is omitted, read everything"""
+        if l is None:
+            return ''.join(self.iter)
+
         left = l
         buf = []
         queue = self._queue
-        while left is None or left > 0:
+        while left > 0:
             # refill the queue
             if not queue:
                 target = 2**18
                 for chunk in self.iter:
@@ -1307,11 +1310,10 @@ class chunkbuffer(object):
                 if not queue:
                     break
 
             chunk = queue.popleft()
-            if left is not None:
-                left -= len(chunk)
-            if left is not None and left < 0:
+            left -= len(chunk)
+            if left < 0:
                 queue.appendleft(chunk[left:])
                 buf.append(chunk[:left])
             else:
                 buf.append(chunk)


More information about the Mercurial-devel mailing list