-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make some Mono sources and aggregators lazier (#3081)
This commit changes 4 broad categories of Mono operators to be lazier and only start requesting from their upstream once they've been themselves requested. Previously, the request to upstream would happen immediately upon receiving the Subscription (in `onSubscribe`). The first category of Monos is the one where the upstream is a Flux that gets aggregated into a Mono, such as `MonoCollectList`, `MonoCount` but also `MonoAll` or `MonoElementAt`... These Mono now all inherit a common `BaseFluxToMonoOperator` which track two states as bits in a single volatile: request status (true/false) and completed status (true/false). It uses a non-volatile boolean hasRequest as a shortcut to ignore any request past the first one. Note that for now these implementations do declare a `Fuseable` trait, but they always negotiate fusion `NONE`. It is kept as a future possible improvement that they would all support SYNC fusion. The second category is Monos that combine multiple sources, like `MonoZip` or `MonoWhen`. A single volatile long-based state machine is used to coordinate the multiple inners and track which inner has made the first request. The third category is ParallelFlux-to-Mono operators, mixing the above two aspects: aggregating values and combining multiple sources (rails). Some of these can benefit from the `BaseFluxToMonoOperator` approach. The last category is sources like `MonoSupplier` or `MonoCallable`. Often the functional interface representing the logic would be run in `onSubscribe` directly, but making it lazier implies the creation of dedicated `Subscription` implementations now. Fixes #2913. Signed-off-by: Oleh Dokuka <odokuka@vmware.com> Co-authored-by: Simon Baslé <sbasle@vmware.com>
- Loading branch information
1 parent
42712b1
commit 88587fc
Showing
61 changed files
with
2,741 additions
and
1,452 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
benchmarks/src/main/java/reactor/core/publisher/MonoAllBenchmark.java
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
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(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
benchmarks/src/main/java/reactor/core/publisher/MonoCallableBenchmark.java
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
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(); | ||
} | ||
} |
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 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
Oops, something went wrong.