Skip to content

Commit e5ab8a9

Browse files
Merge pull request #818 from akarnokd/CompositeSubscriptionPerf3
CompositeSubscription memory reduction
2 parents 73dd109 + 8ff7d40 commit e5ab8a9

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

rxjava-core/src/main/java/rx/subscriptions/CompositeSubscription.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20-
import java.util.Collection;
2120
import java.util.List;
2221
import java.util.concurrent.atomic.AtomicReference;
2322

@@ -34,7 +33,15 @@ public final class CompositeSubscription implements Subscription {
3433

3534
private final AtomicReference<State> state = new AtomicReference<State>();
3635

37-
private static final State CLEAR_STATE = new State(false, new Subscription[0]);
36+
/** Empty initial state. */
37+
private static final State CLEAR_STATE;
38+
/** Unsubscribed empty state. */
39+
private static final State CLEAR_STATE_UNSUBSCRIBED;
40+
static {
41+
Subscription[] s0 = new Subscription[0];
42+
CLEAR_STATE = new State(false, s0);
43+
CLEAR_STATE_UNSUBSCRIBED = new State(true, s0);
44+
}
3845

3946
private static final class State {
4047
final boolean isUnsubscribed;
@@ -46,7 +53,7 @@ private static final class State {
4653
}
4754

4855
State unsubscribe() {
49-
return new State(true, subscriptions);
56+
return CLEAR_STATE_UNSUBSCRIBED;
5057
}
5158

5259
State add(Subscription s) {
@@ -66,7 +73,7 @@ State remove(Subscription s) {
6673
}
6774

6875
State clear() {
69-
return new State(isUnsubscribed, new Subscription[0]);
76+
return isUnsubscribed ? CLEAR_STATE_UNSUBSCRIBED : CLEAR_STATE;
7077
}
7178
}
7279

@@ -78,6 +85,7 @@ public CompositeSubscription(final Subscription... subscriptions) {
7885
state.set(new State(false, subscriptions));
7986
}
8087

88+
@Override
8189
public boolean isUnsubscribed() {
8290
return state.get().isUnsubscribed;
8391
}

0 commit comments

Comments
 (0)