Skip to content

Commit a23e187

Browse files
committed
Additional optimizations from the reader.py side.
1 parent e2ab8ec commit a23e187

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

sc2reader/readers.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,29 @@ def __call__(self, buffer, replay):
204204
}
205205

206206
game_events, frames = list(), 0
207-
while not buffer.empty:
207+
208+
# Locals are faster!
209+
tell = buffer.io.tell
210+
read_timestamp = buffer.read_timestamp
211+
shift = buffer.shift
212+
read_byte = buffer.read_byte
213+
debug = replay.opt.debug
214+
append = game_events.append
215+
align = buffer.align
216+
217+
while buffer.left:
208218
#Save the start so we can trace for debug purposes
209-
start = buffer.cursor
219+
start = tell()
210220

211221
# Each event has the same header which consists of 1-3 bytes for a
212222
# count of the number of frames since the last recorded event, a
213223
# byte split 5-3 bitwise for the player id (0-16) and the event
214224
# type (0-4). A final header byte representing the code uniquely
215225
# identifies the class of event we are handling.
216-
frames += buffer.read_timestamp()
217-
pid = buffer.shift(5)
218-
type = buffer.shift(3)
219-
code = buffer.read_byte()
226+
frames += read_timestamp()
227+
pid = shift(5)
228+
type = shift(3)
229+
code = read_byte()
220230

221231

222232
try:
@@ -229,10 +239,10 @@ def __call__(self, buffer, replay):
229239

230240
# For debugging purposes, we may wish to record the event.bytes
231241
# associated with this event; including the event header bytes.
232-
if replay.opt.debug:
242+
if debug:
233243
event.bytes = buffer.read_range(start, buffer.cursor)
234244

235-
game_events.append(event)
245+
append(event)
236246

237247
except KeyError:
238248
# If the type is not a key in the PARSERS lookup table we
@@ -252,7 +262,7 @@ def __call__(self, buffer, replay):
252262
# Because events are parsed in a bitwise fashion, they sometimes
253263
# leave the buffer in a bitshifted state. Each new event always
254264
# starts byte aligned so make sure that the buffer does too.
255-
buffer.align()
265+
align()
256266

257267
return game_events
258268

@@ -406,16 +416,13 @@ def parse_cameraX8_event(self, buffer, frames, type, code, pid):
406416

407417
def parse_cameraX1_event(self, buffer, frames, type, code, pid):
408418
#Get the X and Y, last byte is also a flag
409-
buffer.skip(3)
410-
flag = buffer.read_byte()
419+
chunk = buffer.read_int(BIG_ENDIAN)
411420

412-
if flag & 0x10 != 0:
413-
buffer.skip(1)
414-
flag = buffer.read_byte()
415-
if flag & 0x20 != 0:
416-
buffer.skip(1)
417-
flag = buffer.read_byte()
418-
if flag & 0x40 != 0:
421+
if chunk & 0x10 != 0:
422+
chunk = buffer.read_short(BIG_ENDIAN)
423+
if chunk & 0x20 != 0:
424+
chunk = buffer.read_short(BIG_ENDIAN)
425+
if chunk & 0x40 != 0:
419426
buffer.skip(2)
420427

421428
return CameraMovementEvent(frames, pid, type, code)

0 commit comments

Comments
 (0)