From b4fec762bc314db831442c76098b85e389658b6b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" <nick@koston.org> Date: Wed, 5 Apr 2023 14:52:23 -1000 Subject: [PATCH] Switch to fnv-hash-fast from fnvhash (#90761) * Switch to fnv-hash-fast from fnvhash Replaces the pure python implemention with a fast cpp one when available (with fallback to pure python) changelog: https://github.com/bdraco/fnv-hash-fast/releases/tag/v0.3.1 source: https://github.com/bdraco/fnv-hash-fast/tree/main/src/fnv_hash_fast * Apply suggestions from code review * lint --- homeassistant/components/homekit/aidmanager.py | 2 +- homeassistant/components/homekit/manifest.json | 2 +- homeassistant/components/recorder/db_schema.py | 9 +++------ homeassistant/components/recorder/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/homekit/test_aidmanager.py | 4 ++-- tests/components/recorder/db_schema_25.py | 2 +- tests/components/recorder/db_schema_28.py | 2 +- tests/components/recorder/db_schema_30.py | 2 +- 11 files changed, 14 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/homekit/aidmanager.py b/homeassistant/components/homekit/aidmanager.py index 4addbeb1e23..9c3d9e7929c 100644 --- a/homeassistant/components/homekit/aidmanager.py +++ b/homeassistant/components/homekit/aidmanager.py @@ -13,7 +13,7 @@ from __future__ import annotations from collections.abc import Generator import random -from fnvhash import fnv1a_32 +from fnv_hash_fast import fnv1a_32 from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er diff --git a/homeassistant/components/homekit/manifest.json b/homeassistant/components/homekit/manifest.json index 80eea60b9e8..def024d2046 100644 --- a/homeassistant/components/homekit/manifest.json +++ b/homeassistant/components/homekit/manifest.json @@ -10,7 +10,7 @@ "loggers": ["pyhap"], "requirements": [ "HAP-python==4.6.0", - "fnvhash==0.1.0", + "fnv-hash-fast==0.3.1", "PyQRCode==1.2.1", "base36==0.1.1" ], diff --git a/homeassistant/components/recorder/db_schema.py b/homeassistant/components/recorder/db_schema.py index c2252e9f68f..1619cc73513 100644 --- a/homeassistant/components/recorder/db_schema.py +++ b/homeassistant/components/recorder/db_schema.py @@ -3,13 +3,12 @@ from __future__ import annotations from collections.abc import Callable from datetime import datetime, timedelta -from functools import lru_cache import logging import time from typing import Any, cast import ciso8601 -from fnvhash import fnv1a_32 +from fnv_hash_fast import fnv1a_32 from sqlalchemy import ( JSON, BigInteger, @@ -343,10 +342,9 @@ class EventData(Base): return bytes_result @staticmethod - @lru_cache def hash_shared_data_bytes(shared_data_bytes: bytes) -> int: """Return the hash of json encoded shared data.""" - return cast(int, fnv1a_32(shared_data_bytes)) + return fnv1a_32(shared_data_bytes) def to_native(self) -> dict[str, Any]: """Convert to an event data dictionary.""" @@ -592,10 +590,9 @@ class StateAttributes(Base): return bytes_result @staticmethod - @lru_cache(maxsize=2048) def hash_shared_attrs_bytes(shared_attrs_bytes: bytes) -> int: """Return the hash of json encoded shared attributes.""" - return cast(int, fnv1a_32(shared_attrs_bytes)) + return fnv1a_32(shared_attrs_bytes) def to_native(self) -> dict[str, Any]: """Convert to a state attributes dictionary.""" diff --git a/homeassistant/components/recorder/manifest.json b/homeassistant/components/recorder/manifest.json index 596d9a89849..97eb9d062b0 100644 --- a/homeassistant/components/recorder/manifest.json +++ b/homeassistant/components/recorder/manifest.json @@ -6,5 +6,5 @@ "integration_type": "system", "iot_class": "local_push", "quality_scale": "internal", - "requirements": ["sqlalchemy==2.0.8", "fnvhash==0.1.0"] + "requirements": ["sqlalchemy==2.0.8", "fnv-hash-fast==0.3.1"] } diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index cd48245147b..b7f7bf53f5a 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -20,7 +20,7 @@ certifi>=2021.5.30 ciso8601==2.3.0 cryptography==40.0.1 dbus-fast==1.84.2 -fnvhash==0.1.0 +fnv-hash-fast==0.3.1 ha-av==10.0.0 hass-nabucasa==0.63.1 hassil==1.0.6 diff --git a/requirements_all.txt b/requirements_all.txt index 6841968424e..b9fa171a6e6 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -729,7 +729,7 @@ flux_led==0.28.36 # homeassistant.components.homekit # homeassistant.components.recorder -fnvhash==0.1.0 +fnv-hash-fast==0.3.1 # homeassistant.components.foobot foobot_async==1.0.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8d69556e8db..6e10a2983d4 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -557,7 +557,7 @@ flux_led==0.28.36 # homeassistant.components.homekit # homeassistant.components.recorder -fnvhash==0.1.0 +fnv-hash-fast==0.3.1 # homeassistant.components.foobot foobot_async==1.0.0 diff --git a/tests/components/homekit/test_aidmanager.py b/tests/components/homekit/test_aidmanager.py index 64a44cd38a9..18e654cb4ed 100644 --- a/tests/components/homekit/test_aidmanager.py +++ b/tests/components/homekit/test_aidmanager.py @@ -2,7 +2,7 @@ import os from unittest.mock import patch -from fnvhash import fnv1a_32 +from fnv_hash_fast import fnv1a_32 from homeassistant.components.homekit.aidmanager import ( AccessoryAidStorage, @@ -386,7 +386,7 @@ async def test_aid_generation_no_unique_ids_handles_collision( await aid_storage.async_save() await hass.async_block_till_done() - with patch("fnvhash.fnv1a_32", side_effect=Exception): + with patch("fnv_hash_fast.fnv1a_32", side_effect=Exception): aid_storage = AccessoryAidStorage(hass, config_entry) await aid_storage.async_initialize() diff --git a/tests/components/recorder/db_schema_25.py b/tests/components/recorder/db_schema_25.py index 7f276d42df8..291fdb1231d 100644 --- a/tests/components/recorder/db_schema_25.py +++ b/tests/components/recorder/db_schema_25.py @@ -6,7 +6,7 @@ import json import logging from typing import Any, TypedDict, cast, overload -from fnvhash import fnv1a_32 +from fnv_hash_fast import fnv1a_32 from sqlalchemy import ( BigInteger, Boolean, diff --git a/tests/components/recorder/db_schema_28.py b/tests/components/recorder/db_schema_28.py index 8127cb3f26f..7e88d6a5548 100644 --- a/tests/components/recorder/db_schema_28.py +++ b/tests/components/recorder/db_schema_28.py @@ -11,7 +11,7 @@ import logging import time from typing import Any, TypedDict, cast, overload -from fnvhash import fnv1a_32 +from fnv_hash_fast import fnv1a_32 from sqlalchemy import ( BigInteger, Boolean, diff --git a/tests/components/recorder/db_schema_30.py b/tests/components/recorder/db_schema_30.py index 9c5efaea1d3..40417752719 100644 --- a/tests/components/recorder/db_schema_30.py +++ b/tests/components/recorder/db_schema_30.py @@ -12,7 +12,7 @@ import time from typing import Any, TypedDict, cast, overload import ciso8601 -from fnvhash import fnv1a_32 +from fnv_hash_fast import fnv1a_32 from sqlalchemy import ( JSON, BigInteger, -- GitLab