Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/zeroconf/_services/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,31 +345,27 @@ def _set_text(self, text: bytes) -> None:
def _unpack_text_into_properties(self) -> None:
"""Unpacks the text field into properties"""
text = self.text
end = len(text)
if end == 0:
if not text:
# Properties should be set atomically
# in case another thread is reading them
self._properties = {}
return

result: Dict[Union[str, bytes], Optional[Union[str, bytes]]] = {}
index = 0
strs: List[bytes] = []
pairs: List[bytes] = []
end = len(text)
while index < end:
length = text[index]
index += 1
strs.append(text[index : index + length])
pairs.append(text[index : index + length])
index += length

for s in strs:
key, _, value = s.partition(b'=')
# Only update non-existent properties
if key and key not in result:
result[key] = value or None
Comment on lines -364 to -368

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was O(n)*2 + the cost of the python loop


# Properties should be set atomically
# in case another thread is reading them
self._properties = result
# Reverse the list so that the first item in the list
# is the last item in the text field. This is important
# to preserve backwards compatibility where the first
# key always wins if the key is seen multiple times.
pairs.reverse()
self._properties = {key: value or None for key, _, value in (pair.partition(b'=') for pair in pairs)}

def get_name(self) -> str:
"""Name accessor"""
Expand Down