Improve performance of generating non-cryptographically secure uuids (#41314)

This commit is contained in:
J. Nick Koston 2020-10-07 09:37:01 -05:00 committed by GitHub
parent 451f27bba1
commit 8d5c124deb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 17 deletions

View File

@ -139,7 +139,7 @@ class ConfigEntry:
) -> None: ) -> None:
"""Initialize a config entry.""" """Initialize a config entry."""
# Unique id of the 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. # Version of the configuration.
self.version = version self.version = version

View File

@ -511,7 +511,7 @@ class Context:
user_id: str = attr.ib(default=None) user_id: str = attr.ib(default=None)
parent_id: Optional[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: def as_dict(self) -> dict:
"""Return a dictionary representation of the context.""" """Return a dictionary representation of the context."""

View File

@ -26,7 +26,7 @@ class AreaEntry:
"""Area Registry Entry.""" """Area Registry Entry."""
name: Optional[str] = attr.ib(default=None) 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: class AreaRegistry:

View File

@ -73,7 +73,7 @@ class DeviceEntry:
area_id: str = attr.ib(default=None) area_id: str = attr.ib(default=None)
name_by_user: str = attr.ib(default=None) name_by_user: str = attr.ib(default=None)
entry_type: 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. # This value is not stored, just used to keep track of events to fire.
is_new: bool = attr.ib(default=False) is_new: bool = attr.ib(default=False)

View File

@ -1,15 +1,12 @@
"""Helpers to generate uuids.""" """Helpers to generate uuids."""
import random from random import getrandbits
import uuid
def uuid_v1mc_hex() -> str: def random_uuid_hex() -> str:
"""Generate a uuid1 with a random multicast MAC address. """Generate a random UUID hex.
The uuid1 uses a random multicast MAC address instead of the real MAC address This uuid should not be used for cryptographically secure
of the machine without the overhead of calling the getrandom() system call. operations.
This is effectively equivalent to PostgreSQL's uuid_generate_v1mc() function
""" """
return uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex return "%032x" % getrandbits(32 * 4)

View File

@ -5,7 +5,7 @@ import uuid
import homeassistant.util.uuid as uuid_util import homeassistant.util.uuid as uuid_util
async def test_uuid_v1mc_hex(): async def test_uuid_util_random_uuid_hex():
"""Verify we can generate a uuid_v1mc and return hex.""" """Verify we can generate a random uuid."""
assert len(uuid_util.uuid_v1mc_hex()) == 32 assert len(uuid_util.random_uuid_hex()) == 32
assert uuid.UUID(uuid_util.uuid_v1mc_hex()) assert uuid.UUID(uuid_util.random_uuid_hex())