Skip to content

Commit 9803c10

Browse files
authored
avoiding allocations in ConditionalNodes (graphql-java#2123)
* avoiding allocations in ConditionalNodes * share code in NodeUtil Rewriting findDirectiveByName and getArgumentByName in a more generic way.
1 parent 8c6b1a7 commit 9803c10

3 files changed

Lines changed: 14 additions & 23 deletions

File tree

‎src/main/java/graphql/execution/ConditionalNodes.java‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111

1212
import static graphql.Directives.IncludeDirective;
1313
import static graphql.Directives.SkipDirective;
14-
import static graphql.collect.ImmutableKit.emptyList;
15-
1614

1715
@Internal
1816
public class ConditionalNodes {
1917

20-
2118
@VisibleForTesting
2219
ValuesResolver valuesResolver = new ValuesResolver();
2320

@@ -28,19 +25,14 @@ public boolean shouldInclude(Map<String, Object> variables, List<Directive> dire
2825
}
2926

3027
private boolean getDirectiveResult(Map<String, Object> variables, List<Directive> directives, String directiveName, boolean defaultValue) {
31-
List<Directive> foundDirectives = getDirectiveByName(directives, directiveName);
32-
if (!foundDirectives.isEmpty()) {
33-
Directive directive = foundDirectives.get(0);
34-
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(SkipDirective.getArguments(), directive.getArguments(), variables);
28+
Directive foundDirective = NodeUtil.findNodeByName(directives, directiveName);
29+
if (foundDirective != null) {
30+
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(SkipDirective.getArguments(), foundDirective.getArguments(), variables);
3531
Object flag = argumentValues.get("if");
3632
Assert.assertTrue(flag instanceof Boolean, () -> String.format("The '%s' directive MUST have a value for the 'if' argument", directiveName));
3733
return (Boolean) flag;
3834
}
3935
return defaultValue;
4036
}
4137

42-
private List<Directive> getDirectiveByName(List<Directive> directives, String name) {
43-
return NodeUtil.allDirectivesByName(directives).getOrDefault(name, emptyList());
44-
}
45-
4638
}

‎src/main/java/graphql/language/Directive.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static graphql.collect.ImmutableKit.emptyMap;
2020
import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer;
2121
import static graphql.language.NodeUtil.argumentsByName;
22-
import static graphql.language.NodeUtil.getArgumentByName;
2322

2423
@PublicApi
2524
public class Directive extends AbstractNode<Directive> implements NamedNode<Directive> {
@@ -65,7 +64,7 @@ public Map<String, Argument> getArgumentsByName() {
6564
}
6665

6766
public Argument getArgument(String argumentName) {
68-
return NodeUtil.getArgumentByName(arguments, argumentName);
67+
return NodeUtil.findNodeByName(arguments, argumentName);
6968
}
7069

7170
@Override

‎src/main/java/graphql/language/NodeUtil.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.LinkedHashMap;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.Objects;
1213

1314
import static graphql.util.FpKit.mergeFirst;
1415

@@ -29,6 +30,15 @@ public static boolean isEqualTo(String thisStr, String thatStr) {
2930
return true;
3031
}
3132

33+
public static <T extends NamedNode<T>> T findNodeByName(List<T> namedNodes, String name) {
34+
for (T namedNode : namedNodes) {
35+
if (Objects.equals(namedNode.getName(), name)) {
36+
return namedNode;
37+
}
38+
}
39+
return null;
40+
}
41+
3242
public static Map<String, ImmutableList<Directive>> allDirectivesByName(List<Directive> directives) {
3343
return FpKit.groupingBy(directives, Directive::getName);
3444
}
@@ -37,16 +47,6 @@ public static Map<String, Argument> argumentsByName(List<Argument> arguments) {
3747
return FpKit.getByName(arguments, Argument::getName, mergeFirst());
3848
}
3949

40-
public static Argument getArgumentByName(List<Argument> arguments, String name) {
41-
for (Argument argument : arguments) {
42-
if (argument.getName().equals(name)) {
43-
return argument;
44-
}
45-
}
46-
return null;
47-
}
48-
49-
5050
public static class GetOperationResult {
5151
public OperationDefinition operationDefinition;
5252
public Map<String, FragmentDefinition> fragmentsByName;

0 commit comments

Comments
 (0)