From 6e2025a7487caa4c843b1180398bd5f1b08d4b23 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 22 Jul 2020 18:53:01 -1000 Subject: [PATCH] Use postgresql style uuid generation (uuid_generate_v1mc) for Context uuids (#38089) This avoids the syscall to getrandom() every time we generate a uuid. --- homeassistant/core.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 3bfe3c3660c..18493d85629 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -12,6 +12,7 @@ from ipaddress import ip_address import logging import os import pathlib +import random import re import threading from time import monotonic @@ -458,7 +459,13 @@ class Context: user_id: str = attr.ib(default=None) parent_id: Optional[str] = attr.ib(default=None) - id: str = attr.ib(factory=lambda: uuid.uuid4().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 + id: str = attr.ib( + factory=lambda: uuid.uuid1(node=random.getrandbits(48) | (1 << 40)).hex + ) def as_dict(self) -> dict: """Return a dictionary representation of the context."""