Skip to content

Commit 5e8f7fd

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 6b540c2 commit 5e8f7fd

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)
@@ -140,6 +147,7 @@ def __str__(self):
140147
ability_name = self.data.ability(self.ability) if self.ability in self.data.abilities else "UNKNOWN"
141148
return self._str_prefix() + "Ability (%s) - %s" % (hex(self.ability), ability_name)
142149

150+
@loggable
143151
class TargetAbilityEvent(AbilityEvent):
144152
def __init__(self, framestamp, player, type, code, ability, target):
145153
super(TargetAbilityEvent, self).__init__(framestamp, player, type, code, ability)
@@ -172,6 +180,7 @@ def __str__(self):
172180

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

183+
@loggable
175184
class LocationAbilityEvent(AbilityEvent):
176185
def __init__(self, framestamp, player, type, code, ability, location):
177186
super(LocationAbilityEvent, self).__init__(framestamp, player, type, code, ability)
@@ -183,6 +192,7 @@ def __str__(self):
183192
class SelfAbilityEvent(AbilityEvent):
184193
pass
185194

195+
@loggable
186196
class HotkeyEvent(PlayerActionEvent):
187197
def __init__(self, framestamp, player, type, code, hotkey, deselect):
188198
super(HotkeyEvent, self).__init__(framestamp, player, type, code)
@@ -198,6 +208,7 @@ class AddToHotkeyEvent(HotkeyEvent):
198208
class GetFromHotkeyEvent(HotkeyEvent):
199209
pass
200210

211+
@loggable
201212
class SelectionEvent(PlayerActionEvent):
202213
def __init__(self, framestamp, player, type, code, bank, objects, deselect):
203214
super(SelectionEvent, self).__init__(framestamp, player, type, code)
@@ -228,4 +239,4 @@ def load_context(self, replay):
228239

229240
objects.append(obj)
230241

231-
self.objects = objects
242+
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)