Small speed up to _collection_changed (#90621)

attrgetter builds a fast method which happens in native code
4664a7cf68/Modules/_operator.c (L1413)
This commit is contained in:
J. Nick Koston 2023-03-31 15:15:36 -10:00 committed by GitHub
parent 3e59687902
commit 3e94f2a502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ from collections.abc import Awaitable, Callable, Coroutine, Iterable
from dataclasses import dataclass from dataclasses import dataclass
from itertools import groupby from itertools import groupby
import logging import logging
from operator import attrgetter
from typing import Any, cast from typing import Any, cast
import voluptuous as vol import voluptuous as vol
@ -410,9 +411,8 @@ def sync_entity_lifecycle(
# Create a new bucket every time we have a different change type # Create a new bucket every time we have a different change type
# to ensure operations happen in order. We only group # to ensure operations happen in order. We only group
# the same change type. # the same change type.
for _, grouped in groupby( groupby_key = attrgetter("change_type")
change_sets, lambda change_set: change_set.change_type for _, grouped in groupby(change_sets, groupby_key):
):
new_entities = [ new_entities = [
entity entity
for entity in await asyncio.gather( for entity in await asyncio.gather(