-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: Optimizate single just #3622
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
src/main/java/rx/internal/util/ScalarSynchronousSingle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/** | ||
* Copyright 2014 Netflix, Inc. | ||
* <p/> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p/> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p/> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package rx.internal.util; | ||
|
||
import rx.Scheduler; | ||
import rx.Scheduler.Worker; | ||
import rx.Single; | ||
import rx.SingleSubscriber; | ||
import rx.Subscriber; | ||
import rx.functions.Action0; | ||
import rx.functions.Func1; | ||
import rx.internal.schedulers.EventLoopsScheduler; | ||
|
||
public final class ScalarSynchronousSingle<T> extends Single<T> { | ||
|
||
public static final <T> ScalarSynchronousSingle<T> create(T t) { | ||
return new ScalarSynchronousSingle<T>(t); | ||
} | ||
|
||
final T t; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename this to |
||
|
||
protected ScalarSynchronousSingle(final T t) { | ||
super(new OnSubscribe<T>() { | ||
|
||
@Override | ||
public void call(SingleSubscriber<? super T> te) { | ||
te.onSuccess(t); | ||
} | ||
|
||
}); | ||
this.t = t; | ||
} | ||
|
||
public T get() { | ||
return t; | ||
} | ||
|
||
/** | ||
* Customized observeOn/subscribeOn implementation which emits the scalar | ||
* value directly or with less overhead on the specified scheduler. | ||
* | ||
* @param scheduler the target scheduler | ||
* @return the new observable | ||
*/ | ||
public Single<T> scalarScheduleOn(Scheduler scheduler) { | ||
if (scheduler instanceof EventLoopsScheduler) { | ||
EventLoopsScheduler es = (EventLoopsScheduler) scheduler; | ||
return create(new DirectScheduledEmission<T>(es, t)); | ||
} | ||
return create(new NormalScheduledEmission<T>(scheduler, t)); | ||
} | ||
|
||
/** | ||
* Optimized observeOn for scalar value observed on the EventLoopsScheduler. | ||
*/ | ||
static final class DirectScheduledEmission<T> implements OnSubscribe<T> { | ||
private final EventLoopsScheduler es; | ||
private final T value; | ||
|
||
DirectScheduledEmission(EventLoopsScheduler es, T value) { | ||
this.es = es; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void call(SingleSubscriber<? super T> singleSubscriber) { | ||
singleSubscriber.add(es.scheduleDirect(new ScalarSynchronousSingleAction<T>(singleSubscriber, value))); | ||
} | ||
} | ||
|
||
/** | ||
* Emits a scalar value on a general scheduler. | ||
*/ | ||
static final class NormalScheduledEmission<T> implements OnSubscribe<T> { | ||
private final Scheduler scheduler; | ||
private final T value; | ||
|
||
NormalScheduledEmission(Scheduler scheduler, T value) { | ||
this.scheduler = scheduler; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void call(SingleSubscriber<? super T> singleSubscriber) { | ||
Worker worker = scheduler.createWorker(); | ||
singleSubscriber.add(worker); | ||
worker.schedule(new ScalarSynchronousSingleAction<T>(singleSubscriber, value)); | ||
} | ||
} | ||
|
||
/** | ||
* Action that emits a single value when called. | ||
*/ | ||
static final class ScalarSynchronousSingleAction<T> implements Action0 { | ||
private final SingleSubscriber<? super T> subscriber; | ||
private final T value; | ||
|
||
ScalarSynchronousSingleAction(SingleSubscriber<? super T> subscriber, | ||
T value) { | ||
this.subscriber = subscriber; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void call() { | ||
try { | ||
subscriber.onSuccess(value); | ||
} catch (Throwable t) { | ||
subscriber.onError(t); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public <R> Single<R> scalarFlatMap(final Func1<? super T, ? extends Single<? extends R>> func) { | ||
return create(new OnSubscribe<R>() { | ||
@Override | ||
public void call(final SingleSubscriber<? super R> child) { | ||
|
||
Single<? extends R> o = func.call(t); | ||
if (o.getClass() == ScalarSynchronousSingle.class) { | ||
child.onSuccess(((ScalarSynchronousSingle<? extends R>) o).t); | ||
} else { | ||
o.unsafeSubscribe(new Subscriber<R>() { | ||
@Override | ||
public void onCompleted() { | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
child.onError(e); | ||
} | ||
|
||
@Override | ||
public void onNext(R r) { | ||
child.onSuccess(r); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears
instanceof
can be optimized much better by JIT than class equivalence checks. Could you rewrite this and all the other places?