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

Reduce Multi allocations #690

Merged
merged 7 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static io.smallrye.mutiny.helpers.EmptyUniSubscription.CANCELLED;
import static io.smallrye.mutiny.helpers.ParameterValidation.nonNull;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Consumer;

import org.reactivestreams.Subscription;
Expand All @@ -21,7 +21,10 @@
*/
public class UniCallbackSubscriber<T> implements UniSubscriber<T>, UniSubscription {

private final AtomicReference<UniSubscription> subscription = new AtomicReference<>();
private volatile UniSubscription subscription;
private static final AtomicReferenceFieldUpdater<UniCallbackSubscriber, UniSubscription> SUBSCRIPTION_UPDATER = AtomicReferenceFieldUpdater
.newUpdater(UniCallbackSubscriber.class, UniSubscription.class, "subscription");

private final Consumer<? super T> onResultCallback;
private final Consumer<? super Throwable> onFailureCallback;

Expand All @@ -40,7 +43,7 @@ public UniCallbackSubscriber(Consumer<? super T> onResultCallback,

@Override
public final void onSubscribe(UniSubscription sub) {
if (!subscription.compareAndSet(null, sub)) {
if (!SUBSCRIPTION_UPDATER.compareAndSet(this, null, sub)) {
// cancelling this second subscription
// because we already add a subscription (maybe CANCELLED)
sub.cancel();
Expand All @@ -49,7 +52,7 @@ public final void onSubscribe(UniSubscription sub) {

@Override
public final void onFailure(Throwable t) {
UniSubscription sub = subscription.getAndSet(CANCELLED);
UniSubscription sub = SUBSCRIPTION_UPDATER.getAndSet(this, CANCELLED);
if (sub == CANCELLED) {
// Already cancelled, do nothing
return;
Expand All @@ -59,7 +62,7 @@ public final void onFailure(Throwable t) {

@Override
public final void onItem(T x) {
Subscription sub = subscription.getAndSet(CANCELLED);
Subscription sub = SUBSCRIPTION_UPDATER.getAndSet(this, CANCELLED);
if (sub == CANCELLED) {
// Already cancelled, do nothing
return;
Expand All @@ -74,7 +77,7 @@ public final void onItem(T x) {

@Override
public void cancel() {
Subscription sub = subscription.getAndSet(CANCELLED);
Subscription sub = SUBSCRIPTION_UPDATER.getAndSet(this, CANCELLED);
if (sub != null) {
sub.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static final class BufferExactProcessor<T> extends MultiOperatorProcessor<T, Lis

@Override
public void request(long n) {
Subscription subscription = upstream.get();
Subscription subscription = getUpstreamSubscription();
if (subscription != CANCELLED) {
subscription.request(Subscriptions.multiply(n, size));
}
Expand All @@ -110,7 +110,7 @@ public void onItem(T t) {

@Override
public void onCompletion() {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
List<T> buffer = current;
if (buffer != null && !buffer.isEmpty()) {
Expand Down Expand Up @@ -179,7 +179,7 @@ public void onItem(T item) {

@Override
public void onFailure(Throwable t) {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
current = null;
downstream.onFailure(t);
Expand All @@ -190,7 +190,7 @@ public void onFailure(Throwable t) {

@Override
public void onCompletion() {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
List<T> buffer = current;
current = null;
Expand Down Expand Up @@ -277,7 +277,7 @@ public void onItem(T item) {

@Override
public void onFailure(Throwable t) {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
downstream.onFailure(t);
} else {
Expand All @@ -287,7 +287,7 @@ public void onFailure(Throwable t) {

@Override
public void onCompletion() {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
long p = produced;
if (p != 0L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void onFailure(Throwable throwable) {

@Override
public void onSubscribe(Subscription subscription) {
if (upstream.compareAndSet(null, subscription)) {
if (compareAndSetUpstreamSubscription(null, subscription)) {
doOnSubscribe();
downstream.onSubscribe(this);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static class CollectorProcessor<T, A, R> extends MultiOperatorProcessor<T, R> {

@Override
public void onItem(T item) {
if (upstream.get() != CANCELLED) {
if (getUpstreamSubscription() != CANCELLED) {
try {
accumulator.accept(intermediate, item);
} catch (Throwable ex) {
Expand All @@ -83,7 +83,7 @@ public void onItem(T item) {

@Override
public void onCompletion() {
Subscription subscription = upstream.getAndSet(Subscriptions.CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(Subscriptions.CANCELLED);
if (subscription != Subscriptions.CANCELLED) {
R result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static final class MultiEmitOnProcessor<T> extends MultiOperatorProcessor<T, T>

@Override
public void onSubscribe(Subscription subscription) {
if (upstream.compareAndSet(null, subscription)) {
if (compareAndSetUpstreamSubscription(null, subscription)) {
downstream.onSubscribe(this);
subscription.request(16);
} else {
Expand All @@ -102,7 +102,7 @@ public void onItem(T t) {
if (!queue.offer(t)) {
// queue full, this is a failure.
// onError will schedule.
Subscriptions.cancel(upstream); // cancel upstream
cancelUpstream(); // cancel upstream
onFailure(new BackPressureFailure("Queue is full, the upstream didn't enforce the requests"));
done = true;
} else {
Expand Down Expand Up @@ -145,7 +145,7 @@ public void cancel() {
return;
}
cancelled = true;
Subscriptions.cancel(upstream);
cancelUpstream();
if (wip.getAndIncrement() == 0) {
// nothing was currently dispatched, clearing the queue.
queue.clear();
Expand All @@ -161,10 +161,10 @@ void schedule() {
try {
executor.execute(this);
} catch (RejectedExecutionException rejected) {
Subscription s = upstream.getAndSet(CANCELLED);
if (s != CANCELLED) {
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
done = true;
Subscriptions.cancel(upstream);
cancelUpstream();
queue.clear();
downstream.onFailure(rejected);
super.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.*;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -75,7 +73,9 @@ public static final class FlatMapMainSubscriber<I, O> extends FlatMapManager<Fla
volatile boolean done;
volatile boolean cancelled;

final AtomicReference<Subscription> upstream = new AtomicReference<>();
volatile Subscription upstream = null;
private static final AtomicReferenceFieldUpdater<FlatMapMainSubscriber, Subscription> UPSTREAM_UPDATER = AtomicReferenceFieldUpdater
.newUpdater(FlatMapMainSubscriber.class, Subscription.class, "upstream");

AtomicLong requested = new AtomicLong();

Expand Down Expand Up @@ -150,15 +150,15 @@ public void cancel() {

if (wip.getAndIncrement() == 0) {
clearQueue();
upstream.getAndSet(Subscriptions.CANCELLED).cancel();
UPSTREAM_UPDATER.getAndSet(this, Subscriptions.CANCELLED).cancel();
unsubscribe();
}
}
}

@Override
public void onSubscribe(Subscription s) {
if (upstream.compareAndSet(null, s)) {
if (UPSTREAM_UPDATER.compareAndSet(this, null, s)) {
downstream.onSubscribe(this);
s.request(Subscriptions.unboundedOrRequests(maxConcurrency));
}
Expand Down Expand Up @@ -447,7 +447,7 @@ void drainLoop() {
}

if (replenishMain != 0L && !done && !cancelled) {
upstream.get().request(replenishMain);
upstream.request(replenishMain);
}

if (again) {
Expand All @@ -463,7 +463,7 @@ void drainLoop() {

private void cancelUpstream(boolean fromOnError) {
clearQueue();
Subscription subscription = upstream.getAndSet(Subscriptions.CANCELLED);
Subscription subscription = UPSTREAM_UPDATER.getAndSet(this, Subscriptions.CANCELLED);
if (subscription != null) {
subscription.cancel();
}
Expand Down Expand Up @@ -566,7 +566,9 @@ static final class FlatMapInner<O> implements Subscription, MultiSubscriber<O> {

final int limit;

final AtomicReference<Subscription> subscription = new AtomicReference<>();
volatile Subscription subscription = null;
private static final AtomicReferenceFieldUpdater<FlatMapInner, Subscription> SUBSCRIPTION_UPDATER = AtomicReferenceFieldUpdater
.newUpdater(FlatMapInner.class, Subscription.class, "subscription");

long produced;

Expand All @@ -585,7 +587,7 @@ static final class FlatMapInner<O> implements Subscription, MultiSubscriber<O> {
@Override
public void onSubscribe(Subscription s) {
Objects.requireNonNull(s);
if (subscription.compareAndSet(null, s)) {
if (SUBSCRIPTION_UPDATER.compareAndSet(this, null, s)) {
s.request(Subscriptions.unboundedOrRequests(requests));
}
}
Expand Down Expand Up @@ -613,7 +615,7 @@ public void request(long n) {
long p = produced + n;
if (p >= limit) {
produced = 0L;
subscription.get().request(p);
subscription.request(p);
} else {
produced = p;
}
Expand All @@ -626,7 +628,7 @@ public void cancel() {

public void cancel(boolean doNotCancel) {
if (!doNotCancel) {
Subscription last = subscription.getAndSet(Subscriptions.CANCELLED);
Subscription last = SUBSCRIPTION_UPDATER.getAndSet(this, Subscriptions.CANCELLED);
if (last != null) {
last.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public MultiGroupByProcessor(MultiSubscriber<? super GroupedMulti<K, V>> downstr

@Override
public void onSubscribe(Subscription subscription) {
if (upstream.compareAndSet(null, subscription)) {
if (compareAndSetUpstreamSubscription(null, subscription)) {
// Propagate subscription to downstream.
downstream.onSubscribe(this);
subscription.request(128);
Expand Down Expand Up @@ -136,7 +136,7 @@ public void onItem(T item) {

@Override
public void onFailure(Throwable throwable) {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
done = true;
groups.values().forEach(group -> group.onFailure(throwable));
Expand All @@ -151,7 +151,7 @@ public void onFailure(Throwable throwable) {

@Override
public void onCompletion() {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
done = true;
groups.values().forEach(GroupedUnicast::onComplete);
Expand All @@ -175,7 +175,7 @@ public void cancel() {
// but running groups still require new values
if (cancelled.compareAndSet(false, true)) {
if (groupCount.decrementAndGet() == 0) {
Subscriptions.cancel(upstream);
cancelUpstream();
}
}
}
Expand All @@ -184,7 +184,7 @@ public void cancel(K key) {
Object mapKey = key != null ? key : NO_KEY;
groups.remove(mapKey);
if (groupCount.decrementAndGet() == 0) {
Subscriptions.cancel(upstream);
cancelUpstream();

if (wip.getAndIncrement() == 0) {
queue.clear();
Expand Down Expand Up @@ -410,7 +410,7 @@ void drain() {
if (r != Long.MAX_VALUE) {
requested.addAndGet(-e);
}
parent.upstream.get().request(e);
parent.getUpstreamSubscription().request(e);
}
}

Expand All @@ -431,7 +431,7 @@ boolean hasCompleted(boolean isDone, boolean isEmpty, long emitted) {
emitted++;
}
if (emitted != 0) {
parent.upstream.get().request(emitted);
parent.getUpstreamSubscription().request(emitted);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public MultiIgnoreProcessor(MultiSubscriber<? super Void> downstream) {

@Override
public void onSubscribe(Subscription subscription) {
if (upstream.compareAndSet(null, subscription)) {
if (compareAndSetUpstreamSubscription(null, subscription)) {
// Propagate subscription to downstream.
downstream.onSubscribe(this);
subscription.request(Long.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static final class MultiLastItemProcessor<T> extends MultiOperatorProcessor<T, T

@Override
public void onSubscribe(Subscription subscription) {
if (upstream.compareAndSet(null, subscription)) {
if (compareAndSetUpstreamSubscription(null, subscription)) {
downstream.onSubscribe(this);
subscription.request(Long.MAX_VALUE);
} else {
Expand All @@ -54,7 +54,7 @@ public void onFailure(Throwable failure) {

@Override
public void onCompletion() {
Subscription subscription = upstream.getAndSet(CANCELLED);
Subscription subscription = getAndSetUpstreamSubscription(CANCELLED);
if (subscription != CANCELLED) {
T item = last;
MultiSubscriber<? super T> subscriber = downstream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.smallrye.mutiny.operators.multi;

import static io.smallrye.mutiny.helpers.ParameterValidation.nonNull;
import static io.smallrye.mutiny.helpers.Subscriptions.CANCELLED;

import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -37,7 +38,7 @@ public MultiOnFailureInvokeProcessor(MultiSubscriber<? super T> downstream) {

@Override
public void onFailure(Throwable failure) {
Subscription up = upstream.getAndSet(Subscriptions.CANCELLED);
Subscription up = getAndSetUpstreamSubscription(CANCELLED);
MultiSubscriber<? super T> subscriber = downstream;
if (up != Subscriptions.CANCELLED) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MultiOnItemInvokeProcessor(MultiSubscriber<? super T> downstream) {

@Override
public void onItem(T item) {
if (upstream.get() != Subscriptions.CANCELLED) {
if (getUpstreamSubscription() != Subscriptions.CANCELLED) {
MultiSubscriber<? super T> subscriber = this.downstream;
try {
callback.accept(item);
Expand Down
Loading