Skip to content

Commit

Permalink
Merge pull request #818 from akarnokd/CompositeSubscriptionPerf3
Browse files Browse the repository at this point in the history
CompositeSubscription memory reduction
  • Loading branch information
benjchristensen committed Feb 5, 2014
2 parents 73dd109 + 8ff7d40 commit e5ab8a9
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

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

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

private static final State CLEAR_STATE = new State(false, new Subscription[0]);
/** Empty initial state. */
private static final State CLEAR_STATE;
/** Unsubscribed empty state. */
private static final State CLEAR_STATE_UNSUBSCRIBED;
static {
Subscription[] s0 = new Subscription[0];
CLEAR_STATE = new State(false, s0);
CLEAR_STATE_UNSUBSCRIBED = new State(true, s0);
}

private static final class State {
final boolean isUnsubscribed;
Expand All @@ -46,7 +53,7 @@ private static final class State {
}

State unsubscribe() {
return new State(true, subscriptions);
return CLEAR_STATE_UNSUBSCRIBED;
}

State add(Subscription s) {
Expand All @@ -66,7 +73,7 @@ State remove(Subscription s) {
}

State clear() {
return new State(isUnsubscribed, new Subscription[0]);
return isUnsubscribed ? CLEAR_STATE_UNSUBSCRIBED : CLEAR_STATE;
}
}

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

@Override
public boolean isUnsubscribed() {
return state.get().isUnsubscribed;
}
Expand Down

0 comments on commit e5ab8a9

Please sign in to comment.