Optimize FrozenEvent creation

pull/105/head
Erik Johnston 2015-01-06 18:51:03 +00:00
parent 36a2a877e2
commit dfa05f0cd6
2 changed files with 16 additions and 9 deletions

View File

@ -20,7 +20,7 @@ import copy
class _EventInternalMetadata(object): class _EventInternalMetadata(object):
def __init__(self, internal_metadata_dict): def __init__(self, internal_metadata_dict):
self.__dict__ = copy.deepcopy(internal_metadata_dict) self.__dict__ = internal_metadata_dict
def get_dict(self): def get_dict(self):
return dict(self.__dict__) return dict(self.__dict__)
@ -49,10 +49,10 @@ def _event_dict_property(key):
class EventBase(object): class EventBase(object):
def __init__(self, event_dict, signatures={}, unsigned={}, def __init__(self, event_dict, signatures={}, unsigned={},
internal_metadata_dict={}): internal_metadata_dict={}):
self.signatures = copy.deepcopy(signatures) self.signatures = signatures
self.unsigned = copy.deepcopy(unsigned) self.unsigned = unsigned
self._event_dict = copy.deepcopy(event_dict) self._event_dict = event_dict
self.internal_metadata = _EventInternalMetadata( self.internal_metadata = _EventInternalMetadata(
internal_metadata_dict internal_metadata_dict
@ -112,10 +112,16 @@ class EventBase(object):
class FrozenEvent(EventBase): class FrozenEvent(EventBase):
def __init__(self, event_dict, internal_metadata_dict={}): def __init__(self, event_dict, internal_metadata_dict={}):
event_dict = copy.deepcopy(event_dict) event_dict = dict(event_dict)
signatures = copy.deepcopy(event_dict.pop("signatures", {})) # Signatures is a dict of dicts, and this is faster than doing a
unsigned = copy.deepcopy(event_dict.pop("unsigned", {})) # copy.deepcopy
signatures = {
name: {sig_id: sig for sig_id, sig in sigs.items()}
for name, sigs in event_dict.pop("signatures", {}).items()
}
unsigned = dict(event_dict.pop("unsigned", {}))
frozen_dict = freeze(event_dict) frozen_dict = freeze(event_dict)

View File

@ -17,10 +17,11 @@ from frozendict import frozendict
def freeze(o): def freeze(o):
if isinstance(o, dict) or isinstance(o, frozendict): t = type(o)
if t is dict:
return frozendict({k: freeze(v) for k, v in o.items()}) return frozendict({k: freeze(v) for k, v in o.items()})
if isinstance(o, basestring): if t is str or t is unicode:
return o return o
try: try: