diff options
author | michajlo <michajlo@google.com> | 2017-10-21 00:22:05 +0200 |
---|---|---|
committer | Dmitry Lomov <dslomov@google.com> | 2017-10-23 17:16:16 +0200 |
commit | ecdaf520a4b3ec5252cc7256cb481bb6c72e33f2 (patch) | |
tree | 7e8aec1c8b1130f9a1482945696120a64907c991 /src/main | |
parent | e9b10399f6a148a3d20442f2c5020b05fa891873 (diff) |
Micro-optimize mutablelist concatenation
Avoid ArrayList.addAll(Collection) winds up converting the collection to an
array, which is wasteful on such a potentially hot code path.
RELNOTES: None
PiperOrigin-RevId: 172941634
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java | 12 |
1 files changed, 10 insertions, 2 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 382e34a9de..a728796617 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 @@ -359,8 +359,8 @@ public abstract class SkylarkList<E> extends BaseMutableList<E> Mutability mutability) { if (left.getGlobList() == null && right.getGlobList() == null) { ArrayList<T> newContents = new ArrayList<>(left.size() + right.size()); - newContents.addAll(left); - newContents.addAll(right); + addAll(newContents, left.contents); + addAll(newContents, right.contents); return new MutableList<>(newContents, /*globList=*/ null, mutability); } else { // Preserve glob criteria. @@ -371,6 +371,14 @@ public abstract class SkylarkList<E> extends BaseMutableList<E> } } + /** More efficient {@link List#addAll} replacement when both lists are {@link ArrayList}s. */ + private static <T> void addAll(ArrayList<T> addTo, ArrayList<? extends T> addFrom) { + // Hot code path, skip iterator. + for (int i = 0; i < addFrom.size(); i++) { + addTo.add(addFrom.get(i)); + } + } + @Override public MutableList<E> repeat(int times, Mutability mutability) { if (times <= 0) { |