aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib
diff options
context:
space:
mode:
authorGravatar Michajlo Matijkiw <michajlo@google.com>2016-08-10 22:22:06 +0000
committerGravatar Yue Gan <yueg@google.com>2016-08-11 09:17:05 +0000
commit9e71a5b2b90c14ca7b097930e8dff3fafad86407 (patch)
tree0465d9c21ced96167388ac5271896510b43eaa85 /src/main/java/com/google/devtools/build/lib
parenta821b72ea51f8190f663559efa7d7037214767d7 (diff)
Minimize ArrayList resizing in MutableList construction
Specialize the creation of internal contents if we have a collection and can properly presize the ArrayList (this is almost always the case), and provide a special case constructor for concat. -- MOS_MIGRATED_REVID=129919134
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java18
1 files changed, 14 insertions, 4 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 51be3e9b2d..7098a1d790 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
@@ -20,14 +20,12 @@ import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.syntax.SkylarkMutable.MutableCollection;
-
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
-
import javax.annotation.Nullable;
/** A class to handle lists and tuples in Skylark. */
@@ -231,7 +229,7 @@ public abstract class SkylarkList<E> extends MutableCollection<E> implements Lis
* @return a MutableList containing the elements
*/
@SuppressWarnings("unchecked")
- MutableList(Iterable<? extends E> contents, Mutability mutability) {
+ private MutableList(Iterable<? extends E> contents, Mutability mutability) {
super();
addAllUnsafe(contents);
if (contents instanceof GlobList) {
@@ -240,6 +238,18 @@ public abstract class SkylarkList<E> extends MutableCollection<E> implements Lis
this.mutability = mutability;
}
+ /** Specialized constructor for concat. */
+ private MutableList(
+ MutableList<? extends E> lhs,
+ MutableList<? extends E> rhs,
+ @Nullable Environment env) {
+ super();
+ this.contents.ensureCapacity(lhs.size() + rhs.size());
+ this.contents.addAll(lhs);
+ this.contents.addAll(rhs);
+ this.mutability = env == null ? Mutability.IMMUTABLE : env.mutability();
+ }
+
/**
* Creates a MutableList from contents and an Environment.
* @param contents the contents of the list
@@ -334,7 +344,7 @@ public abstract class SkylarkList<E> extends MutableCollection<E> implements Lis
MutableList<? extends E> right,
Environment env) {
if (left.getGlobList() == null && right.getGlobList() == null) {
- return new MutableList(Iterables.concat(left, right), env);
+ return new MutableList<>(left, right, env);
}
return new MutableList(GlobList.concat(
left.getGlobListOrContentsUnsafe(), right.getGlobListOrContentsUnsafe()), env);