-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
1.x: make just() support backpressure #3496
Closed
Closed
Conversation
This file contains 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
This was referenced Nov 5, 2015
akarnokd
force-pushed
the
JustBackpressure1x
branch
from
November 11, 2015 10:45
d6579d2
to
270e0a5
Compare
Rebased. |
akarnokd
force-pushed
the
JustBackpressure1x
branch
from
November 11, 2015 20:58
270e0a5
to
db50d3d
Compare
akarnokd
force-pushed
the
JustBackpressure1x
branch
from
November 11, 2015 21:04
db50d3d
to
47df414
Compare
Interesting. Thanks! 👍 |
This should probably be checked out by @benjchristensen before merging |
Just to check, you are fine with the overhead, right? |
@akarnokd yes. I think consistent behavior is worth the hit. |
👍 |
Any plans to merge this one? |
We are waiting for Ben but I have to fix the merge conflict as well now I see. |
Sorry, I'll redo this entire PR, everything is messed up. |
Replaced by #3614 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
One does not simply add backpressure support to
just()
.Fixes
The reason for this is the bugs hidden by the lack of backpressure support of just(): the overwriting of a previous Producer by
timeout
,zip
andsubscribeOn
. I've fixed uptimeout
with a properProducerArbiter
, had to apply the bugfix from #3493 tozip
(may conflict) and had to rewritesubscribeOn
from scratch and have it anOnSubscribe
. This change required thatSingle.subscribeOn
to be rewritten as well.Benchmark
Let's see the benchmark comparison (i7 4790, Windows 7 x64, Java 8u66):
There are two ways to implement backpressure: with strong atomics or with plain field accesses. The latter tries to exploit the high chance that there won't be concurrent calls to
request()
ever and thus saves on the atomics. As far as I can tell, there is nothing in RxJava 1.x or 2.x that would violate this assumption. However, I added an escape hatch in case of rogue requesters: set therx.just.strong-mode
system parameter to "true" andjust
will run with strong atomics.As seen in the table, the weak version is just slightly better (+3-+10%) in some cases and slightly worse (up to -3%) in other cases. Note, however, the original cases have 2x-5x less overhead.
Maybe the most revealing are the
simple
,simpleEscape
andsimpleEscapeAll
comparison between and within version. What's seen there is that with the original version, the JIT converted the test into a pure stack-allocation and thus saving on overhead in thesimple
case. As the other tests add escapes, it forces the JIT to do regular allocations. Interesting that with this PR, the escape doesn't really matter: this is due to howSubscriber.setProducer
makes the JIT believe the producer escapes.In the
simpleEscapeAll
(which should be the most restrictive for JIT), the overhead is still 2 - 2.3 times bigger: this is due to the extra allocation of aProducer
instance when subscribing.If one remembers my recent blog post, it can be seen that RxJava 2.x does quite well, about 30 Mops/s in the range-1 test (which is equivalent to
simple
).Where does the overhead come from?
SubscriptionList
. In 1.x, theSubscriber
creates aSubscriptionList
whether or not it is ever required. (I've tried my best several times to defer the creation of this list to no success: the performance improved for some cases while worsened for others, see #3479.)The strong/weak optimization is not applied to
scalarScheduleOn
. I haven't benchmarked it but I guess the scheduling overhead overshadows it anyways.Conclusion
I believe the correctness of
just
is more important than its performance, but the increased overhead bothers me nonetheless. Given the architecture of 2.x, I'll look into ways to get rid of the mandatorySubscriptionList
allocation without breaking public API classes such asSubscriber
.