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

perf: replace concatMap to flatMapSequential to improve parallelism and efficiency #6706

Merged
merged 3 commits into from
Oct 10, 2024

Conversation

guqing
Copy link
Member

@guqing guqing commented Sep 26, 2024

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 条数据:

@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?

提升页面访问速度

@f2c-ci-robot f2c-ci-robot bot added kind/improvement Categorizes issue or PR as related to a improvement. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Sep 26, 2024
@f2c-ci-robot f2c-ci-robot bot added this to the 2.20.x milestone Sep 26, 2024
@f2c-ci-robot f2c-ci-robot bot added the area/core Issues or PRs related to the Halo Core label Sep 26, 2024
Copy link

codecov bot commented Sep 26, 2024

Codecov Report

Attention: Patch coverage is 35.29412% with 11 lines in your changes missing coverage. Please review.

Project coverage is 56.99%. Comparing base (c945a94) to head (e1d6c1c).
Report is 96 commits behind head on main.

Files with missing lines Patch % Lines
...ava/run/halo/app/content/impl/PostServiceImpl.java 0.00% 2 Missing ⚠️
...in/java/run/halo/app/plugin/PluginServiceImpl.java 0.00% 2 Missing ⚠️
...run/halo/app/content/comment/ReplyServiceImpl.java 0.00% 1 Missing ⚠️
...n/halo/app/content/impl/SinglePageServiceImpl.java 0.00% 1 Missing ⚠️
...me/finders/impl/CommentPublicQueryServiceImpl.java 66.66% 1 Missing ⚠️
.../app/theme/finders/impl/ContributorFinderImpl.java 0.00% 1 Missing ⚠️
...un/halo/app/theme/finders/impl/PostFinderImpl.java 0.00% 1 Missing ⚠️
.../finders/impl/SinglePageConversionServiceImpl.java 0.00% 1 Missing ⚠️
...run/halo/app/theme/finders/impl/TagFinderImpl.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #6706      +/-   ##
============================================
- Coverage     58.17%   56.99%   -1.18%     
- Complexity     3947     3978      +31     
============================================
  Files           680      713      +33     
  Lines         23262    24009     +747     
  Branches       1582     1572      -10     
============================================
+ Hits          13532    13685     +153     
- Misses         9106     9712     +606     
+ Partials        624      612      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@guqing guqing requested a review from JohnNiang October 10, 2024 09:37
Copy link
Member

@JohnNiang JohnNiang left a comment

Choose a reason for hiding this comment

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

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Oct 10, 2024
Copy link

f2c-ci-robot bot commented Oct 10, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JohnNiang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 10, 2024
Copy link

sonarcloud bot commented Oct 10, 2024

@f2c-ci-robot f2c-ci-robot bot merged commit 25c54d7 into halo-dev:main Oct 10, 2024
8 checks passed
@ruibaby ruibaby modified the milestones: 2.20.x, 2.20.0 LTS Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/improvement Categorizes issue or PR as related to a improvement. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants