From 979f80148857c321a23504e0cef5d4ce894a23cc Mon Sep 17 00:00:00 2001 From: nierob <nierob@users.noreply.github.com> Date: Fri, 19 Jul 2019 20:36:18 +0000 Subject: [PATCH] Avoid creating temporary lists (#25317) That gives nano performance improvements as *() is slightly faster then *[]. --- homeassistant/auth/__init__.py | 8 ++--- homeassistant/bootstrap.py | 32 +++++++++---------- .../components/climate/reproduce_state.py | 4 +-- .../components/device_automation/__init__.py | 4 +-- .../components/device_tracker/setup.py | 4 +-- .../components/google_assistant/smart_home.py | 4 +-- .../media_player/reproduce_state.py | 4 +-- homeassistant/components/nextbus/sensor.py | 4 +-- .../components/smartthings/__init__.py | 4 +-- .../components/system_health/__init__.py | 4 +-- homeassistant/helpers/service.py | 4 +-- homeassistant/helpers/state.py | 4 +-- homeassistant/loader.py | 4 +-- 13 files changed, 42 insertions(+), 42 deletions(-) diff --git a/homeassistant/auth/__init__.py b/homeassistant/auth/__init__.py index 9e4b9d09d78..2f9465d2398 100644 --- a/homeassistant/auth/__init__.py +++ b/homeassistant/auth/__init__.py @@ -38,8 +38,8 @@ async def auth_manager_from_config( store = auth_store.AuthStore(hass) if provider_configs: providers = await asyncio.gather( - *[auth_provider_from_config(hass, store, config) - for config in provider_configs]) + *(auth_provider_from_config(hass, store, config) + for config in provider_configs)) else: providers = () # So returned auth providers are in same order as config @@ -50,8 +50,8 @@ async def auth_manager_from_config( if module_configs: modules = await asyncio.gather( - *[auth_mfa_module_from_config(hass, config) - for config in module_configs]) + *(auth_mfa_module_from_config(hass, config) + for config in module_configs)) else: modules = () # So returned auth modules are in same order as config diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 79e5ec248ae..85611be8d65 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -272,25 +272,25 @@ async def _async_set_up_integrations( debuggers = domains & DEBUGGER_INTEGRATIONS if debuggers: _LOGGER.debug("Starting up debuggers %s", debuggers) - await asyncio.gather(*[ + await asyncio.gather(*( async_setup_component(hass, domain, config) - for domain in debuggers]) + for domain in debuggers)) domains -= DEBUGGER_INTEGRATIONS # Resolve all dependencies of all components so we can find the logging # and integrations that need faster initialization. - resolved_domains_task = asyncio.gather(*[ + resolved_domains_task = asyncio.gather(*( loader.async_component_dependencies(hass, domain) for domain in domains - ], return_exceptions=True) + ), return_exceptions=True) # Set up core. _LOGGER.debug("Setting up %s", CORE_INTEGRATIONS) - if not all(await asyncio.gather(*[ + if not all(await asyncio.gather(*( async_setup_component(hass, domain, config) for domain in CORE_INTEGRATIONS - ])): + ))): _LOGGER.error("Home Assistant core failed to initialize. " "Further initialization aborted") return @@ -312,10 +312,10 @@ async def _async_set_up_integrations( if logging_domains: _LOGGER.info("Setting up %s", logging_domains) - await asyncio.gather(*[ + await asyncio.gather(*( async_setup_component(hass, domain, config) for domain in logging_domains - ]) + )) # Kick off loading the registries. They don't need to be awaited. asyncio.gather( @@ -324,18 +324,18 @@ async def _async_set_up_integrations( hass.helpers.area_registry.async_get_registry()) if stage_1_domains: - await asyncio.gather(*[ + await asyncio.gather(*( async_setup_component(hass, domain, config) for domain in stage_1_domains - ]) + )) # Load all integrations after_dependencies = {} # type: Dict[str, Set[str]] - for int_or_exc in await asyncio.gather(*[ + for int_or_exc in await asyncio.gather(*( loader.async_get_integration(hass, domain) for domain in stage_2_domains - ], return_exceptions=True): + ), return_exceptions=True): # Exceptions are handled in async_setup_component. if (isinstance(int_or_exc, loader.Integration) and int_or_exc.after_dependencies): @@ -360,10 +360,10 @@ async def _async_set_up_integrations( _LOGGER.debug("Setting up %s", domains_to_load) - await asyncio.gather(*[ + await asyncio.gather(*( async_setup_component(hass, domain, config) for domain in domains_to_load - ]) + )) last_load = domains_to_load stage_2_domains -= domains_to_load @@ -373,10 +373,10 @@ async def _async_set_up_integrations( if stage_2_domains: _LOGGER.debug("Final set up: %s", stage_2_domains) - await asyncio.gather(*[ + await asyncio.gather(*( async_setup_component(hass, domain, config) for domain in stage_2_domains - ]) + )) # Wrap up startup await hass.async_block_till_done() diff --git a/homeassistant/components/climate/reproduce_state.py b/homeassistant/components/climate/reproduce_state.py index 261dfe93a40..c0f27477e0a 100644 --- a/homeassistant/components/climate/reproduce_state.py +++ b/homeassistant/components/climate/reproduce_state.py @@ -72,6 +72,6 @@ async def async_reproduce_states(hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None) -> None: """Reproduce component states.""" - await asyncio.gather(*[ + await asyncio.gather(*( _async_reproduce_states(hass, state, context) - for state in states]) + for state in states)) diff --git a/homeassistant/components/device_automation/__init__.py b/homeassistant/components/device_automation/__init__.py index 67ad51210df..1d4bc71e3de 100644 --- a/homeassistant/components/device_automation/__init__.py +++ b/homeassistant/components/device_automation/__init__.py @@ -57,10 +57,10 @@ async def async_get_device_automation_triggers(hass, device_id): for entity in entities: domains.add(split_entity_id(entity.entity_id)[0]) - device_triggers = await asyncio.gather(*[ + device_triggers = await asyncio.gather(*( _async_get_device_automation_triggers(hass, domain, device_id) for domain in domains - ]) + )) for device_trigger in device_triggers: if device_trigger is not None: triggers.extend(device_trigger) diff --git a/homeassistant/components/device_tracker/setup.py b/homeassistant/components/device_tracker/setup.py index a74f51c6638..4b4ce6ac1c4 100644 --- a/homeassistant/components/device_tracker/setup.py +++ b/homeassistant/components/device_tracker/setup.py @@ -95,10 +95,10 @@ async def async_extract_config(hass, config): """Extract device tracker config and split between legacy and modern.""" legacy = [] - for platform in await asyncio.gather(*[ + for platform in await asyncio.gather(*( async_create_platform_type(hass, config, p_type, p_config) for p_type, p_config in config_per_platform(config, DOMAIN) - ]): + )): if platform is None: continue diff --git a/homeassistant/components/google_assistant/smart_home.py b/homeassistant/components/google_assistant/smart_home.py index 07548ee95eb..ef8be50fda7 100644 --- a/homeassistant/components/google_assistant/smart_home.py +++ b/homeassistant/components/google_assistant/smart_home.py @@ -81,11 +81,11 @@ async def async_devices_sync(hass, data, payload): {'request_id': data.request_id}, context=data.context) - devices = await asyncio.gather(*[ + devices = await asyncio.gather(*( entity.sync_serialize() for entity in async_get_entities(hass, data.config) if data.config.should_expose(entity.state) - ]) + )) response = { 'agentUserId': data.config.agent_user_id or data.context.user_id, diff --git a/homeassistant/components/media_player/reproduce_state.py b/homeassistant/components/media_player/reproduce_state.py index cbe98704615..c720ed54d88 100644 --- a/homeassistant/components/media_player/reproduce_state.py +++ b/homeassistant/components/media_player/reproduce_state.py @@ -82,6 +82,6 @@ async def async_reproduce_states(hass: HomeAssistantType, states: Iterable[State], context: Optional[Context] = None) -> None: """Reproduce component states.""" - await asyncio.gather(*[ + await asyncio.gather(*( _async_reproduce_states(hass, state, context) - for state in states]) + for state in states)) diff --git a/homeassistant/components/nextbus/sensor.py b/homeassistant/components/nextbus/sensor.py index acf8028e31f..c3d2d964e3b 100644 --- a/homeassistant/components/nextbus/sensor.py +++ b/homeassistant/components/nextbus/sensor.py @@ -245,10 +245,10 @@ class NextBusDepartureSensor(Entity): )) # Chain all predictions together - predictions = list(chain(*[ + predictions = list(chain(*( listify(direction.get('prediction', [])) for direction in directions - ])) + ))) # Short circuit if we don't have any actual bus predictions if not predictions: diff --git a/homeassistant/components/smartthings/__init__.py b/homeassistant/components/smartthings/__init__.py index ef145c9072f..42f91e80dd5 100644 --- a/homeassistant/components/smartthings/__init__.py +++ b/homeassistant/components/smartthings/__init__.py @@ -109,8 +109,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): device.label, device.device_id, exc_info=True) devices.remove(device) - await asyncio.gather(*[retrieve_device_status(d) - for d in devices.copy()]) + await asyncio.gather(*(retrieve_device_status(d) + for d in devices.copy())) # Sync device subscriptions await smartapp_sync_subscriptions( diff --git a/homeassistant/components/system_health/__init__.py b/homeassistant/components/system_health/__init__.py index 7dbb682b287..fa197906ddd 100644 --- a/homeassistant/components/system_health/__init__.py +++ b/homeassistant/components/system_health/__init__.py @@ -65,10 +65,10 @@ async def handle_info(hass: HomeAssistantType, await hass.helpers.system_info.async_get_system_info() if info_callbacks: - for domain, domain_data in zip(info_callbacks, await asyncio.gather(*[ + for domain, domain_data in zip(info_callbacks, await asyncio.gather(*( _info_wrapper(hass, info_callback) for info_callback in info_callbacks.values() - ])): + ))): data[domain] = domain_data connection.send_message(websocket_api.result_message(msg['id'], data)) diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 7eb72a66c8b..a09e552348c 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -189,9 +189,9 @@ async def async_get_all_descriptions(hass): loaded = {} if missing: - contents = await asyncio.gather(*[ + contents = await asyncio.gather(*( _load_services_file(hass, domain) for domain in missing - ]) + )) for domain, content in zip(missing, contents): loaded[domain] = content diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index 8878334ead4..4aa8b2453a1 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -140,10 +140,10 @@ async def async_reproduce_state( if to_call: # run all domains in parallel - await asyncio.gather(*[ + await asyncio.gather(*( worker(domain, data) for domain, data in to_call.items() - ]) + )) @bind_hass diff --git a/homeassistant/loader.py b/homeassistant/loader.py index 653fd60f368..8b1bf588aa4 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -83,14 +83,14 @@ async def _async_get_custom_components( dirs = await hass.async_add_executor_job( get_sub_directories, custom_components.__path__) - integrations = await asyncio.gather(*[ + integrations = await asyncio.gather(*( hass.async_add_executor_job( Integration.resolve_from_root, hass, custom_components, comp.name) for comp in dirs - ]) + )) return { integration.domain: integration