Skip to content

Commit 0ad655c

Browse files
authored
Merge pull request #3930 from graphql-java/remove-optional-streams-style-code
Removing some of the Optional.map() and .stream() for performance reasons
2 parents b376b80 + 0d8ab56 commit 0ad655c

15 files changed

+119
-70
lines changed

src/main/java/graphql/GraphqlErrorHelper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package graphql;
22

33
import graphql.language.SourceLocation;
4+
import graphql.util.FpKit;
45

56
import java.util.ArrayList;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
910
import java.util.Objects;
10-
import java.util.stream.Collectors;
1111

1212
import static graphql.collect.ImmutableKit.mapAndDropNulls;
1313

@@ -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 = FpKit.arrayListSizedTo(specificationMaps);
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
@@ -184,9 +184,7 @@ private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext exe
184184
MergedSelectionSet fields = fieldCollector.collectFields(
185185
collectorParameters,
186186
operationDefinition.getSelectionSet(),
187-
Optional.ofNullable(executionContext.getGraphQLContext())
188-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
189-
.orElse(false)
187+
executionContext.hasIncrementalSupport()
190188
);
191189

192190
ResultPath path = ResultPath.rootPath();
@@ -259,9 +257,7 @@ private DataLoaderDispatchStrategy createDataLoaderDispatchStrategy(ExecutionCon
259257
return DataLoaderDispatchStrategy.NO_OP;
260258
}
261259
if (!executionContext.isSubscriptionOperation()) {
262-
boolean deferEnabled = Optional.ofNullable(executionContext.getGraphQLContext())
263-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
264-
.orElse(false);
260+
boolean deferEnabled = executionContext.hasIncrementalSupport();
265261

266262
// Dedicated strategy for defer support, for safety purposes.
267263
return deferEnabled ?

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,14 @@ public ResultNodesInfo getResultNodesInfo() {
366366
return resultNodesInfo;
367367
}
368368

369+
@Internal
370+
public boolean hasIncrementalSupport() {
371+
GraphQLContext graphqlContext = getGraphQLContext();
372+
return graphqlContext != null && graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT);
373+
}
374+
369375
@Internal
370376
public EngineRunningState getEngineRunningState() {
371377
return engineRunningState;
372378
}
373-
374379
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,7 @@ private BiConsumer<List<Object>, Throwable> buildFieldValueMap(List<String> fiel
283283
DeferredExecutionSupport createDeferredExecutionSupport(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
284284
MergedSelectionSet fields = parameters.getFields();
285285

286-
return Optional.ofNullable(executionContext.getGraphQLContext())
287-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
288-
.orElse(false) ?
286+
return executionContext.hasIncrementalSupport() ?
289287
new DeferredExecutionSupport.DeferredExecutionSupportImpl(
290288
fields,
291289
parameters,
@@ -908,9 +906,7 @@ protected Object completeValueForObject(ExecutionContext executionContext, Execu
908906
MergedSelectionSet subFields = fieldCollector.collectFields(
909907
collectorParameters,
910908
parameters.getField(),
911-
Optional.ofNullable(executionContext.getGraphQLContext())
912-
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
913-
.orElse(false)
909+
executionContext.hasIncrementalSupport()
914910
);
915911

916912
ExecutionStepInfo newExecutionStepInfo = executionStepInfo.changeTypeWithPreservedNonNull(resolvedObjectType);

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,18 @@ 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> listOrSingletonList = FpKit.toListOrSingletonList(value);
606+
List<Object> list = FpKit.arrayListSizedTo(listOrSingletonList);
607+
for (Object val : listOrSingletonList) {
608+
list.add(externalValueToInternalValueImpl(
609+
inputInterceptor,
610+
fieldVisibility,
611+
wrappedType,
612+
val,
613+
graphqlContext,
614+
locale));
615+
}
616+
return list;
615617
}
616618

617619
/**

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ 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<Object> listOrSingletonList = FpKit.toListOrSingletonList(value);
137+
List<Value> valuesNodes = FpKit.arrayListSizedTo(listOrSingletonList);
138+
for (Object item : listOrSingletonList) {
139+
valuesNodes.add(valueToLiteralLegacy(item, itemType, graphqlContext, locale));
140+
}
140141
return ArrayValue.newArrayValue().values(valuesNodes).build();
141142
}
142143
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
@@ -18,14 +18,14 @@
1818

1919
import java.util.Collections;
2020
import java.util.HashMap;
21+
import java.util.HashSet;
2122
import java.util.LinkedHashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.Set;
2526
import java.util.concurrent.CompletableFuture;
2627
import java.util.function.BiFunction;
2728
import java.util.function.Supplier;
28-
import java.util.stream.Collectors;
2929

3030
/**
3131
* The purpose of this class hierarchy is to encapsulate most of the logic for deferring field execution, thus
@@ -107,19 +107,23 @@ public List<String> getNonDeferredFieldNames(List<String> allFieldNames) {
107107

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

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

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

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

124128
return new DeferredFragmentCall(
125129
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
@@ -1,12 +1,12 @@
11
package graphql.incremental;
22

33
import graphql.ExperimentalApi;
4+
import graphql.util.FpKit;
45

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 = FpKit.arrayListSizedTo(incrementalItems);
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)