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
This commit is contained in:
J. Nick Koston 2023-04-05 14:52:23 -10:00 committed by GitHub
parent a8f1d033a0
commit b4fec762bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 14 additions and 17 deletions

View File

@ -13,7 +13,7 @@ from __future__ import annotations
from collections.abc import Generator from collections.abc import Generator
import random import random
from fnvhash import fnv1a_32 from fnv_hash_fast import fnv1a_32
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er

View File

@ -10,7 +10,7 @@
"loggers": ["pyhap"], "loggers": ["pyhap"],
"requirements": [ "requirements": [
"HAP-python==4.6.0", "HAP-python==4.6.0",
"fnvhash==0.1.0", "fnv-hash-fast==0.3.1",
"PyQRCode==1.2.1", "PyQRCode==1.2.1",
"base36==0.1.1" "base36==0.1.1"
], ],

View File

@ -3,13 +3,12 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import lru_cache
import logging import logging
import time import time
from typing import Any, cast from typing import Any, cast
import ciso8601 import ciso8601
from fnvhash import fnv1a_32 from fnv_hash_fast import fnv1a_32
from sqlalchemy import ( from sqlalchemy import (
JSON, JSON,
BigInteger, BigInteger,
@ -343,10 +342,9 @@ class EventData(Base):
return bytes_result return bytes_result
@staticmethod @staticmethod
@lru_cache
def hash_shared_data_bytes(shared_data_bytes: bytes) -> int: def hash_shared_data_bytes(shared_data_bytes: bytes) -> int:
"""Return the hash of json encoded shared data.""" """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]: def to_native(self) -> dict[str, Any]:
"""Convert to an event data dictionary.""" """Convert to an event data dictionary."""
@ -592,10 +590,9 @@ class StateAttributes(Base):
return bytes_result return bytes_result
@staticmethod @staticmethod
@lru_cache(maxsize=2048)
def hash_shared_attrs_bytes(shared_attrs_bytes: bytes) -> int: def hash_shared_attrs_bytes(shared_attrs_bytes: bytes) -> int:
"""Return the hash of json encoded shared attributes.""" """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]: def to_native(self) -> dict[str, Any]:
"""Convert to a state attributes dictionary.""" """Convert to a state attributes dictionary."""

View File

@ -6,5 +6,5 @@
"integration_type": "system", "integration_type": "system",
"iot_class": "local_push", "iot_class": "local_push",
"quality_scale": "internal", "quality_scale": "internal",
"requirements": ["sqlalchemy==2.0.8", "fnvhash==0.1.0"] "requirements": ["sqlalchemy==2.0.8", "fnv-hash-fast==0.3.1"]
} }

View File

@ -20,7 +20,7 @@ certifi>=2021.5.30
ciso8601==2.3.0 ciso8601==2.3.0
cryptography==40.0.1 cryptography==40.0.1
dbus-fast==1.84.2 dbus-fast==1.84.2
fnvhash==0.1.0 fnv-hash-fast==0.3.1
ha-av==10.0.0 ha-av==10.0.0
hass-nabucasa==0.63.1 hass-nabucasa==0.63.1
hassil==1.0.6 hassil==1.0.6

View File

@ -729,7 +729,7 @@ flux_led==0.28.36
# homeassistant.components.homekit # homeassistant.components.homekit
# homeassistant.components.recorder # homeassistant.components.recorder
fnvhash==0.1.0 fnv-hash-fast==0.3.1
# homeassistant.components.foobot # homeassistant.components.foobot
foobot_async==1.0.0 foobot_async==1.0.0

View File

@ -557,7 +557,7 @@ flux_led==0.28.36
# homeassistant.components.homekit # homeassistant.components.homekit
# homeassistant.components.recorder # homeassistant.components.recorder
fnvhash==0.1.0 fnv-hash-fast==0.3.1
# homeassistant.components.foobot # homeassistant.components.foobot
foobot_async==1.0.0 foobot_async==1.0.0

View File

@ -2,7 +2,7 @@
import os import os
from unittest.mock import patch from unittest.mock import patch
from fnvhash import fnv1a_32 from fnv_hash_fast import fnv1a_32
from homeassistant.components.homekit.aidmanager import ( from homeassistant.components.homekit.aidmanager import (
AccessoryAidStorage, AccessoryAidStorage,
@ -386,7 +386,7 @@ async def test_aid_generation_no_unique_ids_handles_collision(
await aid_storage.async_save() await aid_storage.async_save()
await hass.async_block_till_done() 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) aid_storage = AccessoryAidStorage(hass, config_entry)
await aid_storage.async_initialize() await aid_storage.async_initialize()

View File

@ -6,7 +6,7 @@ import json
import logging import logging
from typing import Any, TypedDict, cast, overload from typing import Any, TypedDict, cast, overload
from fnvhash import fnv1a_32 from fnv_hash_fast import fnv1a_32
from sqlalchemy import ( from sqlalchemy import (
BigInteger, BigInteger,
Boolean, Boolean,

View File

@ -11,7 +11,7 @@ import logging
import time import time
from typing import Any, TypedDict, cast, overload from typing import Any, TypedDict, cast, overload
from fnvhash import fnv1a_32 from fnv_hash_fast import fnv1a_32
from sqlalchemy import ( from sqlalchemy import (
BigInteger, BigInteger,
Boolean, Boolean,

View File

@ -12,7 +12,7 @@ import time
from typing import Any, TypedDict, cast, overload from typing import Any, TypedDict, cast, overload
import ciso8601 import ciso8601
from fnvhash import fnv1a_32 from fnv_hash_fast import fnv1a_32
from sqlalchemy import ( from sqlalchemy import (
JSON, JSON,
BigInteger, BigInteger,