Skip to content

Commit 8983903

Browse files
committed
Optimize GraphqlTypeComparators
- Return singleton Comparator from byNameAsc - Use ImmutableList.sortedCopyOf to avoid extra List allocation in sortTypes
1 parent c83bf73 commit 8983903

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/main/java/graphql/schema/GraphqlTypeComparators.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import com.google.common.collect.ImmutableList;
44
import graphql.Internal;
55

6-
import java.util.ArrayList;
76
import java.util.Collection;
87
import java.util.Comparator;
98
import java.util.List;
109

1110
@Internal
1211
public class GraphqlTypeComparators {
1312

13+
private static final Comparator<? super GraphQLSchemaElement> BY_NAME_ASCENDING =
14+
Comparator.comparing(graphQLSchemaElement ->
15+
((GraphQLNamedSchemaElement) graphQLSchemaElement).getName());
16+
1417
/**
1518
* This sorts the list of {@link graphql.schema.GraphQLType} objects (by name) and allocates a new sorted
1619
* list back.
@@ -22,9 +25,7 @@ public class GraphqlTypeComparators {
2225
* @return a new allocated list of sorted things
2326
*/
2427
public static <T extends GraphQLSchemaElement> List<T> sortTypes(Comparator<? super GraphQLSchemaElement> comparator, Collection<T> types) {
25-
List<T> sorted = new ArrayList<>(types);
26-
sorted.sort(comparator);
27-
return ImmutableList.copyOf(sorted);
28+
return ImmutableList.sortedCopyOf(comparator, types);
2829
}
2930

3031
/**
@@ -42,7 +43,7 @@ public static Comparator<? super GraphQLSchemaElement> asIsOrder() {
4243
* @return a comparator that compares {@link graphql.schema.GraphQLType} objects by ascending name
4344
*/
4445
public static Comparator<? super GraphQLSchemaElement> byNameAsc() {
45-
return Comparator.comparing(graphQLSchemaElement -> ((GraphQLNamedSchemaElement) graphQLSchemaElement).getName());
46+
return BY_NAME_ASCENDING;
4647
}
4748

4849
}

0 commit comments

Comments
 (0)