Skip to content

perf(tree-shaking): parallelize tree shaking module map init#1452

Merged
sorrycc merged 4 commits intomasterfrom
ref/extract_shake_fn
Jul 28, 2024
Merged

perf(tree-shaking): parallelize tree shaking module map init#1452
sorrycc merged 4 commits intomasterfrom
ref/extract_shake_fn

Conversation

@stormslowly
Copy link
Contributor

@stormslowly stormslowly commented Jul 26, 2024

performance: with-antd

(disable skipModules and concatenateModules )

branch:  190.0ms
master:  265.1ms 

Summary by CodeRabbit

  • 新特性

    • 引入了并行处理的树摇逻辑,提升模块优化性能。
    • 新增 shake_module 函数,提高代码可读性和可维护性。
  • bug 修复

    • 简化了 TreeShakeModule 的实例化过程,消除了不必要的参数和导入。

@coderabbitai
Copy link

coderabbitai bot commented Jul 26, 2024

Walkthrough

此次更改主要集中在 TreeShakeModule 的构造函数和模块优化逻辑的改进上。new 函数的参数简化,使得模块实例化过程更加清晰。同时,引入 rayon 库以实现并行处理,提高了模块优化的性能和可维护性。这些调整旨在增强代码的可读性和整体效率。

Changes

文件 更改摘要
crates/mako/src/plugins/tree_shaking/module.rs TreeShakeModulenew 函数参数从 &ModuleGraph 简化为无,影响模块实例化流程。
crates/mako/src/plugins/tree_shaking/shake.rs optimize_modules 函数逻辑显著修改,引入 rayon 进行并行处理,新增 shake_module 函数以提升可读性。
crates/mako/src/plugins/tree_shaking/shake/find_export_source.rs 测试模块中移除了 Deref 引用,简化了对 TreeShakeModule::new 的调用,提升了代码清晰度。

Poem

在树间摇曳,兔子轻声吟,
模块简化,如春风拂面。
并行处理,效率飞升,
代码更清晰,欢乐无边。
新功能到来,欢呼共庆,
跳跃的兔子,心中有梦! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@stormslowly stormslowly changed the title perf(tree-shaking): parallelize tree shaking module map init wip perf(tree-shaking): parallelize tree shaking module map init Jul 26, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 94d15d4 and 6628fc2.

Files selected for processing (3)
  • crates/mako/src/plugins/tree_shaking/module.rs (2 hunks)
  • crates/mako/src/plugins/tree_shaking/shake.rs (4 hunks)
  • crates/mako/src/plugins/tree_shaking/shake/find_export_source.rs (3 hunks)
Additional comments not posted (7)
crates/mako/src/plugins/tree_shaking/shake/find_export_source.rs (2)

482-483: 移除 deref() 调用

移除 deref() 调用简化了代码,且不影响功能。


458-458: 变量 _module_graph 可能未使用

module_graph 重命名为 _module_graph 表明它可能未使用。确保该变量确实不需要。

Verification successful

变量 _module_graph 被多处使用

搜索结果表明,变量 _module_graph 在多个上下文中被使用,包括函数参数和函数体内。这表明该变量不是未使用的变量。将其重命名为 _module_graph 可能是为了表示它在特定函数体内未使用,但作为参数仍然是必要的。

  • crates/mako/src/plugin.rs
  • crates/mako/src/plugins/tree_shaking/shake/find_export_source.rs
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if `_module_graph` is unused in the codebase.

# Test: Search for the variable usage. Expect: No usage found.
rg --type rust '_module_graph'

Length of output: 1242


Script:

#!/bin/bash
# Description: Verify the context where `_module_graph` is used in the codebase.

# Test: Search for the context of `_module_graph` usage.
rg --type rust -A 5 '_module_graph'

Length of output: 5281

crates/mako/src/plugins/tree_shaking/module.rs (1)

280-280: 函数签名已简化

new 函数签名已简化,移除了 _module_graph 参数。确保此更改不会影响代码库中的其他部分。

Verification successful

函数签名已简化

new 函数签名已简化,移除了 _module_graph 参数。确保此更改不会影响代码库中的其他部分。

  • crates/mako/src/plugins/tree_shaking/shake.rs
  • crates/mako/src/plugins/tree_shaking/shake/find_export_source.rs
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `TreeShakeModule::new` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type rust 'TreeShakeModule::new'

Length of output: 250

crates/mako/src/plugins/tree_shaking/shake.rs (4)

11-11: 引入 rayon

引入 rayon 库以实现并行处理,提升了性能。


32-57: 模块分区逻辑

使用分区策略直接过滤模块,减少了不必要的迭代,提高了性能。


60-72: 并行处理模块

使用 par_iter 方法并行处理模块,提升了性能。


470-585: 新函数 shake_module

新函数 shake_module 提高了代码的可读性和可维护性。

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6628fc2 and 30197f7.

Files selected for processing (1)
  • crates/mako/src/plugins/tree_shaking/shake.rs (4 hunks)
Additional comments not posted (11)
crates/mako/src/plugins/tree_shaking/shake.rs (11)

11-11: 引入 rayon

引入 rayon 库用于并行处理,提高了性能。


24-25: 定义 TreeShakingModuleMap 类型

定义了一个新的类型 TreeShakingModuleMap,用于存储模块的引用。


32-57: 模块分区逻辑

模块分区逻辑将模块分为需要跳过的模块和需要处理的模块。逻辑看起来正确,但需要确保所有模块类型和外部模块都被正确处理。

请确保所有模块类型和外部模块都被正确处理。


60-72: 并行处理模块

使用 rayonpar_iter 方法并行处理模块,创建 TreeShakeModule 实例。逻辑看起来正确,但需要确保并行处理不会引入数据竞争。

请确保并行处理不会引入数据竞争。


132-137: 调用 shake_module 函数

在一个循环中调用 shake_module 函数处理每个模块。逻辑看起来正确,但需要确保 shake_module 函数的实现是正确的。

请确保 shake_module 函数的实现是正确的。


470-495: 模块系统检查逻辑

检查模块是否是 ESModule,如果不是则标记所有导入模块为 UsedExports::All。逻辑看起来正确,但需要确保所有模块类型都被正确处理。

请确保所有模块类型都被正确处理。


498-501: 未使用模块检查

如果模块未被使用,则返回下一个索引。逻辑看起来正确,但需要确保未使用模块的判断是正确的。

请确保未使用模块的判断是正确的。


503-551: 更新导入和导出的使用信息

更新导入和导出的使用信息,移除无用的语句和标识符。逻辑看起来正确,但需要确保所有导入和导出的使用信息都被正确更新。

请确保所有导入和导出的使用信息都被正确更新。


554-582: 处理动态导入和其他依赖

处理动态导入和其他依赖,标记所有动态导入的依赖为 UsedExports::All。逻辑看起来正确,但需要确保所有动态导入和其他依赖都被正确处理。

请确保所有动态导入和其他依赖都被正确处理。


Line range hint 104-133:
导入模块类型检查逻辑

检查导入模块的类型,确保模块不是占位符模块。逻辑看起来正确,但需要确保所有导入模块类型都被正确处理。

请确保所有导入模块类型都被正确处理。


Line range hint 135-175:
使用的导出更新逻辑

更新使用的导出,根据导入说明符更新使用的导出。逻辑看起来正确,但需要确保所有导入说明符都被正确处理。

请确保所有导入说明符都被正确处理。

@stormslowly stormslowly changed the title wip perf(tree-shaking): parallelize tree shaking module map init perf(tree-shaking): parallelize tree shaking module map init Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants