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

Make some Mono sources and aggregators lazier #3081

Merged
merged 25 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
18c36c7
adds support for lazy evaluation for `MonoCallable`
OlegDokuka Jun 17, 2022
8590947
fixes callable tests
OlegDokuka Jun 18, 2022
743ec19
adds lazy execution to `MonoAll`
OlegDokuka Jun 18, 2022
a34b17a
adds lazy execution to `MonoZip`
OlegDokuka Jun 27, 2022
6f69252
removes async fusion checking tests
OlegDokuka Jun 28, 2022
1e12d77
adds discard support for monoAll
OlegDokuka Jun 28, 2022
ef2afb2
simplifies MonoCallable impl
OlegDokuka Jun 29, 2022
42fa21f
makes MonoAny lazy
OlegDokuka Jun 29, 2022
eeb0307
fixes callable behaviours
OlegDokuka Jun 30, 2022
576be09
makes MonoCount lazy
OlegDokuka Jun 30, 2022
274505f
fixes failing tests
OlegDokuka Jul 1, 2022
1c9c45c
makes ParallelThen lazy
OlegDokuka Jul 4, 2022
4be49da
makes MonoFlatMap lazy
OlegDokuka Jul 4, 2022
14be9e8
fixes tests
OlegDokuka Jul 4, 2022
8f2a7c4
makes MonoCollectList lazy
OlegDokuka Jul 5, 2022
c3f0e96
fixes tests
OlegDokuka Jul 5, 2022
9ac6e9d
makes MonoCollect lazy
OlegDokuka Jul 5, 2022
b786d15
reworks flux to mono operators to support backpressure
OlegDokuka Jul 12, 2022
12382e0
makes `MonoStreamCollector` lazy
OlegDokuka Jul 12, 2022
305ab49
migrates remaining operators and generify impls
OlegDokuka Jul 26, 2022
c0995ca
fixes discard issue
OlegDokuka Jul 27, 2022
27de820
fixes comments
OlegDokuka Aug 1, 2022
6991d1d
added comment on the MonoZip cancelledSubscription trick
simonbasle Aug 2, 2022
38a3ece
adds onDiscard checks and fixes related issues
OlegDokuka Aug 3, 2022
6fb28a5
apply spotless
OlegDokuka Aug 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 reactor.core.publisher;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class MonoAllBenchmark {

@Param({"0", "10", "1000", "100000"})
int rangeSize;

public static void main(String[] args) throws Exception {
reactor.core.scrabble.ShakespearePlaysScrabbleOpt
s = new reactor.core.scrabble.ShakespearePlaysScrabbleOpt();
s.init();
System.out.println(s.measureThroughput());
}

@SuppressWarnings("unused")
@Benchmark
public void measureThroughput() {
Flux.range(0, rangeSize)
.all(i -> i < Integer.MAX_VALUE)
.block();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 reactor.core.publisher;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class MonoCallableBenchmark {

@Param({"10", "1000", "100000"})
int rangeSize;

public static void main(String[] args) throws Exception {
reactor.core.scrabble.ShakespearePlaysScrabbleOpt
s = new reactor.core.scrabble.ShakespearePlaysScrabbleOpt();
s.init();
System.out.println(s.measureThroughput());
}

@SuppressWarnings("unused")
@Benchmark
public void measureThroughput() {
Flux.range(0, rangeSize)
.all(i -> i < Integer.MAX_VALUE)
.block();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2016-2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,21 +37,7 @@ final class FluxCallable<T> extends Flux<T> implements Callable<T>, Fuseable, So

@Override
public void subscribe(CoreSubscriber<? super T> actual) {
Operators.MonoSubscriber<T, T> wrapper = new Operators.MonoSubscriber<>(actual);
actual.onSubscribe(wrapper);

try {
T v = callable.call();
if (v == null) {
wrapper.onComplete();
}
else {
wrapper.complete(v);
}
}
catch (Throwable ex) {
actual.onError(Operators.onOperatorError(ex, actual.currentContext()));
}
actual.onSubscribe(new MonoCallable.MonoCallableSubscription<>(actual, callable));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2016-2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,9 @@
package reactor.core.publisher;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Fuseable;
import reactor.util.annotation.Nullable;

/**
Expand Down Expand Up @@ -50,75 +49,120 @@ public Object scanUnsafe(Attr key) {
}

static final class DefaultIfEmptySubscriber<T>
extends Operators.MonoSubscriber<T, T> {
extends Operators.BaseFluxToMonoOperator<T, T> {

Subscription s;
boolean done;

boolean hasValue;

DefaultIfEmptySubscriber(CoreSubscriber<? super T> actual, T value) {
volatile T fallbackValue;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater<DefaultIfEmptySubscriber, Object> FALLBACK_VALUE =
AtomicReferenceFieldUpdater.newUpdater(DefaultIfEmptySubscriber.class, Object.class, "fallbackValue");

DefaultIfEmptySubscriber(CoreSubscriber<? super T> actual, T fallbackValue) {
super(actual);
//noinspection deprecation
this.value = value; //we write once, setValue() is NO-OP
FALLBACK_VALUE.lazySet(this, fallbackValue);
}

@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) return s;
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
if (key == Attr.TERMINATED) return done;

return super.scanUnsafe(key);
}

@Override
public void request(long n) {
super.request(n);
if (!hasRequest) {
hasRequest = true;

final int state = this.state;

if (state != 1 && STATE.compareAndSet(this, state, state | 1)) {
if (state > 1) {
final T fallbackValue = this.fallbackValue;
if (fallbackValue != null && FALLBACK_VALUE.compareAndSet(this,
fallbackValue,
null)) {
// completed before request means source was empty
actual.onNext(fallbackValue);
actual.onComplete();
}
return;
}
}
}

s.request(n);
}

@Override
public void cancel() {
super.cancel();
s.cancel();
}

@Override
public void onSubscribe(Subscription s) {
if (Operators.validate(this.s, s)) {
this.s = s;

actual.onSubscribe(this);
final T fallbackValue = this.fallbackValue;
if (fallbackValue != null && FALLBACK_VALUE.compareAndSet(this, fallbackValue, null)) {
Operators.onDiscard(fallbackValue, actual.currentContext());
}
}

@Override
public void onNext(T t) {
if (!hasValue) {
hasValue = true;

final T fallbackValue = this.fallbackValue;
if (fallbackValue != null && FALLBACK_VALUE.compareAndSet(this, fallbackValue, null)) {
Operators.onDiscard(fallbackValue, actual.currentContext());
}
}

actual.onNext(t);
}

@Override
public void onComplete() {
if (hasValue) {
actual.onComplete();
} else {
complete(this.value);
if (done) {
return;
}

done = true;

if (!hasValue) {
completeWhenEmpty();

return;
}

actual.onComplete();
}

@Override
public void setValue(T value) {
// value is constant. writes from the base class are redundant, and the constant
// would always be visible in cancel(), so it will safely be discarded.
public void onError(Throwable t) {
if (done) {
return;
}

done = true;
if (!hasValue) {
final T fallbackValue = this.fallbackValue;
if (fallbackValue != null && FALLBACK_VALUE.compareAndSet(this, fallbackValue, null)) {
Operators.onDiscard(t, actual.currentContext());
}
}

actual.onError(t);
}

@Override
public int requestFusion(int requestedMode) {
return Fuseable.NONE; // prevent fusion because of the upstream
T resolveValue() {
final T fallbackValue = this.fallbackValue;
if (fallbackValue != null && FALLBACK_VALUE.compareAndSet(this, fallbackValue, null)) {
return fallbackValue;
}
return null;
}
}
}