Improve performance of generating non-cryptographically secure uuids (#41314)
This commit is contained in:
parent
451f27bba1
commit
8d5c124deb
6 changed files with 14 additions and 17 deletions
|
@ -139,7 +139,7 @@ class ConfigEntry:
|
|||
) -> None:
|
||||
"""Initialize a config entry."""
|
||||
# Unique id of the config entry
|
||||
self.entry_id = entry_id or uuid_util.uuid_v1mc_hex()
|
||||
self.entry_id = entry_id or uuid_util.random_uuid_hex()
|
||||
|
||||
# Version of the configuration.
|
||||
self.version = version
|
||||
|
|
|
@ -511,7 +511,7 @@ class Context:
|
|||
|
||||
user_id: str = attr.ib(default=None)
|
||||
parent_id: Optional[str] = attr.ib(default=None)
|
||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
||||
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||
|
||||
def as_dict(self) -> dict:
|
||||
"""Return a dictionary representation of the context."""
|
||||
|
|
|
@ -26,7 +26,7 @@ class AreaEntry:
|
|||
"""Area Registry Entry."""
|
||||
|
||||
name: Optional[str] = attr.ib(default=None)
|
||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
||||
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||
|
||||
|
||||
class AreaRegistry:
|
||||
|
|
|
@ -73,7 +73,7 @@ class DeviceEntry:
|
|||
area_id: str = attr.ib(default=None)
|
||||
name_by_user: str = attr.ib(default=None)
|
||||
entry_type: str = attr.ib(default=None)
|
||||
id: str = attr.ib(factory=uuid_util.uuid_v1mc_hex)
|
||||
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
|
||||
# This value is not stored, just used to keep track of events to fire.
|
||||
is_new: bool = attr.ib(default=False)
|
||||
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
"""Helpers to generate uuids."""
|
||||
|
||||
import random
|
||||
import uuid
|
||||
from random import getrandbits
|
||||
|
||||
|
||||
def uuid_v1mc_hex() -> str:
|
||||
"""Generate a uuid1 with a random multicast MAC address.
|
||||
def random_uuid_hex() -> str:
|
||||
"""Generate a random UUID hex.
|
||||
|
||||
The uuid1 uses a random multicast MAC address instead of the real MAC address
|
||||
of the machine without the overhead of calling the getrandom() system call.
|
||||
|
||||
This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function
|
||||
This uuid should not be used for cryptographically secure
|
||||
operations.
|
||||
"""
|
||||
return uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex
|
||||
return "%032x" % getrandbits(32 * 4)
|
||||
|
|
|
@ -5,7 +5,7 @@ import uuid
|
|||
import homeassistant.util.uuid as uuid_util
|
||||
|
||||
|
||||
async def test_uuid_v1mc_hex():
|
||||
"""Verify we can generate a uuid_v1mc and return hex."""
|
||||
assert len(uuid_util.uuid_v1mc_hex()) == 32
|
||||
assert uuid.UUID(uuid_util.uuid_v1mc_hex())
|
||||
async def test_uuid_util_random_uuid_hex():
|
||||
"""Verify we can generate a random uuid."""
|
||||
assert len(uuid_util.random_uuid_hex()) == 32
|
||||
assert uuid.UUID(uuid_util.random_uuid_hex())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue