Skip to content

Commit 10bb713

Browse files
committed
Tweaks the execution of the get_big member of the ByteStream class to cut the replay parsing time by between 60-70%
1 parent 3f80483 commit 10bb713

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

sc2reader/parsers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def load(self, replay, filecontents):
143143
replay.messages = list()
144144
bytes, time = ByteStream(filecontents), 0
145145

146-
while(bytes.length!=0):
146+
while(bytes.remaining!=0):
147147
time += bytes.get_timestamp()
148148
player_id = bytes.get_big_int(1) & 0x0F
149149
flags = bytes.get_big_int(1)
@@ -221,7 +221,7 @@ def load(self, replay, filecontents):
221221
#set up an event list, start the timer, and process the file contents
222222
replay.events, elapsed_time, bytes = list(), 0, ByteStream(filecontents)
223223

224-
while bytes.length > 0:
224+
while bytes.remaining > 0:
225225
#First section is always a timestamp marking the elapsed time
226226
#since the last eventObjectlisted
227227
location = hex(bytes.cursor)

sc2reader/utils.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ class ByteStream(object):
33
"""Most functions will return the byte_code as well when requested"""
44

55
def __init__(self, stream):
6+
self.__cbyte = 0
67
self.cursor = -1 #First element is 0
78
self.stream = stream.encode("hex").upper()
89

910
def get_big(self, number, byte_code=False):
1011
#Do a sanity check, if streams are parsed right this won't ever happen
11-
if len(self.stream) < number*2:
12-
msg = "Stream is only %s bytes long; %s bytes requested"
13-
raise ValueError(msg % (self.length, number) )
12+
if len(self.stream)-self.__cbyte < number*2:
13+
msg = "Stream only has %s bytes left; %s bytes requested"
14+
raise ValueError(msg % (self.remaining, number) )
1415

1516
#For big endian, the byte_string is the result
16-
result = self.stream[:number*2]
17+
result = self.stream[self.__cbyte:self.__cbyte+number*2]
1718

1819
#Move the ByteStream forward
19-
self.stream = self.stream[number*2:]
20+
self.__cbyte = self.__cbyte + number*2
2021
self.cursor = self.cursor + number
2122

2223
if byte_code:
@@ -31,7 +32,7 @@ def skip(self, number, byte_code=False):
3132
self.get_big(number)
3233

3334
def peek(self, number):
34-
return self.stream[:number*2]
35+
return self.stream[self.__cbyte:self.__cbyte + number*2]
3536

3637
def get_little(self, number, byte_code=False):
3738
#Get a list of the next 'number' of bytes from the stream
@@ -166,5 +167,5 @@ def parse_serialized_data(self, byte_code=False):
166167
return data
167168

168169
@property
169-
def length(self):
170-
return len(self.stream)
170+
def remaining(self):
171+
return len(self.stream)-self.__cbyte

0 commit comments

Comments
 (0)