Skip to content

Commit 915ee15

Browse files
committed
Improve replay events parse speed by 20+%.
Moves loggers from instance variables to event class variables, dramatically reducing calls to the (apparently slow) getLogger function. See pull request 46 for more details: GraylinKim#46 Thanks @fbulens.
1 parent 4d971f0 commit 915ee15

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

sc2reader/events.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from sc2reader.utils import Length, LITTLE_ENDIAN
44
from sc2reader.data.utils import DataObject
5-
from sc2reader import log_utils
5+
from sc2reader.log_utils import loggable
66

7+
@loggable
78
class Event(object):
89
def __init__(self, frame, pid):
9-
self.logger = log_utils.get_logger(self.__class__)
1010
self.pid = pid
1111
self.frame = frame
1212
self.second = frame >> 4
@@ -18,6 +18,7 @@ def load_context(self, replay):
1818
if self.pid != 16:
1919
self.player = replay.person[self.pid]
2020

21+
@loggable
2122
class GameEvent(Event):
2223
"""Abstract Event Type, should not be directly instanciated"""
2324
def __init__(self, frame, pid, event_type, event_code):
@@ -43,11 +44,13 @@ def __str__(self):
4344
# Message Events
4445
#########################
4546

47+
@loggable
4648
class MessageEvent(Event):
4749
def __init__(self, frame, pid, flags):
4850
super(MessageEvent, self).__init__(frame, pid)
4951
self.flags=flags
5052

53+
@loggable
5154
class ChatEvent(MessageEvent):
5255
def __init__(self, frame, pid, flags, buffer):
5356
super(ChatEvent, self).__init__(frame, pid, flags)
@@ -60,6 +63,7 @@ def __init__(self, frame, pid, flags, buffer):
6063
self.to_all = (self.target == 0)
6164
self.to_allies = (self.target == 2)
6265

66+
@loggable
6367
class PacketEvent(MessageEvent):
6468
def __init__(self, frame, pid, flags, buffer):
6569
super(PacketEvent, self).__init__(frame, pid, flags)
@@ -68,6 +72,7 @@ def __init__(self, frame, pid, flags, buffer):
6872
# send over the network to establish latency or connectivity.
6973
self.data = buffer.read_chars(4)
7074

75+
@loggable
7176
class PingEvent(MessageEvent):
7277
def __init__(self, frame, pid, flags, buffer):
7378
super(PingEvent, self).__init__(frame, pid, flags)
@@ -100,6 +105,7 @@ class CameraMovementEvent(GameEvent):
100105
class PlayerActionEvent(GameEvent):
101106
pass
102107

108+
@loggable
103109
class ResourceTransferEvent(PlayerActionEvent):
104110
def __init__(self, frames, pid, type, code, target, minerals, vespene):
105111
super(ResourceTransferEvent, self).__init__(frames, pid, type, code)
@@ -116,6 +122,7 @@ def load_context(self, replay):
116122
self.sender = replay.player[self.sender]
117123
self.reciever = replay.player[self.reciever]
118124

125+
@loggable
119126
class AbilityEvent(PlayerActionEvent):
120127
def __init__(self, framestamp, player, type, code, ability):
121128
super(AbilityEvent, self).__init__(framestamp, player, type, code)
@@ -144,6 +151,7 @@ def __str__(self):
144151
else:
145152
return self._str_prefix() + "Ability (%s) - %s" % (hex(self.ability_code), self.ability_name)
146153

154+
@loggable
147155
class TargetAbilityEvent(AbilityEvent):
148156
def __init__(self, framestamp, player, type, code, ability, target):
149157
super(TargetAbilityEvent, self).__init__(framestamp, player, type, code, ability)
@@ -179,6 +187,7 @@ def __str__(self):
179187

180188
return AbilityEvent.__str__(self) + "; Target: {0}".format(target)
181189

190+
@loggable
182191
class LocationAbilityEvent(AbilityEvent):
183192
def __init__(self, framestamp, player, type, code, ability, location):
184193
super(LocationAbilityEvent, self).__init__(framestamp, player, type, code, ability)
@@ -190,6 +199,7 @@ def __str__(self):
190199
class SelfAbilityEvent(AbilityEvent):
191200
pass
192201

202+
@loggable
193203
class HotkeyEvent(PlayerActionEvent):
194204
def __init__(self, framestamp, player, type, code, hotkey, deselect):
195205
super(HotkeyEvent, self).__init__(framestamp, player, type, code)
@@ -205,6 +215,7 @@ class AddToHotkeyEvent(HotkeyEvent):
205215
class GetFromHotkeyEvent(HotkeyEvent):
206216
pass
207217

218+
@loggable
208219
class SelectionEvent(PlayerActionEvent):
209220
def __init__(self, framestamp, player, type, code, bank, objects, deselect):
210221
super(SelectionEvent, self).__init__(framestamp, player, type, code)
@@ -235,4 +246,4 @@ def load_context(self, replay):
235246

236247
objects.append(obj)
237248

238-
self.objects = objects
249+
self.objects = objects

sc2reader/log_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ def get_logger(entity):
4646

4747
except AttributeError as e:
4848
msg = "Cannot retrieve logger for {0}. Only strings, classes, and functions supported."
49-
raise TypeError(msg.format(entity))
49+
raise TypeError(msg.format(entity))
50+
51+
def loggable(cls):
52+
cls.logger = get_logger(cls)
53+
return cls

0 commit comments

Comments
 (0)