Avoid creating tasks for automation and script validation (#111181)

These functions created tasks to run small validators, and the cost of
creating all the tasks was more expensive than running the validators
themselves. Since the code is unlikely to suspend its more efficient to
await them in series.
This commit is contained in:
J. Nick Koston 2024-02-23 08:41:36 -10:00 committed by GitHub
parent 5d421e249f
commit 3877a56d23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 13 deletions

View File

@ -1,7 +1,6 @@
"""Config validation helper for the automation integration.""" """Config validation helper for the automation integration."""
from __future__ import annotations from __future__ import annotations
import asyncio
from collections.abc import Mapping from collections.abc import Mapping
from contextlib import suppress from contextlib import suppress
from typing import Any from typing import Any
@ -255,15 +254,15 @@ async def async_validate_config_item(
async def async_validate_config(hass: HomeAssistant, config: ConfigType) -> ConfigType: async def async_validate_config(hass: HomeAssistant, config: ConfigType) -> ConfigType:
"""Validate config.""" """Validate config."""
# No gather here since _try_async_validate_config_item is unlikely to suspend
# and the cost of creating many tasks is not worth the benefit.
automations = list( automations = list(
filter( filter(
lambda x: x is not None, lambda x: x is not None,
await asyncio.gather( [
*( await _try_async_validate_config_item(hass, p_config)
_try_async_validate_config_item(hass, p_config) for _, p_config in config_per_platform(config, DOMAIN)
for _, p_config in config_per_platform(config, DOMAIN) ],
)
),
) )
) )

View File

@ -1055,9 +1055,9 @@ async def async_validate_conditions_config(
hass: HomeAssistant, conditions: list[ConfigType] hass: HomeAssistant, conditions: list[ConfigType]
) -> list[ConfigType | Template]: ) -> list[ConfigType | Template]:
"""Validate config.""" """Validate config."""
return await asyncio.gather( # No gather here because async_validate_condition_config is unlikely
*(async_validate_condition_config(hass, cond) for cond in conditions) # to suspend and the overhead of creating many tasks is not worth it
) return [await async_validate_condition_config(hass, cond) for cond in conditions]
@callback @callback

View File

@ -274,9 +274,9 @@ async def async_validate_actions_config(
hass: HomeAssistant, actions: list[ConfigType] hass: HomeAssistant, actions: list[ConfigType]
) -> list[ConfigType]: ) -> list[ConfigType]:
"""Validate a list of actions.""" """Validate a list of actions."""
return await asyncio.gather( # No gather here because async_validate_action_config is unlikely
*(async_validate_action_config(hass, action) for action in actions) # to suspend and the overhead of creating many tasks is not worth it
) return [await async_validate_action_config(hass, action) for action in actions]
async def async_validate_action_config( async def async_validate_action_config(