Small speed up to setting up integrations and config entries (#117278)

* Small speed up to setting up integration and config entries

When profiling tests, I noticed many calls to get_running_loop. In the places
where we are already in a coro, pass the existing loop so it does not have to
be looked up. I did not do this for places were we are not in a coro since there
is risk that an integration could be doing a non-thread-safe call and its better
that the code raises when trying to fetch the running loop vs the performance
improvement for these cases.

* fix merge

* missed some
This commit is contained in:
J. Nick Koston 2024-05-12 11:39:20 +09:00 committed by GitHub
parent 15825b9444
commit b061e7d1aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 9 deletions

View File

@ -427,7 +427,11 @@ async def async_from_config_dict(
if not all( if not all(
await asyncio.gather( await asyncio.gather(
*( *(
create_eager_task(async_setup_component(hass, domain, config)) create_eager_task(
async_setup_component(hass, domain, config),
name=f"bootstrap setup {domain}",
loop=hass.loop,
)
for domain in CORE_INTEGRATIONS for domain in CORE_INTEGRATIONS
) )
) )

View File

@ -1997,7 +1997,11 @@ class ConfigEntries:
*( *(
create_eager_task( create_eager_task(
self._async_forward_entry_setup(entry, platform, False), self._async_forward_entry_setup(entry, platform, False),
name=f"config entry forward setup {entry.title} {entry.domain} {entry.entry_id} {platform}", name=(
f"config entry forward setup {entry.title} "
f"{entry.domain} {entry.entry_id} {platform}"
),
loop=self.hass.loop,
) )
for platform in platforms for platform in platforms
) )
@ -2050,7 +2054,11 @@ class ConfigEntries:
*( *(
create_eager_task( create_eager_task(
self.async_forward_entry_unload(entry, platform), self.async_forward_entry_unload(entry, platform),
name=f"config entry forward unload {entry.title} {entry.domain} {entry.entry_id} {platform}", name=(
f"config entry forward unload {entry.title} "
f"{entry.domain} {entry.entry_id} {platform}"
),
loop=self.hass.loop,
) )
for platform in platforms for platform in platforms
) )

View File

@ -354,7 +354,7 @@ class EntityPlatform:
try: try:
awaitable = async_create_setup_awaitable() awaitable = async_create_setup_awaitable()
if asyncio.iscoroutine(awaitable): if asyncio.iscoroutine(awaitable):
awaitable = create_eager_task(awaitable) awaitable = create_eager_task(awaitable, loop=hass.loop)
async with hass.timeout.async_timeout(SLOW_SETUP_MAX_WAIT, self.domain): async with hass.timeout.async_timeout(SLOW_SETUP_MAX_WAIT, self.domain):
await asyncio.shield(awaitable) await asyncio.shield(awaitable)
@ -536,7 +536,7 @@ class EntityPlatform:
event loop and will finish faster if we run them concurrently. event loop and will finish faster if we run them concurrently.
""" """
results: list[BaseException | None] | None = None results: list[BaseException | None] | None = None
tasks = [create_eager_task(coro) for coro in coros] tasks = [create_eager_task(coro, loop=self.hass.loop) for coro in coros]
try: try:
async with self.hass.timeout.async_timeout(timeout, self.domain): async with self.hass.timeout.async_timeout(timeout, self.domain):
results = await asyncio.gather(*tasks, return_exceptions=True) results = await asyncio.gather(*tasks, return_exceptions=True)
@ -1035,7 +1035,9 @@ class EntityPlatform:
return return
if tasks := [ if tasks := [
create_eager_task(entity.async_update_ha_state(True)) create_eager_task(
entity.async_update_ha_state(True), loop=self.hass.loop
)
for entity in self.entities.values() for entity in self.entities.values()
if entity.should_poll if entity.should_poll
]: ]:

View File

@ -300,7 +300,7 @@ async def _async_setup_component(
# If for some reason the background task in bootstrap was too slow # If for some reason the background task in bootstrap was too slow
# or the integration was added after bootstrap, we will load them here. # or the integration was added after bootstrap, we will load them here.
load_translations_task = create_eager_task( load_translations_task = create_eager_task(
translation.async_load_integrations(hass, integration_set) translation.async_load_integrations(hass, integration_set), loop=hass.loop
) )
# Validate all dependencies exist and there are no circular dependencies # Validate all dependencies exist and there are no circular dependencies
if not await integration.resolve_dependencies(): if not await integration.resolve_dependencies():
@ -448,7 +448,11 @@ async def _async_setup_component(
*( *(
create_eager_task( create_eager_task(
entry.async_setup_locked(hass, integration=integration), entry.async_setup_locked(hass, integration=integration),
name=f"config entry setup {entry.title} {entry.domain} {entry.entry_id}", name=(
f"config entry setup {entry.title} {entry.domain} "
f"{entry.entry_id}"
),
loop=hass.loop,
) )
for entry in entries for entry in entries
) )

View File

@ -567,7 +567,8 @@ async def hass(
await asyncio.gather( await asyncio.gather(
*( *(
create_eager_task( create_eager_task(
hass.config_entries.async_unload(config_entry.entry_id) hass.config_entries.async_unload(config_entry.entry_id),
loop=hass.loop,
) )
for config_entry in loaded_entries for config_entry in loaded_entries
) )