Guard expensive casts in performance sensitive spots with if TYPE_CHECKING (#71960)

This commit is contained in:
J. Nick Koston 2022-05-16 14:15:04 -05:00 committed by GitHub
parent bac2dce5ee
commit fb7aead756
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -406,7 +406,12 @@ class HomeAssistant:
if asyncio.iscoroutine(target): if asyncio.iscoroutine(target):
return self.async_create_task(target) return self.async_create_task(target)
target = cast(Callable[..., Union[Coroutine[Any, Any, _R], _R]], target) # This code path is performance sensitive and uses
# if TYPE_CHECKING to avoid the overhead of constructing
# the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960
if TYPE_CHECKING:
target = cast(Callable[..., Union[Coroutine[Any, Any, _R], _R]], target)
return self.async_add_hass_job(HassJob(target), *args) return self.async_add_hass_job(HassJob(target), *args)
@overload @overload
@ -434,17 +439,25 @@ class HomeAssistant:
args: parameters for method to call. args: parameters for method to call.
""" """
task: asyncio.Future[_R] task: asyncio.Future[_R]
# This code path is performance sensitive and uses
# if TYPE_CHECKING to avoid the overhead of constructing
# the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960
if hassjob.job_type == HassJobType.Coroutinefunction: if hassjob.job_type == HassJobType.Coroutinefunction:
task = self.loop.create_task( if TYPE_CHECKING:
cast(Callable[..., Coroutine[Any, Any, _R]], hassjob.target)(*args) hassjob.target = cast(
) Callable[..., Coroutine[Any, Any, _R]], hassjob.target
)
task = self.loop.create_task(hassjob.target(*args))
elif hassjob.job_type == HassJobType.Callback: elif hassjob.job_type == HassJobType.Callback:
self.loop.call_soon(cast(Callable[..., _R], hassjob.target), *args) if TYPE_CHECKING:
hassjob.target = cast(Callable[..., _R], hassjob.target)
self.loop.call_soon(hassjob.target, *args)
return None return None
else: else:
task = self.loop.run_in_executor( if TYPE_CHECKING:
None, cast(Callable[..., _R], hassjob.target), *args hassjob.target = cast(Callable[..., _R], hassjob.target)
) task = self.loop.run_in_executor(None, hassjob.target, *args)
# If a task is scheduled # If a task is scheduled
if self._track_task: if self._track_task:
@ -522,8 +535,14 @@ class HomeAssistant:
hassjob: HassJob hassjob: HassJob
args: parameters for method to call. args: parameters for method to call.
""" """
# This code path is performance sensitive and uses
# if TYPE_CHECKING to avoid the overhead of constructing
# the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960
if hassjob.job_type == HassJobType.Callback: if hassjob.job_type == HassJobType.Callback:
cast(Callable[..., _R], hassjob.target)(*args) if TYPE_CHECKING:
hassjob.target = cast(Callable[..., _R], hassjob.target)
hassjob.target(*args)
return None return None
return self.async_add_hass_job(hassjob, *args) return self.async_add_hass_job(hassjob, *args)
@ -565,7 +584,12 @@ class HomeAssistant:
if asyncio.iscoroutine(target): if asyncio.iscoroutine(target):
return self.async_create_task(target) return self.async_create_task(target)
target = cast(Callable[..., Union[Coroutine[Any, Any, _R], _R]], target) # This code path is performance sensitive and uses
# if TYPE_CHECKING to avoid the overhead of constructing
# the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960
if TYPE_CHECKING:
target = cast(Callable[..., Union[Coroutine[Any, Any, _R], _R]], target)
return self.async_run_hass_job(HassJob(target), *args) return self.async_run_hass_job(HassJob(target), *args)
def block_till_done(self) -> None: def block_till_done(self) -> None: