Skip to content

Commit 4256831

Browse files
committed
Cheaper calculation for narrowing down possible objects in ENO
1 parent 49d012e commit 4256831

4 files changed

Lines changed: 18222 additions & 0 deletions

File tree

src/main/java/graphql/normalized/ExecutableNormalizedOperationFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ private Set<GraphQLObjectType> narrowDownPossibleObjects(Set<GraphQLObjectType>
453453
if (currentOnes.isEmpty()) {
454454
return resolvedTypeCondition;
455455
}
456+
457+
// Intersection calculation is expensive when either set is large.
458+
// If a set only has one member, it is equivalent and cheaper to calculate via contains.
459+
if (resolvedTypeCondition.size() == 1 && currentOnes.contains(resolvedTypeCondition.iterator().next())) {
460+
return resolvedTypeCondition;
461+
} else if (currentOnes.size() == 1 && resolvedTypeCondition.contains(currentOnes.iterator().next())) {
462+
return currentOnes;
463+
}
464+
456465
return Sets.intersection(currentOnes, resolvedTypeCondition);
457466
}
458467

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package benchmark;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.io.Resources;
5+
import graphql.execution.CoercedVariables;
6+
import graphql.language.Document;
7+
import graphql.normalized.ExecutableNormalizedOperation;
8+
import graphql.normalized.ExecutableNormalizedOperationFactory;
9+
import graphql.parser.Parser;
10+
import graphql.schema.GraphQLSchema;
11+
import graphql.schema.idl.SchemaGenerator;
12+
import org.openjdk.jmh.annotations.Benchmark;
13+
import org.openjdk.jmh.annotations.BenchmarkMode;
14+
import org.openjdk.jmh.annotations.Fork;
15+
import org.openjdk.jmh.annotations.Measurement;
16+
import org.openjdk.jmh.annotations.Mode;
17+
import org.openjdk.jmh.annotations.OutputTimeUnit;
18+
import org.openjdk.jmh.annotations.Scope;
19+
import org.openjdk.jmh.annotations.Setup;
20+
import org.openjdk.jmh.annotations.State;
21+
import org.openjdk.jmh.annotations.Threads;
22+
import org.openjdk.jmh.annotations.Warmup;
23+
import org.openjdk.jmh.infra.Blackhole;
24+
25+
import java.io.IOException;
26+
import java.net.URL;
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static com.google.common.io.Resources.getResource;
30+
31+
@State(Scope.Benchmark)
32+
@BenchmarkMode(Mode.Throughput)
33+
@Warmup(iterations = 2)
34+
@Measurement(iterations = 2, timeUnit = TimeUnit.NANOSECONDS)
35+
public class NQExtraLargeBenchmark {
36+
37+
@State(Scope.Benchmark)
38+
public static class MyState {
39+
40+
GraphQLSchema schema;
41+
Document document;
42+
43+
@Setup
44+
public void setup() {
45+
try {
46+
String schemaString = readFromClasspath("extra-large-schema-1.graphqls");
47+
schema = SchemaGenerator.createdMockedSchema(schemaString);
48+
49+
String query = readFromClasspath("extra-large-schema-1-query.graphql");
50+
document = Parser.parse(query);
51+
} catch (Exception e) {
52+
System.out.println(e);
53+
throw new RuntimeException(e);
54+
}
55+
}
56+
57+
private String readFromClasspath(String file) throws IOException {
58+
URL url = getResource(file);
59+
return Resources.toString(url, Charsets.UTF_8);
60+
}
61+
}
62+
63+
@Benchmark
64+
@Warmup(iterations = 2)
65+
@Measurement(iterations = 3, time = 10)
66+
@Threads(1)
67+
@Fork(3)
68+
@BenchmarkMode(Mode.AverageTime)
69+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
70+
public void benchMarkAvgTime(MyState myState, Blackhole blackhole ) {
71+
runImpl(myState, blackhole);
72+
}
73+
74+
@Benchmark
75+
@Warmup(iterations = 2)
76+
@Measurement(iterations = 3, time = 10)
77+
@Threads(1)
78+
@Fork(3)
79+
@BenchmarkMode(Mode.Throughput)
80+
@OutputTimeUnit(TimeUnit.SECONDS)
81+
public void benchMarkThroughput(MyState myState, Blackhole blackhole ) {
82+
runImpl(myState, blackhole);
83+
}
84+
85+
private void runImpl(MyState myState, Blackhole blackhole) {
86+
ExecutableNormalizedOperation executableNormalizedOperation = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation(myState.schema, myState.document, null, CoercedVariables.emptyVariables());
87+
blackhole.consume(executableNormalizedOperation);
88+
}
89+
}

0 commit comments

Comments
 (0)