Skip to content

Commit 721e6ac

Browse files
ES-Alexanderpatrickelectric
authored andcommitted
pingmessage: [performance] use __slots__ declaration in PingParser
Reduces memory overhead (no internal __dict__), and improves variable access speeds.
1 parent f1055a1 commit 721e6ac

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

brping/pingmessage.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ def __repr__(self):
268268

269269
# A class to digest a serial stream and decode PingMessages
270270
class PingParser(object):
271+
# pre-declare instance variables for faster access and reduced memory overhead
272+
__slots__ = (
273+
"buf",
274+
"state",
275+
"payload_length",
276+
"message_id",
277+
"errors",
278+
"parsed",
279+
"rx_msg",
280+
)
281+
271282
NEW_MESSAGE = 0 # Just got a complete checksum-verified message
272283
WAIT_START = 1 # Waiting for the first character of a message 'B'
273284
WAIT_HEADER = 2 # Waiting for the second character in the two-character sequence 'BR'

0 commit comments

Comments
 (0)