[Skylark] Size tuple slice sizes to avoid unnecessary allocations.
Closes
@@ -512,7 +512,10 @@ public final class EvalUtils {
}
start = clampRangeEndpoint(start, length, step < 0);
end = clampRangeEndpoint(end, length, step < 0);
- ImmutableList.Builder<Integer> indices = ImmutableList.builder();
+ // precise computation is slightly more involved, but since it can overshoot only by a single
+ // element it's fine
+ final int expectedMaxSize = Math.abs(start - end) / Math.abs(step) + 1;
+ ImmutableList.Builder<Integer> indices = ImmutableList.builderWithExpectedSize(expectedMaxSize);
for (int current = start; step > 0 ? current < end : current > end; current += step) {
indices.add(current);
}
