From 9cf45882a7b912c8f98fb9e695491e117be40907 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 16 Feb 2024 00:58:11 -0600 Subject: [PATCH] Use bytes join fast path for large states payload (#110694) b"".join has a fast path for when there are more than two bytes-strings to combine https://github.com/python/cpython/blob/f383ca1a6fa1a2a83c8c1a0e56cf997c77fa2893/Objects/stringlib/join.h#L123 --- homeassistant/components/api/__init__.py | 2 +- homeassistant/components/websocket_api/commands.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/api/__init__.py b/homeassistant/components/api/__init__.py index 266703bbab463a..38d791d0ddaa84 100644 --- a/homeassistant/components/api/__init__.py +++ b/homeassistant/components/api/__init__.py @@ -222,7 +222,7 @@ def get(self, request: web.Request) -> web.Response: if entity_perm(state.entity_id, "read") ) response = web.Response( - body=b"[" + b",".join(states) + b"]", + body=b"".join((b"[", b",".join(states), b"]")), content_type=CONTENT_TYPE_JSON, zlib_executor_size=32768, ) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index c088acc6e00e44..c2f7a8ce669cc8 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -356,7 +356,9 @@ def _send_handle_get_states_response( ) -> None: """Send handle get states response.""" connection.send_message( - construct_result_message(msg_id, b"[" + b",".join(serialized_states) + b"]") + construct_result_message( + msg_id, b"".join((b"[", b",".join(serialized_states), b"]")) + ) )