Skip to content

Commit 1ef689d

Browse files
Lift Performance
Using `f.lift()` directly instead of `subscribe` improves ops/second on the included test from 5,907,721 ops/sec to 10,145,486 ops/sec
1 parent 4f841d9 commit 1ef689d

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ public static interface OnSubscribeFunc<T> extends Function {
261261
* @return an Observable that emits values that are the result of applying the bind function to the values
262262
* of the current Observable
263263
*/
264-
public <R> Observable<R> lift(final Operator<? extends R, ? super T> bind) {
264+
public <R> Observable<R> lift(final Operator<? extends R, ? super T> lift) {
265265
return new Observable<R>(new OnSubscribe<R>() {
266266
@Override
267267
public void call(Subscriber<? super R> o) {
268-
subscribe(hook.onLift(bind).call(o));
268+
f.call(hook.onLift(lift).call(o));
269269
}
270270
});
271271
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package rx.composition;
2+
3+
import rx.Observable;
4+
import rx.perf.AbstractPerformanceTester;
5+
import rx.perf.IntegerSumObserver;
6+
import rx.util.functions.Action0;
7+
import rx.util.functions.Func1;
8+
9+
public class RangeMapTakeOnNextPerf extends AbstractPerformanceTester {
10+
11+
final static int NUM = 10;
12+
final static long REPS = REPETITIONS / NUM;
13+
14+
RangeMapTakeOnNextPerf() {
15+
super(REPS);
16+
}
17+
18+
public static void main(String args[]) {
19+
20+
final RangeMapTakeOnNextPerf spt = new RangeMapTakeOnNextPerf();
21+
try {
22+
spt.runTest(new Action0() {
23+
24+
@Override
25+
public void call() {
26+
spt.test();
27+
}
28+
});
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
33+
}
34+
35+
/**
36+
*
37+
* With 'lift' calling the `f` function directly:
38+
*
39+
* Run: 10 - 11,152,996 ops/sec
40+
* Run: 11 - 9,791,825 ops/sec
41+
* Run: 12 - 10,080,035 ops/sec
42+
* Run: 13 - 10,189,525 ops/sec
43+
* Run: 14 - 10,145,486 ops/sec
44+
*
45+
* With `lift` calling `subscribe`:
46+
*
47+
* Run: 10 - 5,592,153 ops/sec
48+
* Run: 11 - 5,881,799 ops/sec
49+
* Run: 12 - 5,853,430 ops/sec
50+
* Run: 13 - 5,902,769 ops/sec
51+
* Run: 14 - 5,907,721 ops/sec
52+
*/
53+
public long test() {
54+
55+
Observable<Integer> s = Observable.range(0, 100).map(new Func1<Integer, Integer>() {
56+
57+
@Override
58+
public Integer call(Integer l) {
59+
return l + 1;
60+
}
61+
62+
}).take(NUM);
63+
IntegerSumObserver o = new IntegerSumObserver();
64+
65+
for (long l = 0; l < REPS; l++) {
66+
s.subscribe(o);
67+
}
68+
return o.sum;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)