Skip to content

Commit 26cd9dd

Browse files
akarnokdakarnokd
authored andcommitted
Some performance improvements
1 parent 5454879 commit 26cd9dd

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

rxjava-core/src/main/java/rx/subjects/BehaviorSubject.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private static <T> BehaviorSubject<T> create(T defaultValue, boolean hasDefault)
9898
static final class State<T> {
9999
final AtomicReference<Object> latest = new AtomicReference<Object>();
100100
final AtomicReference<BehaviorState> observers = new AtomicReference<BehaviorState>(BehaviorState.EMPTY);
101+
boolean active = true;
101102
void set(Object value) {
102103
this.latest.set(value);
103104
}
@@ -139,6 +140,7 @@ BehaviorObserver<T>[] next(Object n) {
139140
}
140141
BehaviorObserver<T>[] terminate(Object n) {
141142
set(n);
143+
active = false;
142144
do {
143145
BehaviorState oldState = observers.get();
144146
if (oldState.terminated) {
@@ -149,9 +151,6 @@ BehaviorObserver<T>[] terminate(Object n) {
149151
}
150152
} while (true);
151153
}
152-
boolean isActive() {
153-
return !observers.get().terminated;
154-
}
155154
}
156155
static final class BehaviorState {
157156
final boolean terminated;
@@ -241,7 +240,7 @@ protected BehaviorSubject(OnSubscribe<T> onSubscribe, State<T> state) {
241240
@Override
242241
public void onCompleted() {
243242
Object last = state.get();
244-
if (last == null || state.isActive()) {
243+
if (last == null || state.active) {
245244
Object n = nl.completed();
246245
for (BehaviorObserver<T> bo : state.terminate(n)) {
247246
bo.emitNext(n);
@@ -252,7 +251,7 @@ public void onCompleted() {
252251
@Override
253252
public void onError(Throwable e) {
254253
Object last = state.get();
255-
if (last == null || state.isActive()) {
254+
if (last == null || state.active) {
256255
Object n = nl.error(e);
257256
for (BehaviorObserver<T> bo : state.terminate(n)) {
258257
bo.emitNext(n);
@@ -263,7 +262,7 @@ public void onError(Throwable e) {
263262
@Override
264263
public void onNext(T v) {
265264
Object last = state.get();
266-
if (last == null || state.isActive()) {
265+
if (last == null || state.active) {
267266
Object n = nl.next(v);
268267
for (BehaviorObserver<T> bo : state.next(n)) {
269268
bo.emitNext(n);
@@ -284,8 +283,6 @@ private static final class BehaviorObserver<T> {
284283
boolean emitting;
285284
/** Guarded by this. */
286285
List<Object> queue;
287-
/** Accessed only from serialized state. */
288-
boolean done;
289286
/* volatile */boolean fastPath;
290287
public BehaviorObserver(Observer<? super T> actual) {
291288
this.actual = actual;
@@ -349,10 +346,7 @@ void emitLoop(List<Object> localQueue, Object current) {
349346
}
350347
}
351348
void accept(Object n) {
352-
if (n != null && !done) {
353-
if (nl.isCompleted(n) || nl.isError(n)) {
354-
done = true;
355-
}
349+
if (n != null) {
356350
nl.accept(actual, n);
357351
}
358352
}

rxjava-core/src/test/java/rx/subjects/BehaviorSubjectTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,12 @@ public void testStartEmpty() {
304304

305305
source.onNext(2);
306306

307+
verify(o, never()).onError(any(Throwable.class));
308+
307309
inOrder.verify(o).onNext(1);
308310
inOrder.verify(o).onCompleted();
309311
inOrder.verifyNoMoreInteractions();
310312

311-
verify(o, never()).onError(any(Throwable.class));
312313

313314
}
314315
@Test

0 commit comments

Comments
 (0)