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

feat(tree-shaking): optimize import namespace used all exports to partial used of source modules #1584

Merged
merged 7 commits into from
Sep 13, 2024

Conversation

stormslowly
Copy link
Member

@stormslowly stormslowly commented Sep 12, 2024

problem

// index.js
import * as math from "./math.js"
math.square(2)
// math.js
export function square(){}
export function cube(){}

after building, both square and cube are kept in dist js, but cube is not necessary.

in this case, we can figure out that only square is used, so we can optimize that math.js 's used exports from ALL_USED toPartial(['square'])

how to

if all accesses to the namespace are "explicit", we can say the namespace used and only used these exports of the source module.

"explicit" is define as these two types

  • namespace.foo
  • namespace['foo']

if there is an no-explicit access in code, we will stop thes optimize; eg.

console.log(namespace); // this implies that all the exports of the namespace will be used.

determining if all accesses to the namespace are "explicit" is easy.
in intuition, the count of namespace appears in explicit member expr should equal with the count of namespace ident appears; but don't forget the import * as namespace
so if count(namespace ident appears) - count(namespace_member_access ) == 1 we can optimize the namespace usage .

Summary by CodeRabbit

  • 新功能

    • 引入了新的 optimize_import_namespace 功能,优化命名空间导入处理。
    • 新增用于验证树摇功能的测试文件,确保构建过程的输出符合预期。
    • 添加了 needKeepneedNotKeep 函数,提供基本数学运算功能。
    • 新增配置文件以优化模块处理,确保构建过程不合并多个模块。
    • 改进了模块摇动过程中的助手函数管理,增强了优化流程的结构化。
  • 修复

    • 移除了 TransformOutput 接口,简化了输出转换的处理方式。

Copy link
Contributor

coderabbitai bot commented Sep 12, 2024

Walkthrough

此次更改引入了一个新的函数 optimize_import_namespace,旨在增强 remove_useless_stmts 函数中的导入命名空间处理。该功能通过分析 import_infos 来识别和优化命名空间导入,基于显式属性访问进行调整。此外,更新了 shake.rs 文件中的 optimize_modules 函数,以改善模块摇动过程中的助手函数管理,整体逻辑流得到了优化。

Changes

文件路径 更改摘要
crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs 新增了 optimize_import_namespace 函数,优化导入命名空间的处理,利用显式属性访问收集器。
crates/mako/src/plugins/tree_shaking/shake.rs 修改了 optimize_modules 函数,改进了 shake_module 函数的调用上下文,增强了助手函数的管理。
e2e/fixtures/tree-shaking.import_star/expect.js 新增了用于验证树摇功能输出的断言集。
e2e/fixtures/tree-shaking.import_star/src/index.tsx 新增了 index.tsx 文件,演示 needKeepshouldKeep 函数的使用。
e2e/fixtures/tree-shaking.import_star/src/mod2.ts 新增了两个导出函数 needKeepneedNotKeep,实现基本的数学运算。
packages/mako/binding.d.ts 移除了 TransformOutput 接口,简化了输出转换的处理方式。
e2e/fixtures/tree-shaking.import_namespace/expect.js 新增了用于验证树摇功能输出的断言,确保 shouldKeep1shouldKeep2 存在,而 shouldNotKeep 不存在。
e2e/fixtures/tree-shaking.import_namespace/mako.config.json 新增了 mako.config.json 文件,配置了模块连接的优化设置,明确指定不合并多个模块。
e2e/fixtures/tree-shaking.import_namespace/src/index.tsx 新增了 index.tsx 文件,使用 mod 模块中的 shouldKeep1shouldKeep2 函数。
e2e/fixtures/tree-shaking.import_namespace/src/mod.js 新增了三个导出函数 shouldKeep1shouldKeep2shouldNotKeep,实现基本的数学运算,演示树摇机制的不同处理。

Possibly related PRs

Poem

🐰 在树摇中跳跃,
显式属性收集,
优化导入如风,
代码清晰又明亮。
兔子欢呼雀跃,
新功能真不错!
🌟

Tip

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8ef3d2d and ab6a1fa.

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

14-14: LGTM!

新增的导入语句是有效且必要的,为 optimize_modules 函数中的更改提供了所需的依赖。


138-155: 代码更改看起来不错!

将对 shake_module 的调用包装在由 HELPERS.set 提供的新上下文中,有助于在模块 tree-shaking 过程中更好地管理辅助函数或应用于模块的转换。虽然整体控制流保持相似,但引入 HELPERS.set 表明在优化过程中管理依赖项或转换方面向更结构化的方法转变。这种改进潜在地允许:

  1. 更细粒度地控制辅助函数的范围和生命周期。
  2. 在模块层面上应用特定的转换或优化。
  3. 更容易维护和推理优化过程中的不同阶段和上下文。

总的来说,这一变化朝着更模块化、可维护的 tree-shaking 实现迈进了一步。


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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

Copy link

codecov bot commented Sep 12, 2024

Codecov Report

Attention: Patch coverage is 80.89172% with 30 lines in your changes missing coverage. Please review.

Project coverage is 62.24%. Comparing base (4897e32) to head (ab6a1fa).
Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
...o/src/plugins/tree_shaking/remove_useless_stmts.rs 38.09% 26 Missing ⚠️
.../src/plugins/tree_shaking/collect_explicit_prop.rs 96.22% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1584      +/-   ##
==========================================
+ Coverage   62.03%   62.24%   +0.20%     
==========================================
  Files         127      128       +1     
  Lines       15353    15512     +159     
==========================================
+ Hits         9524     9655     +131     
- Misses       5829     5857      +28     

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

Copy link
Contributor

@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

Outside diff range and nitpick comments (1)
e2e/fixtures/tree-shaking.import_star/src/index.tsx (1)

1-8: 代码段看起来不错,正确演示了命名空间导入和访问特定导出的用法。

这段代码通过使用命名空间导入 mathmath2 模块,并访问它们的特定导出,如 needKeepshouldKeep,来验证树摇优化的行为。

建议添加注释来说明这段代码的目的,即作为树摇优化的测试用例。这将有助于其他开发人员理解代码的意图。

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a615367 and 094395a.

Files selected for processing (5)
  • crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs (3 hunks)
  • e2e/fixtures/tree-shaking.import_star/expect.js (1 hunks)
  • e2e/fixtures/tree-shaking.import_star/src/index.tsx (1 hunks)
  • e2e/fixtures/tree-shaking.import_star/src/mod2.ts (1 hunks)
  • packages/mako/binding.d.ts (0 hunks)
Files not reviewed due to no reviewable changes (1)
  • packages/mako/binding.d.ts
Files skipped from review as they are similar to previous changes (1)
  • e2e/fixtures/tree-shaking.import_star/expect.js
Additional comments not posted (3)
e2e/fixtures/tree-shaking.import_star/src/mod2.ts (2)

1-3: 没有问题!

该函数的逻辑正确,实现准确无误。


5-7: 看起来不错!

函数的实现是正确的,没有发现任何问题。

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

240-300: 代码看起来不错!这个优化可以提高模块导入处理的效率。

新增的 optimize_import_namespace 函数通过分析显式属性访问来识别和优化命名空间导入。它有效地将命名空间导入替换为仅包含已访问属性的命名导入,从而减少了不必要的导入。该函数的逻辑流程结构清晰,易于理解,并且很好地集成到现有的 remove_useless_stmts 函数中。

@Jinbao1001
Copy link
Member

+1

@stormslowly stormslowly merged commit 81a52f8 into master Sep 13, 2024
21 checks passed
@stormslowly stormslowly deleted the optimize/import_star_as branch September 13, 2024 03:11
stormslowly added a commit that referenced this pull request Sep 22, 2024
…s to partial used of source modules (#1584)"

This reverts commit 81a52f8.
stormslowly added a commit that referenced this pull request Sep 23, 2024
* Revert "fix(tree-shaking): object spread transform should go before deconstructing (#1598)"

This reverts commit 9434d99.

* Revert "feat(tree-shaking): optimize import namespace used all exports to partial used of source modules (#1584)"

This reverts commit 81a52f8.
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