Skip to content

Commit 0f565f2

Browse files
mycallmaxakarnokd
authored andcommitted
Remove dependency of Schedulers from ObservableRefCount (#6452)
In the constructor of `ObservableRefCount` that takes `ConnectableObservable<T> source` as the argument, we set `timeout` to `0L`. In that specific use case of `ObservableRefCount`, `scheduler` is never needed. It's only referenced in `cancel()` method but if timeout is 0, it won't be triggered at all because there is early return. This commit removes the need to depend on `Schedulers.trampoline()` and instead passes null to be scheduler when the ref count is not time-based. The reasons for this change are the following: 1. In projects that don't depend on `Schedulers` class, if there is no reference to `Schedulers`, the whole `Schedulers` can be stripped out of the library after optimizations (e.g., proguard). With constructor that references `Schedulers`, the optimizer can't properly strip it out. In our quick test of our Android app, we were able to reduce the RxJava library size dependency from 51KB to 37KB (after optimization but before compression) by simply avoiding access to `Schedulers` in `ObservableRefCount`. 2. In terms of modularity, `ObservableRefCount` is just an operator so it by itself should probably not have dependency on what available pool of schedulers (`Schedulers`) there are. It should just know that there is some `Scheduler` that could be passed to `ObservableRefCount` when `ObservableRefCount` needs it.
1 parent 4a78cfc commit 0f565f2

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

src/main/java/io/reactivex/internal/operators/flowable/FlowableRefCount.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.reactivex.internal.disposables.*;
2626
import io.reactivex.internal.subscriptions.SubscriptionHelper;
2727
import io.reactivex.plugins.RxJavaPlugins;
28-
import io.reactivex.schedulers.Schedulers;
2928

3029
/**
3130
* Returns an observable sequence that stays connected to the source as long as
@@ -49,7 +48,7 @@ public final class FlowableRefCount<T> extends Flowable<T> {
4948
RefConnection connection;
5049

5150
public FlowableRefCount(ConnectableFlowable<T> source) {
52-
this(source, 1, 0L, TimeUnit.NANOSECONDS, Schedulers.trampoline());
51+
this(source, 1, 0L, TimeUnit.NANOSECONDS, null);
5352
}
5453

5554
public FlowableRefCount(ConnectableFlowable<T> source, int n, long timeout, TimeUnit unit,

src/main/java/io/reactivex/internal/operators/observable/ObservableRefCount.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.reactivex.internal.disposables.*;
2323
import io.reactivex.observables.ConnectableObservable;
2424
import io.reactivex.plugins.RxJavaPlugins;
25-
import io.reactivex.schedulers.Schedulers;
2625

2726
/**
2827
* Returns an observable sequence that stays connected to the source as long as
@@ -46,7 +45,7 @@ public final class ObservableRefCount<T> extends Observable<T> {
4645
RefConnection connection;
4746

4847
public ObservableRefCount(ConnectableObservable<T> source) {
49-
this(source, 1, 0L, TimeUnit.NANOSECONDS, Schedulers.trampoline());
48+
this(source, 1, 0L, TimeUnit.NANOSECONDS, null);
5049
}
5150

5251
public ObservableRefCount(ConnectableObservable<T> source, int n, long timeout, TimeUnit unit,

0 commit comments

Comments
 (0)