From c9284530d8be41f16fca7dbd54bc764ea740710f Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Tue, 14 Jun 2016 01:08:42 -0400 Subject: [PATCH] Provide faster defensive copy. Without this AbstractCollection defaults to an iterator-based approach instead of a simple array copy. --- .../main/java/com/squareup/wire/internal/ImmutableList.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wire-runtime/src/main/java/com/squareup/wire/internal/ImmutableList.java b/wire-runtime/src/main/java/com/squareup/wire/internal/ImmutableList.java index 5974c247a2..9fe497d50f 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/internal/ImmutableList.java +++ b/wire-runtime/src/main/java/com/squareup/wire/internal/ImmutableList.java @@ -24,7 +24,7 @@ import java.util.RandomAccess; final class ImmutableList extends AbstractList implements RandomAccess, Serializable { - final List list; + private final ArrayList list; ImmutableList(List list) { this.list = new ArrayList<>(list); @@ -38,6 +38,10 @@ final class ImmutableList extends AbstractList implements RandomAccess, Se return list.get(i); } + @Override public Object[] toArray() { + return list.toArray(); // Optimizing for mutable copy by MutableOnWriteList. + } + private Object writeReplace() throws ObjectStreamException { return Collections.unmodifiableList(list); }