Skip to content

Commit 3b67eab

Browse files
committed
Makes ReplayBuffer functions smarter and more efficient by about 15% according to tests. Still slower than master branch (with good reason).
1 parent 307742f commit 3b67eab

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

sc2reader/readers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ def read(self, filecontents, replay):
7777
]))
7878

7979
def load_header(self, replay, buffer):
80-
buffer.read_chars(4,LITTLE_ENDIAN)
80+
buffer.read_chars(4)
8181

8282
class AttributeEventsReader_17326(AttributeEventsReader):
8383
def reads(self, build):
8484
return build >= 17326
8585

8686
def load_header(self, replay, buffer):
87-
buffer.read_chars(5,LITTLE_ENDIAN)
87+
buffer.read_chars(5)
8888

8989
##################################################
9090

sc2reader/utils.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def cursor(self): return self.io.tell()
8585
def tell(self): return self.io.tell()
8686
def skip(self, amount): self.seek(amount, SEEK_CUR)
8787
def reset(self): self.io.seek(0); self.bit_shift = 0
88-
def align(self): self.shift(8-self.bit_shift) if self.bit_shift else 0
88+
def align(self): self.bit_shift=0
8989
def seek(self, position, mode=SEEK_SET):
9090
self.io.seek(position, mode)
9191
if self.io.tell() and self.bit_shift:
@@ -103,21 +103,26 @@ def peek(self, length):
103103
'''
104104
def read_byte(self):
105105
""" Basic byte read """
106-
return self.read(1)[0]
106+
if self.bit_shift==0:
107+
return ord(self.io.read(1))
108+
else:
109+
return self.read(1)[0]
107110

108111
def read_int(self, endian=LITTLE_ENDIAN):
109112
""" int32 read """
110-
return struct.unpack(endian+'I', self.read_chars(4))[0]
113+
chars = self.io.read(4) if self.bit_shift==0 else self.read_chars(4)
114+
return struct.unpack(endian+'I', chars)[0]
111115

112116
def read_short(self, endian=LITTLE_ENDIAN):
113117
""" short16 read """
114-
return struct.unpack(endian+'H', self.read_chars(2))[0]
118+
chars = self.io.read(2) if self.bit_shift==0 else self.read_chars(2)
119+
return struct.unpack(endian+'H', chars)[0]
115120

116-
def read_chars(self, length=0, endian=BIG_ENDIAN):
117-
chars = [chr(byte) for byte in self.read(length)]
118-
if endian == LITTLE_ENDIAN:
119-
chars = reversed(chars)
120-
return ''.join(chars)
121+
def read_chars(self, length=0):
122+
if self.bit_shift==0:
123+
return self.io.read(length)
124+
else:
125+
return ''.join(chr(byte) for byte in self.read(length))
121126

122127
def read_hex(self, length=0):
123128
return self.read_chars(length).encode("hex")
@@ -274,7 +279,7 @@ def shift(self, bits):
274279

275280
except TypeError:
276281
raise EOFError("Cannot shift requested bits. End of buffer reached")
277-
282+
278283
def read(self, bytes=0, bits=0):
279284
try:
280285
bytes, bits = bytes+bits/8, bits%8
@@ -354,7 +359,7 @@ def read(self, bytes=0, bits=0):
354359

355360
except TypeError:
356361
raise EOFError("Cannot read requested bits/bytes. End of buffer reached")
357-
362+
358363
class PersonDict(dict):
359364
"""Delete is supported on the pid index only"""
360365
def __init__(self, *args, **kwargs):

0 commit comments

Comments
 (0)