Skip to content

Commit bc40a84

Browse files
dhendryakarnokd
authored andcommitted
Defer creation of the TimeoutException when using the Single.timeout() operator (#5250)
* Defer creation of the TimeoutException when using the Single.timeout() operator * Fix formatting
1 parent e37d1a7 commit bc40a84

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/main/java/rx/Single.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,15 @@ public final Single<T> timeout(long timeout, TimeUnit timeUnit, Single<? extends
22542254
*/
22552255
public final Single<T> timeout(long timeout, TimeUnit timeUnit, Single<? extends T> other, Scheduler scheduler) {
22562256
if (other == null) {
2257-
other = Single.<T> error(new TimeoutException());
2257+
// Use a defer instead of simply other = Single.error(new TimeoutException())
2258+
// since instantiating an exception will cause the current stack trace to be inspected
2259+
// and we only want to incur that overhead when a timeout actually happens.
2260+
other = Single.<T>defer(new Func0<Single<T>>() {
2261+
@Override
2262+
public Single<T> call() {
2263+
return Single.<T>error(new TimeoutException());
2264+
}
2265+
});
22582266
}
22592267
return create(new SingleTimeout<T>(onSubscribe, timeout, timeUnit, scheduler, other.onSubscribe));
22602268
}

0 commit comments

Comments
 (0)