Skip to content

perf: less render in dev - #53844

Merged
zombieJ merged 4 commits into
nextfrom
perf
May 16, 2025
Merged

zombieJ merged 4 commits into
nextfrom
perf

Conversation

@zombieJ

@zombieJ zombieJ commented May 15, 2025 •

Copy link
Copy Markdown
Member

中文版模板 / Chinese template

🤔 This is a ...

  • 🆕 New feature
  • 🐞 Bug fix
  • 📝 Site / documentation improvement
  • 📽️ Demo improvement
  • 💄 Component style improvement
  • 🤖 TypeScript definition improvement
  • 📦 Bundle size optimization
  • ⚡️ Performance optimization
  • ⭐️ Feature enhancement
  • 🌐 Internationalization
  • 🛠 Refactoring
  • 🎨 Code style optimization
  • ✅ Test Case
  • 🔀 Branch merge
  • ⏩ Workflow
  • ⌨️ Accessibility improvement
  • ❓ Other (about what?)

🔗 Related Issues

ref #50443

💡 Background and Solution

Dev 模式下,React 会对 createElement 和 cloneElement 注入额外的代码:

Dev

function ReactElement(
  type,
  key,
  self,
  source,
  owner,
  props,
  debugStack,
  debugTask
) {
  self = props.ref;
  type = {
    $$typeof: REACT_ELEMENT_TYPE,
    type: type,
    key: key,
    props: props,
    _owner: owner
  };
  null !== (void 0 !== self ? self : null)
    ? Object.defineProperty(type, "ref", {
        enumerable: !1,
        get: elementRefGetterWithDeprecationWarning
      })
    : Object.defineProperty(type, "ref", { enumerable: !1, value: null });
  type._store = {};
  Object.defineProperty(type._store, "validated", {
    configurable: !1,
    enumerable: !1,
    writable: !0,
    value: 0
  });
  Object.defineProperty(type, "_debugInfo", {
    configurable: !1,
    enumerable: !1,
    writable: !0,
    value: null
  });
  Object.defineProperty(type, "_debugStack", {
    configurable: !1,
    enumerable: !1,
    writable: !0,
    value: debugStack
  });
  Object.defineProperty(type, "_debugTask", {
    configurable: !1,
    enumerable: !1,
    writable: !0,
    value: debugTask
  });
  Object.freeze && (Object.freeze(type.props), Object.freeze(type));
  return type;
}

Prod

function ReactElement(type, key, self, source, owner, props) {
  self = props.ref;
  return {
    $$typeof: REACT_ELEMENT_TYPE,
    type: type,
    key: key,
    ref: void 0 !== self ? self : null,
    props: props
  };
}

所以在 dev 模式下,Tooltip 的嵌套 createElement 与 cloneElement 会产生大量的 Object defineProperty 操作。这会导致浏览器对 Object 对象进行降级,从而影响渲染速度。提升后的 @rc-component/trigger 在渲染时会少渲染 2 个 createElement 和 1 个 cloneElement。加上之前的 babel target 升级能进一步提升开发体验:

裸渲染 10000 条 Tooltips 效果

const App: React.FC = () => {
  const [show, setShow] = React.useState(false);

  React.useLayoutEffect(() => {
    if (show) {
      console.timeEnd('render');
    }
  }, [show]);

  return (
    <>
      <button
        onClick={() => {
          console.time('render');
          setShow(true);
        }}
      >
        Click
      </button>
      {show &&
        Array.from({ length: 10000 }).map((_, index) => (
          <Tooltip key={index} title="prompt text">
            <span>Tooltip </span>
          </Tooltip>
        ))}
    </>
  );
};
版本 渲染时间 提升
v5 ~4401ms 0%
v6 babel target ~3520ms 20%
v6 babel target + less element ~2647ms 39.8%
  • MAC M1 16GB

📝 Change Log

Language Changelog
🇺🇸 English Optimize Tooltip dev render performance(~40%) to enhance developer experience.
🇨🇳 Chinese 优化 Tooltip 开发模式下性能(约 ~40%)以提升研发体验。

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@coderabbitai

coderabbitai Bot commented May 15, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

@github-actions

github-actions Bot commented May 15, 2025 •

Copy link
Copy Markdown
Contributor

👁 Visual Regression Report for PR #53844 Passed ✅

🎯 Target branch: next (29f35b6)
📖 View Full Report ↗︎
📖 Alternative Report ↗︎

🎊 Congrats! No visual-regression diff found.

@petercat-assistant

Copy link
Copy Markdown

Walkthrough

This pull request focuses on optimizing the performance of the Tooltip component in development mode by reducing unnecessary renders. It achieves this by upgrading the @rc-component/trigger package to minimize createElement and cloneElement operations, which are costly in development mode due to additional Object.defineProperty operations.

Changes

Files Summary
package.json Upgraded @rc-component/trigger from version ~3.1.0 to ^3.4.0 to improve Tooltip rendering performance in development mode.

@github-actions

github-actions Bot commented May 15, 2025 •

Copy link
Copy Markdown
Contributor

Preview is ready

Comment thread package.json Resolved
@socket-security

socket-security Bot commented May 15, 2025 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License

View full report

@github-actions

github-actions Bot commented May 15, 2025 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
dist/antd.min.js 497.57 KB (-22.91 KB 🔽)
dist/antd-with-locales.min.js 590.11 KB (-22.23 KB 🔽)

@pkg-pr-new

pkg-pr-new Bot commented May 15, 2025 •

Copy link
Copy Markdown

@codecov

codecov Bot commented May 15, 2025 •

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 7.76MB (206.98%) ⬆️⚠️, exceeding the configured threshold of 5%.

Bundle name Size Change
antd.min-array-push 11.51MB 7.76MB (206.98%) ⬆️⚠️

Affected Assets, Files, and Routes:

view changes for bundle: antd.min-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
antd-with-locales.js (New) 6.06MB 6.06MB 100.0% 🚀
antd.js (New) 5.45MB 5.45MB 100.0% 🚀
antd-with-locales.min.js (Deleted) -2.06MB 0 bytes -100.0% 🗑️
antd.min.js (Deleted) -1.69MB 0 bytes -100.0% 🗑️

Comment thread package.json
"@rc-component/tree": "~1.0.1",
"@rc-component/tree-select": "~1.1.2",
"@rc-component/trigger": "~3.1.0",
"@rc-component/trigger": "^3.4.0",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

改 ^ 了么?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

以前就是 ^,上一个 contributor 不小心把 ^ 改成 ~ 提交了:

"@rc-component/trigger": "^2.2.6",

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 15, 2025 •

Copy link
Copy Markdown

Deploying ant-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: 61e440b
Status: ✅  Deploy successful!
Preview URL: https://fe753e76.ant-design.pages.dev
Branch Preview URL: https://perf.ant-design.pages.dev

View logs

MadCcc
MadCcc previously approved these changes May 15, 2025
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 15, 2025
afc163
afc163 previously approved these changes May 15, 2025
@afc163

afc163 commented May 15, 2025

Copy link
Copy Markdown
Member

提升效果写到 changelog 里吧。

@zombieJ
zombieJ dismissed stale reviews from afc163 and MadCcc via 61e440b May 16, 2025 03:40
@codecov

codecov Bot commented May 16, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (29f35b6) to head (61e440b).
Report is 1 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff            @@
##              next    #53844   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          787       787           
  Lines        14291     14291           
  Branches      3780      3780           
=========================================
  Hits         14291     14291           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zombieJ
zombieJ merged commit c4a33b6 into next May 16, 2025
@zombieJ
zombieJ deleted the perf branch May 16, 2025 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants