From 366fe5524d8acfbfb27055cdc7e03d36cd3f5d95 Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Tue, 7 Oct 2014 14:30:08 -0400 Subject: [PATCH 1/2] Performance improvement in Stream handling Store stream data into a list and only merge it into a string when requested. This removes the overhead of constantly building new strings. --- lib/Stream.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/Stream.py b/lib/Stream.py index b01ab3f..86c3594 100644 --- a/lib/Stream.py +++ b/lib/Stream.py @@ -29,21 +29,30 @@ def __init__(self, stream = ""): self.offset = 0 self.size = len(stream) self.dontScanAgain = False + self.unmergedData = [] - #Appends new data to the stream + #Appends new data to the buffer def appendData(self, data): - self.stream +=data + self.unmergedData.append(data) self.size +=len(data) + #Merges the buffered data into the stream + def _mergeData(self): + if self.unmergedData: + self.stream += ''.join(self.unmergedData) + self.unmergedData = [] + #Prints the stream def dump(self): + self._mergeData() hexdump(self.stream[self.offset:]) - #Gets n bytes from the buffer and increments the offset + #Gets n bytes from the stream and increments the offset def getBytes(self, n): if self.offset + n> self.size: raise StreamNoMoreBytes + self._mergeData() bytes = self.stream[self.offset:self.offset+n] self.offset = self.offset + n return bytes @@ -52,11 +61,12 @@ def getBytes(self, n): def getByte(self): return ord(self.getBytes(1)) - #Reads n bytes from the buffer without increments the offset + #Reads n bytes from the stream without increments the offset def readBytes(self, n): if self.offset >= self.size: return None + self._mergeData() bytes = self.stream[self.offset:self.offset+n] return bytes From dd547cd664fc39462b49c46a9d912fb782e5f47e Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Tue, 7 Oct 2014 14:31:15 -0400 Subject: [PATCH 2/2] Fixed grammar in comment --- lib/Stream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Stream.py b/lib/Stream.py index 86c3594..86f1a53 100644 --- a/lib/Stream.py +++ b/lib/Stream.py @@ -61,7 +61,7 @@ def getBytes(self, n): def getByte(self): return ord(self.getBytes(1)) - #Reads n bytes from the stream without increments the offset + #Reads n bytes from the stream without incrementing the offset def readBytes(self, n): if self.offset >= self.size: return None