aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Taras Tsugrii <ttsugrii@fb.com>2018-05-24 13:47:21 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-24 13:49:06 -0700
commitd8e7f9c7531e44dfcd1f23a9cedea50ecac4d0f8 (patch)
treef653279fd91e5b013b9e03d204e7f7065946af0e /src/main/java/com
parent33f08e75fa4e7480f8d46b8305ce03a171adefa0 (diff)
[Skylark] Size newly created arrays to avoid unnecessary re-allocations.
It's more efficient to use properly sized array builders to avoid having a need to dynamically adjust the size of the underlying array. The result is improved performance (no need to copy elements to newly created array) and better memory efficiency - no need to re-allocate and no extra space is wasted. Closes #5241. PiperOrigin-RevId: 197946723
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
index 670c4d2c21..6be38d5f8f 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
@@ -667,10 +667,11 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
public static <T> Tuple<T> concat(Tuple<? extends T> left, Tuple<? extends T> right) {
// Build the ImmutableList directly rather than use Iterables.concat, to avoid unnecessary
// array resizing.
- return create(ImmutableList.<T>builder()
- .addAll(left)
- .addAll(right)
- .build());
+ return create(
+ ImmutableList.<T>builderWithExpectedSize(left.size() + right.size())
+ .addAll(left)
+ .addAll(right)
+ .build());
}
@Override
@@ -678,7 +679,7 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
Object start, Object end, Object step, Location loc, Mutability mutability)
throws EvalException {
List<Integer> sliceIndices = EvalUtils.getSliceIndices(start, end, step, this.size(), loc);
- ImmutableList.Builder<E> builder = ImmutableList.builder();
+ ImmutableList.Builder<E> builder = ImmutableList.builderWithExpectedSize(sliceIndices.size());
for (int pos : sliceIndices) {
builder.add(this.get(pos));
}
@@ -687,7 +688,11 @@ public abstract class SkylarkList<E> extends BaseMutableList<E>
@Override
public Tuple<E> repeat(int times, Mutability mutability) {
- ImmutableList.Builder<E> builder = ImmutableList.builder();
+ if (times <= 0) {
+ return empty();
+ }
+
+ ImmutableList.Builder<E> builder = ImmutableList.builderWithExpectedSize(this.size() * times);
for (int i = 0; i < times; i++) {
builder.addAll(this);
}