Skip to content

Commit

Permalink
Convert from String to Supplier<Type> only once per string
Browse files Browse the repository at this point in the history
Iterables.transform is fairly expensive if you're going to iterate over the
result more than once, because it performs the transformation each time.

The speedup from this is pretty insignificant, but it's not nothing.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=259012455
  • Loading branch information
amalloy authored and nick-someone committed Jul 24, 2019
1 parent 9f0ec32 commit 73c063a
Showing 1 changed file with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.errorprone.predicates;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import static com.google.errorprone.suppliers.Suppliers.fromStrings;

import com.google.errorprone.predicates.type.Any;
import com.google.errorprone.predicates.type.Array;
import com.google.errorprone.predicates.type.DescendantOf;
Expand Down Expand Up @@ -51,17 +51,9 @@ public static TypePredicate isExactType(Supplier<Type> type) {
return new Exact(type);
}

private static final Function<String, Supplier<Type>> GET_TYPE =
new Function<String, Supplier<Type>>() {
@Override
public Supplier<Type> apply(String input) {
return Suppliers.typeFromString(input);
}
};

/** Match types that are exactly equal to any of the given types. */
public static TypePredicate isExactTypeAny(Iterable<String> types) {
return new ExactAny(Iterables.transform(types, GET_TYPE));
return new ExactAny(fromStrings(types));
}

/** Match sub-types of the given type. */
Expand All @@ -71,7 +63,7 @@ public static TypePredicate isDescendantOf(Supplier<Type> type) {

/** Match types that are a sub-type of one of the given types. */
public static TypePredicate isDescendantOfAny(Iterable<String> types) {
return new DescendantOfAny(Iterables.transform(types, GET_TYPE));
return new DescendantOfAny(fromStrings(types));
}

/** Match sub-types of the given type. */
Expand Down

0 comments on commit 73c063a

Please sign in to comment.