Avoid boxing in SimplePageWithPositionComparator
@@ -19,48 +19,54 @@ import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.block.SortOrder;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.spi.PrestoException;
-import com.google.common.collect.ImmutableList;
+import com.google.common.primitives.Ints;
import java.util.List;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
public class SimplePageWithPositionComparator
implements PageWithPositionComparator
{
- private final List<Integer> sortChannels;
- private final List<SortOrder> sortOrders;
- private final List<Type> types;
+ private final int[] sortChannels;
+ private final SortOrder[] sortOrders;
+ private final Type[] types;
public SimplePageWithPositionComparator(List<Type> types, List<Integer> sortChannels, List<SortOrder> sortOrders)
{
- this.types = ImmutableList.copyOf(requireNonNull(types, "types is null"));
- this.sortChannels = ImmutableList.copyOf(requireNonNull(sortChannels, "sortChannels is null"));
- this.sortOrders = ImmutableList.copyOf(requireNonNull(sortOrders, "sortOrders is null"));
+ requireNonNull(types, "types is null");
+ this.sortChannels = Ints.toArray(requireNonNull(sortChannels, "sortChannels is null"));
+ this.sortOrders = requireNonNull(sortOrders, "sortOrders is null").toArray(new SortOrder[0]);
+ checkArgument(this.sortOrders.length == this.sortChannels.length, "sortChannels and sortOrders length mismatch");
+ this.types = new Type[this.sortChannels.length];
+ for (int i = 0; i < this.sortChannels.length; i++) {
+ int channel = this.sortChannels[i];
+ this.types[i] = types.get(channel);
+ checkArgument(this.types[i] != null, "type %s in types is null", channel);
+ }
}
@Override
public int compareTo(Page left, int leftPosition, Page right, int rightPosition)
{
- for (int i = 0; i < sortChannels.size(); i++) {
- int sortChannel = sortChannels.get(i);
+ for (int i = 0; i < sortChannels.length; i++) {
+ int sortChannel = sortChannels[i];
+ SortOrder sortOrder = sortOrders[i];
+ Type type = types[i];
Block leftBlock = left.getBlock(sortChannel);
Block rightBlock = right.getBlock(sortChannel);
- SortOrder sortOrder = sortOrders.get(i);
-
- int compare;
try {
- compare = sortOrder.compareBlockValue(types.get(sortChannel), leftBlock, leftPosition, rightBlock, rightPosition);
+ int compare = sortOrder.compareBlockValue(type, leftBlock, leftPosition, rightBlock, rightPosition);
+ if (compare != 0) {
+ return compare;
+ }
}
catch (NotSupportedException e) {
throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
}
-
- if (compare != 0) {
- return compare;
- }
}
return 0;
}
