Improve performance of generate diffs of state change events (#124601)

dict comps are inlined in cpython 3.12+
pull/124620/head
J. Nick Koston 2024-08-25 20:49:31 -10:00 committed by GitHub
parent 0591b5e47b
commit 76ebb0df08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 3 deletions

View File

@ -224,9 +224,12 @@ def _state_diff_event(
if (old_attributes := old_state.attributes) != (
new_attributes := new_state.attributes
):
for key, value in new_attributes.items():
if old_attributes.get(key) != value:
additions.setdefault(COMPRESSED_STATE_ATTRIBUTES, {})[key] = value
if added := {
key: value
for key, value in new_attributes.items()
if key not in old_attributes or old_attributes[key] != value
}:
additions[COMPRESSED_STATE_ATTRIBUTES] = added
if removed := old_attributes.keys() - new_attributes:
# sets are not JSON serializable by default so we convert to list
# here if there are any values to avoid jumping into the json_encoder_default