-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: replace concatMap to flatMapSequential to improve parallelism a…
…nd efficiency (#6706) #### What type of PR is this? /kind improvement /area core /milestone 2.20.x #### What this PR does / why we need it: 将 concatMap 替换为 flatMapSequential 以提高并行度和执行效率 可以看一下这个场景示例来模拟像文章列表 API 的数据组装 假如每个步骤的执行时间是 1s 有 4 个步骤 同时 Flux 发出 4 条数据: ```java @test void test() { var startMs = System.currentTimeMillis(); var monoA = Mono.fromSupplier( () -> { sleep(); return "A"; }) .subscribeOn(Schedulers.boundedElastic()); var monoB = Mono.fromSupplier( () -> { sleep(); return "B"; }) .subscribeOn(Schedulers.boundedElastic()); var monoC = Mono.fromSupplier( () -> { sleep(); return "C"; }) .subscribeOn(Schedulers.boundedElastic()); var monoD = Mono.fromSupplier( () -> { sleep(); return "D"; }) .subscribeOn(Schedulers.boundedElastic()); var convert = Mono.when(monoA, monoB, monoC, monoD); Flux.just("1", "2", "3", "4") // concatMap(convert::thenReturn) .flatMapSequential(convert::thenReturn) .collectList() .block(); System.out.println("Time: " + (System.currentTimeMillis() - startMs)); } private static void sleep() { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } ``` **结果:** 1. 如果每个步骤没有加 subscribeOn 且使用 concatMap 耗时: 16362 ms 2. 每个步骤使用 subscribeOn 且使用 concatMap 耗时: 4174 ms 3. 每个步骤使用 subscribeOn 且使用 flatMapSequential 耗时: 1185 ms #### Does this PR introduce a user-facing change? ```release-note 提升页面访问速度 ```
- Loading branch information
Showing
12 changed files
with
17 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
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
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