Commit df0e157f authored by Vincent Pelletier's avatar Vincent Pelletier

lib.utils: Avoid data duplication.

parent 74bdff97
...@@ -197,9 +197,8 @@ class ReadBuffer(object): ...@@ -197,9 +197,8 @@ class ReadBuffer(object):
def append(self, data): def append(self, data):
""" Append some data and compute the new buffer size """ """ Append some data and compute the new buffer size """
size = len(data) self.size += len(data)
self.size += size self.content.append(data)
self.content.append((size, data))
def __len__(self): def __len__(self):
""" Return the current buffer size """ """ Return the current buffer size """
...@@ -216,14 +215,14 @@ class ReadBuffer(object): ...@@ -216,14 +215,14 @@ class ReadBuffer(object):
to_read = size to_read = size
# select required chunks # select required chunks
while to_read > 0: while to_read > 0:
chunk_size, chunk_data = pop_chunk() chunk_data = pop_chunk()
to_read -= chunk_size to_read -= len(chunk_data)
append_data(chunk_data) append_data(chunk_data)
if to_read < 0: if to_read < 0:
# too many bytes consumed, cut the last chunk # too many bytes consumed, cut the last chunk
last_chunk = chunk_list[-1] last_chunk = chunk_list[-1]
keep, let = last_chunk[:to_read], last_chunk[to_read:] keep, let = last_chunk[:to_read], last_chunk[to_read:]
self.content.appendleft((-to_read, let)) self.content.appendleft(let)
chunk_list[-1] = keep chunk_list[-1] = keep
# join all chunks (one copy) # join all chunks (one copy)
data = ''.join(chunk_list) data = ''.join(chunk_list)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment