Skip to content
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

materialize() - add backpressure support #3103

Merged
merged 1 commit into from Aug 6, 2015

Conversation

davidmoten
Copy link
Collaborator

As mentioned in #3098 the existing version of materialize could deliver one more event than requested being the termination event (completion or error).

This PR ensures that a termination event is buffered till requested.

@akarnokd akarnokd added the Bug label Jul 24, 2015
private final Subscriber<? super Notification<T>> child;

// guarded by this
private Notification<T> terminalNotification;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made volatile and there is no need to synchronize to read its value.

@davidmoten
Copy link
Collaborator Author

Thanks @akarnokd, I've updated with those fixes and rebased.

@davidmoten
Copy link
Collaborator Author

A quick question about synchronized blocks vs using volatile variables in general. It would also be possible to atomically change a pair like busy and missed using an immutable wrapping object BusyAndMissed and storing it in an AtomicReference<BusyAndMissed> and use spinning to modify. It could involve some GC pressure if rapidly changing of course. Have you any experience of advantages/disadvantages with this approach?

@akarnokd
Copy link
Member

That approach is wasteful. You only need an AtomicInteger and its increment/decrement methods. 0 means not busy, 1+ means busy and 2+ means there is still work to be done. The pair of booleans + synchronized, however, plays into the hands of lock elision and biased locking which affects our benchmarks. If it were up to me, I'd use atomics everywhere so we avoid the chance of unnecessary thread suspension due to a lock not available around 2 bytes.

@davidmoten
Copy link
Collaborator Author

Thanks, quite right for that pair of objects, was probably a bad example. So under circumstances where we are getting significant lock elision and biased locking is it also the case that the volatile read and write of terminalNotification is just like a non-volatile read and write? I left it as non-volatile to favour lock elision and biased locking in the first place.

@akarnokd
Copy link
Member

That is a terminal event that happens once, but when the lock optimizations don't happen, you may get a hefty delay. Paying a few dozen nanoseconds for a single volatile write is way better.

@davidmoten
Copy link
Collaborator Author

Makes sense, thanks.

@akarnokd
Copy link
Member

akarnokd commented Aug 6, 2015

Thanks!

akarnokd added a commit that referenced this pull request Aug 6, 2015
materialize() - add backpressure support
@akarnokd akarnokd merged commit 1bb9cba into ReactiveX:1.x Aug 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants