@@ -441,7 +441,7 @@ def execute_fields(
441441 if is_awaitable (result ):
442442 append_awaitable (response_name )
443443
444- # If there are no coroutines, we can just return the object
444+ # If there are no coroutines, we can just return the object.
445445 if not awaitable_fields :
446446 return results
447447
@@ -450,12 +450,17 @@ def execute_fields(
450450 # will yield this same map, but with any coroutines awaited in parallel and
451451 # replaced with the values they yielded.
452452 async def get_results () -> Dict [str , Any ]:
453- results .update (
454- zip (
455- awaitable_fields ,
456- await gather (* (results [field ] for field in awaitable_fields )),
453+ if len (awaitable_fields ) == 1 :
454+ # If there is only one field, avoid the overhead of parallelization.
455+ field = awaitable_fields [0 ]
456+ results [field ] = await results [field ]
457+ else :
458+ results .update (
459+ zip (
460+ awaitable_fields ,
461+ await gather (* (results [field ] for field in awaitable_fields )),
462+ )
457463 )
458- )
459464 return results
460465
461466 return get_results ()
@@ -758,13 +763,18 @@ async def await_completed(item: Any, item_path: Path) -> Any:
758763
759764 # noinspection PyShadowingNames
760765 async def get_completed_results () -> List [Any ]:
761- for index , result in zip (
762- awaitable_indices ,
763- await gather (
764- * (completed_results [index ] for index in awaitable_indices )
765- ),
766- ):
767- completed_results [index ] = result
766+ if len (awaitable_indices ) == 1 :
767+ # If there is only one index, avoid the overhead of parallelization.
768+ index = awaitable_indices [0 ]
769+ completed_results [index ] = await completed_results [index ]
770+ else :
771+ for index , result in zip (
772+ awaitable_indices ,
773+ await gather (
774+ * (completed_results [index ] for index in awaitable_indices )
775+ ),
776+ ):
777+ completed_results [index ] = result
768778 return completed_results
769779
770780 return get_completed_results ()
@@ -907,7 +917,7 @@ def complete_object_value(
907917
908918 # If there is an `is_type_of()` predicate function, call it with the current
909919 # result. If `is_type_of()` returns False, then raise an error rather than
910- # continuing execution.
920+ # continuing execution.
911921 if return_type .is_type_of :
912922 is_type_of = return_type .is_type_of (result , info )
913923
@@ -943,7 +953,7 @@ def collect_subfields(
943953 # We cannot use the field_nodes themselves as key for the cache, since they
944954 # are not hashable as a list. We also do not want to use the field_nodes
945955 # themselves (converted to a tuple) as keys, since hashing them is slow.
946- # Therefore we use the ids of the field_nodes as keys. Note that we do not
956+ # Therefore, we use the ids of the field_nodes as keys. Note that we do not
947957 # use the id of the list, since we want to hit the cache for all lists of
948958 # the same nodes, not only for the same list of nodes. Also, the list id may
949959 # even be reused, in which case we would get wrong results from the cache.
0 commit comments