Skip to content

SafeSubscriber memory reduction #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions rxjava-core/src/main/java/rx/observers/SafeSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package rx.observers;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import rx.Subscriber;
import rx.exceptions.CompositeException;
Expand Down Expand Up @@ -60,7 +60,11 @@
public class SafeSubscriber<T> extends Subscriber<T> {

private final Subscriber<? super T> actual;
private final AtomicBoolean isFinished = new AtomicBoolean(false);
/** Terminal state indication if not zero. */
volatile int done;
@SuppressWarnings("rawtypes")
static final AtomicIntegerFieldUpdater<SafeSubscriber> DONE_UPDATER
= AtomicIntegerFieldUpdater.newUpdater(SafeSubscriber.class, "done");

public SafeSubscriber(Subscriber<? super T> actual) {
super(actual);
Expand All @@ -69,7 +73,7 @@ public SafeSubscriber(Subscriber<? super T> actual) {

@Override
public void onCompleted() {
if (isFinished.compareAndSet(false, true)) {
if (DONE_UPDATER.getAndSet(this, 1) == 0) {
try {
actual.onCompleted();
} catch (Throwable e) {
Expand All @@ -90,15 +94,15 @@ public void onError(Throwable e) {
// we handle here instead of another method so we don't add stacks to the frame
// which can prevent it from being able to handle StackOverflow
Exceptions.throwIfFatal(e);
if (isFinished.compareAndSet(false, true)) {
if (DONE_UPDATER.getAndSet(this, 1) == 0) {
_onError(e);
}
}

@Override
public void onNext(T args) {
try {
if (!isFinished.get()) {
if (done == 0) {
actual.onNext(args);
}
} catch (Throwable e) {
Expand Down