Skip to content

Commit

Permalink
Cache (sorted) keys property, add few minimal perf tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Sep 3, 2015
1 parent 75e73ce commit 6c91b38
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions schemamessages/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def format(cls):
"""
return cls._format

@property
def keys(cls):
"""
Format of this message as per schema
"""
return cls._keys

@property
def enums(cls):
"""
Expand Down Expand Up @@ -78,8 +85,7 @@ def get_binary_length(self):
"""
Returns actual binary length of the message in it's current state
"""
keys = list(self.__class__.format.keys())
keys.sort()
keys = self.__class__.keys
binary_format = self.__class__.binary_format
string_lengths = []
for key in keys:
Expand All @@ -92,15 +98,14 @@ def get_binary_length(self):
def pack(self):
binary_format = self.__class__.binary_format
format_ = self.__class__.format
keys = list(format_.keys())
keys.sort()
keys = self.__class__.keys

# start off with an id
data = [self.__class__.id, ]
# if we encounter any strings, log the length of each one
str_lengths = []
for key in keys:
value = self.get(key, 0)
value = self[key]
if(format_[key] == 'enum'):
value = self.__class__.enum_lookup(key, value)
elif(format_[key] == 'string'):
Expand All @@ -111,7 +116,7 @@ def pack(self):
str_lengths.append(len(value))
data.append(value)

if(len(str_lengths)):
if(str_lengths):
binary_format = binary_format.format(*str_lengths)
buffer_ = struct.pack(binary_format, *data)
return buffer_
Expand Down Expand Up @@ -308,6 +313,8 @@ def __init__(self, schema):
schema[msg_class_name]
)
newclass._format = schema[msg_class_name]['format']
newclass._keys = list(newclass._format)
newclass._keys.sort()
newclass._schema = schema
newclass._id = next_id

Expand All @@ -323,14 +330,12 @@ def unpack_message(data, factory):
buffer_ = memoryview(data)

(msg_id, ) = struct.unpack_from(
'!{}'.format(factory.id_binary_format),
buffer_[0:factory.bytes_needed_for_id]
factory.id_binary_format,
buffer_
)
cls = factory.get_by_id(msg_id)
item = cls()

keys = list(cls.format.keys())
keys.sort()
keys = cls.keys
string_lengths = list()
indexes_to_remove = list()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'author': 'Tom Najdek',
'url': 'https://github.com/tnajdek/schema-messages-python',
'author_email': 'tom@doppnet.com',
'version': '0.1.1',
'version': '0.1.2',
'packages': ['schemamessages'],
'install_requires': ['future', 'bidict']
}
Expand Down
2 changes: 1 addition & 1 deletion test/schemamessages_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from builtins import bytes
from mock import MagicMock
from schemamessages import (
from schemamessages.message import (
MessageFactory,
ImproperlyConfigured,
pack_messages,
Expand Down

0 comments on commit 6c91b38

Please sign in to comment.