Skip to content

Commit 0896f70

Browse files
committed
use LinkedList to buffer the sequence’s items
LinkedList has guaranteed constant insertion time when appending to the end of the list, whereas ArrayList takes O(1) amortized, since a reallocation might be necessary to insert further items. Since no capacity was specified for the buffer, on Hotspot this would cause the array to reallocate after the first 10 insertions, on Android after the first insertion (since Android’s ArrayList uses a default capacity of zero.) Since the buffer is copied to an ArrayList before emission, subscriber performance when working with the list should remain unaffected. Refs #1141
1 parent d64b3a1 commit 0896f70

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

rxjava-core/src/main/java/rx/operators/OperatorToObservableList.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package rx.operators;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
20-
2118
import rx.Observable.Operator;
2219
import rx.Subscriber;
2320

21+
import java.util.ArrayList;
22+
import java.util.LinkedList;
23+
import java.util.List;
24+
2425
/**
2526
* Returns an Observable that emits a single item, a list composed of all the items emitted by the
2627
* source Observable.
@@ -41,7 +42,7 @@ public final class OperatorToObservableList<T> implements Operator<List<T>, T> {
4142
public Subscriber<? super T> call(final Subscriber<? super List<T>> o) {
4243
return new Subscriber<T>(o) {
4344

44-
final List<T> list = new ArrayList<T>();
45+
final List<T> list = new LinkedList<T>();
4546

4647
@Override
4748
public void onCompleted() {

0 commit comments

Comments
 (0)