Guard expensive cast
s in performance sensitive spots with if TYPE_CHECKING
(#71960)
This commit is contained in:
parent
bac2dce5ee
commit
fb7aead756
1 changed files with 34 additions and 10 deletions
|
@ -406,7 +406,12 @@ class HomeAssistant:
|
|||
if asyncio.iscoroutine(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)
|
||||
|
||||
@overload
|
||||
|
@ -434,17 +439,25 @@ class HomeAssistant:
|
|||
args: parameters for method to call.
|
||||
"""
|
||||
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:
|
||||
task = self.loop.create_task(
|
||||
cast(Callable[..., Coroutine[Any, Any, _R]], hassjob.target)(*args)
|
||||
)
|
||||
if TYPE_CHECKING:
|
||||
hassjob.target = cast(
|
||||
Callable[..., Coroutine[Any, Any, _R]], hassjob.target
|
||||
)
|
||||
task = self.loop.create_task(hassjob.target(*args))
|
||||
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
|
||||
else:
|
||||
task = self.loop.run_in_executor(
|
||||
None, cast(Callable[..., _R], hassjob.target), *args
|
||||
)
|
||||
if TYPE_CHECKING:
|
||||
hassjob.target = cast(Callable[..., _R], hassjob.target)
|
||||
task = self.loop.run_in_executor(None, hassjob.target, *args)
|
||||
|
||||
# If a task is scheduled
|
||||
if self._track_task:
|
||||
|
@ -522,8 +535,14 @@ class HomeAssistant:
|
|||
hassjob: HassJob
|
||||
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:
|
||||
cast(Callable[..., _R], hassjob.target)(*args)
|
||||
if TYPE_CHECKING:
|
||||
hassjob.target = cast(Callable[..., _R], hassjob.target)
|
||||
hassjob.target(*args)
|
||||
return None
|
||||
|
||||
return self.async_add_hass_job(hassjob, *args)
|
||||
|
@ -565,7 +584,12 @@ class HomeAssistant:
|
|||
if asyncio.iscoroutine(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)
|
||||
|
||||
def block_till_done(self) -> None:
|
||||
|
|
Loading…
Add table
Reference in a new issue