Skip to content

Commit 66d527c

Browse files
committed
Removing some fo the Optional.map() and .stream() for performance reasons.
1 parent 5fc58fb commit 66d527c

14 files changed

Lines changed: 114 additions & 83 deletions

src/main/java/graphql/GraphqlErrorHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ public static Object location(SourceLocation location) {
7777
}
7878

7979
static List<GraphQLError> fromSpecification(List<Map<String, Object>> specificationMaps) {
80-
return specificationMaps.stream()
81-
.map(GraphqlErrorHelper::fromSpecification).collect(Collectors.toList());
80+
List<GraphQLError> list = new ArrayList<>();
81+
for (Map<String, Object> specificationMap : specificationMaps) {
82+
list.add(fromSpecification(specificationMap));
83+
}
84+
return list;
8285
}
8386

8487
static GraphQLError fromSpecification(Map<String, Object> specificationMap) {

src/main/java/graphql/execution/Execution.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
180180
MergedSelectionSet fields = fieldCollector.collectFields(
181181
collectorParameters,
182182
operationDefinition.getSelectionSet(),
183-
Optional.ofNullable(executionContext.getGraphQLContext())
184-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
185-
.orElse(false)
183+
executionContext.hasIncrementalSupport()
186184
);
187185

188186
ResultPath path = ResultPath.rootPath();
@@ -255,9 +253,7 @@ private DataLoaderDispatchStrategy createDataLoaderDispatchStrategy(ExecutionCon
255253
return DataLoaderDispatchStrategy.NO_OP;
256254
}
257255
if (!executionContext.isSubscriptionOperation()) {
258-
boolean deferEnabled = Optional.ofNullable(executionContext.getGraphQLContext())
259-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
260-
.orElse(false);
256+
boolean deferEnabled = executionContext.hasIncrementalSupport();
261257

262258
// Dedicated strategy for defer support, for safety purposes.
263259
return deferEnabled ?

src/main/java/graphql/execution/ExecutionContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,14 @@ public ResultNodesInfo getResultNodesInfo() {
360360
return resultNodesInfo;
361361
}
362362

363+
@Internal
364+
public boolean hasIncrementalSupport() {
365+
GraphQLContext graphqlContext = getGraphQLContext();
366+
return graphqlContext != null && graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT);
367+
}
368+
363369
@Internal
364370
public EngineRunningState getEngineRunningState() {
365371
return engineRunningState;
366372
}
367-
368373
}

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,7 @@ private static Map<String, Object> buildFieldValueMap(List<String> fieldNames, L
300300
DeferredExecutionSupport createDeferredExecutionSupport(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
301301
MergedSelectionSet fields = parameters.getFields();
302302

303-
return Optional.ofNullable(executionContext.getGraphQLContext())
304-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
305-
.orElse(false) ?
303+
return executionContext.hasIncrementalSupport() ?
306304
new DeferredExecutionSupport.DeferredExecutionSupportImpl(
307305
fields,
308306
parameters,
@@ -928,9 +926,7 @@ protected Object completeValueForObject(ExecutionContext executionContext, Execu
928926
MergedSelectionSet subFields = fieldCollector.collectFields(
929927
collectorParameters,
930928
parameters.getField(),
931-
Optional.ofNullable(executionContext.getGraphQLContext())
932-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
933-
.orElse(false)
929+
executionContext.hasIncrementalSupport()
934930
);
935931

936932
ExecutionStepInfo newExecutionStepInfo = executionStepInfo.changeTypeWithPreservedNonNull(resolvedObjectType);

src/main/java/graphql/execution/ValuesResolverConversion.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,17 @@ private static List externalValueToInternalValueForList(
602602
) throws CoercingParseValueException, NonNullableValueCoercedAsNullException {
603603

604604
GraphQLInputType wrappedType = (GraphQLInputType) graphQLList.getWrappedType();
605-
return FpKit.toListOrSingletonList(value)
606-
.stream()
607-
.map(val -> externalValueToInternalValueImpl(
608-
inputInterceptor,
609-
fieldVisibility,
610-
wrappedType,
611-
val,
612-
graphqlContext,
613-
locale))
614-
.collect(toList());
605+
List<Object> list = new ArrayList<>();
606+
for (Object val : FpKit.toListOrSingletonList(value)) {
607+
list.add(externalValueToInternalValueImpl(
608+
inputInterceptor,
609+
fieldVisibility,
610+
wrappedType,
611+
val,
612+
graphqlContext,
613+
locale));
614+
}
615+
return list;
615616
}
616617

617618
/**

src/main/java/graphql/execution/ValuesResolverLegacy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ private static Value<?> handleNumberLegacy(String stringValue) {
133133
private static Value<?> handleListLegacy(Object value, GraphQLList type, GraphQLContext graphqlContext, Locale locale) {
134134
GraphQLType itemType = type.getWrappedType();
135135
if (FpKit.isIterable(value)) {
136-
List<Value> valuesNodes = FpKit.toListOrSingletonList(value)
137-
.stream()
138-
.map(item -> valueToLiteralLegacy(item, itemType, graphqlContext, locale))
139-
.collect(toList());
136+
List<Value> valuesNodes = new ArrayList<>();
137+
for (Object item : FpKit.toListOrSingletonList(value)) {
138+
valuesNodes.add(valueToLiteralLegacy(item, itemType, graphqlContext, locale));
139+
}
140140
return ArrayValue.newArrayValue().values(valuesNodes).build();
141141
}
142142
return valueToLiteralLegacy(value, itemType, graphqlContext, locale);

src/main/java/graphql/execution/incremental/DeferredExecutionSupport.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
import graphql.incremental.IncrementalPayload;
1616
import graphql.util.FpKit;
1717

18+
import java.util.ArrayList;
1819
import java.util.Collections;
1920
import java.util.HashMap;
21+
import java.util.HashSet;
2022
import java.util.LinkedHashMap;
2123
import java.util.List;
2224
import java.util.Map;
2325
import java.util.Set;
2426
import java.util.concurrent.CompletableFuture;
2527
import java.util.function.BiFunction;
2628
import java.util.function.Supplier;
27-
import java.util.stream.Collectors;
2829

2930
/**
3031
* The purpose of this class hierarchy is to encapsulate most of the logic for deferring field execution, thus
@@ -106,19 +107,22 @@ public List<String> getNonDeferredFieldNames(List<String> allFieldNames) {
106107

107108
@Override
108109
public Set<IncrementalCall<? extends IncrementalPayload>> createCalls(ExecutionStrategyParameters executionStrategyParameters) {
109-
return deferredExecutionToFields.keySet().stream()
110-
.map(deferredExecution -> this.createDeferredFragmentCall(deferredExecution, executionStrategyParameters))
111-
.collect(Collectors.toSet());
110+
Set<IncrementalCall<? extends IncrementalPayload>> set = new HashSet<>();
111+
for (DeferredExecution deferredExecution : deferredExecutionToFields.keySet()) {
112+
set.add(this.createDeferredFragmentCall(deferredExecution, executionStrategyParameters));
113+
}
114+
return set;
112115
}
113116

114117
private DeferredFragmentCall createDeferredFragmentCall(DeferredExecution deferredExecution, ExecutionStrategyParameters executionStrategyParameters) {
115118
DeferredCallContext deferredCallContext = new DeferredCallContext();
116119

117120
List<MergedField> mergedFields = deferredExecutionToFields.get(deferredExecution);
118121

119-
List<Supplier<CompletableFuture<DeferredFragmentCall.FieldWithExecutionResult>>> calls = mergedFields.stream()
120-
.map(currentField -> this.createResultSupplier(currentField, deferredCallContext, executionStrategyParameters))
121-
.collect(Collectors.toList());
122+
List<Supplier<CompletableFuture<DeferredFragmentCall.FieldWithExecutionResult>>> calls = new ArrayList<>();
123+
for (MergedField currentField : mergedFields) {
124+
calls.add(this.createResultSupplier(currentField, deferredCallContext, executionStrategyParameters));
125+
}
122126

123127
return new DeferredFragmentCall(
124128
deferredExecution.getLabel(),

src/main/java/graphql/execution/instrumentation/dataloader/PerLevelDataLoaderDispatchStrategyWithDeferAlwaysDispatch.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import graphql.execution.ExecutionContext;
77
import graphql.execution.ExecutionStrategyParameters;
88
import graphql.execution.FieldValueInfo;
9+
import graphql.execution.MergedField;
910
import graphql.schema.DataFetcher;
1011
import graphql.util.LockKit;
1112
import org.dataloader.DataLoaderRegistry;
@@ -195,10 +196,13 @@ public void fieldFetched(ExecutionContext executionContext,
195196
}
196197

197198
private void increaseCallCounts(int curLevel, ExecutionStrategyParameters parameters) {
198-
int nonDeferredFieldCount = (int) parameters.getFields().getSubFieldsList().stream()
199-
.filter(field -> !field.isDeferred())
200-
.count();
201-
199+
int count = 0;
200+
for (MergedField field : parameters.getFields().getSubFieldsList()) {
201+
if (!field.isDeferred()) {
202+
count++;
203+
}
204+
}
205+
int nonDeferredFieldCount = count;
202206
callStack.lock.runLocked(() -> {
203207
callStack.increaseExpectedFetchCount(curLevel, nonDeferredFieldCount);
204208
callStack.increaseHappenedStrategyCalls(curLevel);

src/main/java/graphql/incremental/DelayedIncrementalPartialResultImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import graphql.ExperimentalApi;
44

5+
import java.util.ArrayList;
56
import java.util.Collections;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
9-
import java.util.stream.Collectors;
1010

1111
@ExperimentalApi
1212
public class DelayedIncrementalPartialResultImpl implements DelayedIncrementalPartialResult {
@@ -44,10 +44,12 @@ public Map<String, Object> toSpecification() {
4444
result.put("extensions", extensions);
4545
}
4646

47-
if(incrementalItems != null) {
48-
result.put("incremental", incrementalItems.stream()
49-
.map(IncrementalPayload::toSpecification)
50-
.collect(Collectors.toList()));
47+
if (incrementalItems != null) {
48+
List<Map<String, Object>> list = new ArrayList<>();
49+
for (IncrementalPayload incrementalItem : incrementalItems) {
50+
list.add(incrementalItem.toSpecification());
51+
}
52+
result.put("incremental", list);
5153
}
5254

5355
return result;

src/main/java/graphql/incremental/IncrementalExecutionResultImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.List;
1212
import java.util.Map;
1313
import java.util.function.Consumer;
14-
import java.util.stream.Collectors;
1514

1615
@ExperimentalApi
1716
public class IncrementalExecutionResultImpl extends ExecutionResultImpl implements IncrementalExecutionResult {
@@ -66,11 +65,11 @@ public Map<String, Object> toSpecification() {
6665
map.put("hasNext", hasNext);
6766

6867
if (this.incremental != null) {
69-
map.put("incremental",
70-
this.incremental.stream()
71-
.map(IncrementalPayload::toSpecification)
72-
.collect(Collectors.toCollection(LinkedList::new))
73-
);
68+
LinkedList<Map<String, Object>> linkedList = new LinkedList<>();
69+
for (IncrementalPayload incrementalPayload : this.incremental) {
70+
linkedList.add(incrementalPayload.toSpecification());
71+
}
72+
map.put("incremental", linkedList);
7473
}
7574

7675
return map;

0 commit comments

Comments
 (0)