@@ -441,7 +441,7 @@ def execute_fields(
441
441
if is_awaitable (result ):
442
442
append_awaitable (response_name )
443
443
444
- # If there are no coroutines, we can just return the object
444
+ # If there are no coroutines, we can just return the object.
445
445
if not awaitable_fields :
446
446
return results
447
447
@@ -450,12 +450,17 @@ def execute_fields(
450
450
# will yield this same map, but with any coroutines awaited in parallel and
451
451
# replaced with the values they yielded.
452
452
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
+ )
457
463
)
458
- )
459
464
return results
460
465
461
466
return get_results ()
@@ -758,13 +763,18 @@ async def await_completed(item: Any, item_path: Path) -> Any:
758
763
759
764
# noinspection PyShadowingNames
760
765
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
768
778
return completed_results
769
779
770
780
return get_completed_results ()
@@ -907,7 +917,7 @@ def complete_object_value(
907
917
908
918
# If there is an `is_type_of()` predicate function, call it with the current
909
919
# result. If `is_type_of()` returns False, then raise an error rather than
910
- # continuing execution.
920
+ # continuing execution.
911
921
if return_type .is_type_of :
912
922
is_type_of = return_type .is_type_of (result , info )
913
923
@@ -943,7 +953,7 @@ def collect_subfields(
943
953
# We cannot use the field_nodes themselves as key for the cache, since they
944
954
# are not hashable as a list. We also do not want to use the field_nodes
945
955
# 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
947
957
# use the id of the list, since we want to hit the cache for all lists of
948
958
# the same nodes, not only for the same list of nodes. Also, the list id may
949
959
# even be reused, in which case we would get wrong results from the cache.
0 commit comments