Skip to content

Commit

Permalink
Speed up registry config websocket api calls with list comps (#110693)
Browse files Browse the repository at this point in the history
* Speed up registry config websocket api calls with list comps

list comps are faster than generator expressions, even more so in
python 3.12 since https://peps.python.org/pep-0709/

https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions/62709748#62709748

* more readable
  • Loading branch information
bdraco authored Feb 16, 2024
1 parent 59d7bce commit 1608e05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
9 changes: 4 additions & 5 deletions homeassistant/components/config/device_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ def websocket_list_devices(
f'"success":true,"result": ['
).encode()
# Concatenate cached entity registry item JSON serializations
msg_json = (
msg_json_prefix
+ b",".join(
inner = b",".join(
[
entry.json_repr
for entry in registry.devices.values()
if entry.json_repr is not None
)
+ b"]}"
]
)
msg_json = b"".join((msg_json_prefix, inner, b"]}"))
connection.send_message(msg_json)


Expand Down
18 changes: 8 additions & 10 deletions homeassistant/components/config/entity_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ def websocket_list_entities(
'"success":true,"result": ['
).encode()
# Concatenate cached entity registry item JSON serializations
msg_json = (
msg_json_prefix
+ b",".join(
inner = b",".join(
[
entry.partial_json_repr
for entry in registry.entities.values()
if entry.partial_json_repr is not None
)
+ b"]}"
]
)
msg_json = b"".join((msg_json_prefix, inner, b"]}"))
connection.send_message(msg_json)


Expand All @@ -77,15 +76,14 @@ def websocket_list_entities_for_display(
f'"result":{{"entity_categories":{_ENTITY_CATEGORIES_JSON},"entities":['
).encode()
# Concatenate cached entity registry item JSON serializations
msg_json = (
msg_json_prefix
+ b",".join(
inner = b",".join(
[
entry.display_json_repr
for entry in registry.entities.values()
if entry.disabled_by is None and entry.display_json_repr is not None
)
+ b"]}}"
]
)
msg_json = b"".join((msg_json_prefix, inner, b"]}}"))
connection.send_message(msg_json)


Expand Down

0 comments on commit 1608e05

Please sign in to comment.