Skip to content

Conversation

@Alexzjt
Copy link
Contributor

@Alexzjt Alexzjt commented Sep 8, 2025

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / Document optimization
  • TypeScript definition update
  • Refactoring
  • Performance improvement
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

💡 Background and solution

在S2百万数据交叉表滚动场景下(1649个元素),S2花费了巨量的时间在创建、计算和渲染单元格上,伴随着大量单元格、line、rect、text等Destroy与new。大量的对象在短时间内被创建然后又被废弃,会导致频繁的垃圾回收和较高的性能开销。所以有必要采取复用已有DisplayObject这样性能友好的写法来提高性能。在复用DisplayObject过程中,就会有大量的对象属性更改的操作。如果以当前版本的G的API为准,写法如下:
假设某obj有10个属性需要更改,则需要遍历10次setAttribute走同样的解析流程,在对象数量较多的情况下,每一帧都要做不必要的重复解析。

export function batchSetStyle<
  T extends DisplayObject,
  S extends BaseStyleProps,
>(obj: T, style: S) {
  for (const styleKey in style) {
    obj.style[styleKey] = style[styleKey];
  }
}

引入setAttributes后,一个对象的属性只需要一次就修改好。

export function batchSetStyle<
  T extends DisplayObject,
  S extends BaseStyleProps,
>(obj: T, style: S) {
  obj.setAttributes(style, {skipDispatchAttrModifiedEvent: true});
}

引入了skipDispatchAttrModifiedEvent,可以看到去掉更新事件的触发之后,本次更改可以带来 > 4%的性能提升

Before After
image image

📝 Changelog

Language Changelog
🇺🇸 English batch update attributes without attributeChangedCallback, for performance
🇨🇳 Chinese 批量更新DisplayObject属性

☑️ Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@changeset-bot
Copy link

changeset-bot bot commented Sep 8, 2025

🦋 Changeset detected

Latest commit: 3aaa946

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 29 packages
Name Type
@antv/g-lite Minor
@antv/g-canvas Patch
@antv/g-canvaskit Patch
@antv/g-lottie-player Patch
@antv/g-mobile-canvas Patch
@antv/g-mobile-svg Patch
@antv/g-mobile-webgl Patch
@antv/g-plugin-3d Patch
@antv/g-plugin-a11y Patch
@antv/g-plugin-annotation Patch
@antv/g-plugin-box2d Patch
@antv/g-plugin-control Patch
@antv/g-plugin-css-select Patch
@antv/g-plugin-device-renderer Patch
@antv/g-plugin-dragndrop Patch
@antv/g-plugin-gesture Patch
@antv/g-plugin-matterjs Patch
@antv/g-plugin-physx Patch
@antv/g-plugin-rough-canvas-renderer Patch
@antv/g-plugin-rough-svg-renderer Patch
@antv/g-plugin-yoga Patch
@antv/g-plugin-zdog-canvas-renderer Patch
@antv/g-plugin-zdog-svg-renderer Patch
@antv/g-svg Patch
@antv/g-web-components Patch
@antv/g-webgl Patch
@antv/g-webgpu Patch
@antv/g Patch
@antv/react-g Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @Alexzjt, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on a critical performance enhancement by implementing a mechanism for batch updating attributes of display objects. The primary goal is to minimize the overhead associated with individual attribute modifications, leading to improved rendering efficiency and reduced garbage collection frequency in environments with high data volumes or dynamic rendering requirements.

Highlights

  • New setAttributes Method: Introduced a new setAttributes method to the DisplayObject class, allowing for the batch updating of multiple attributes simultaneously.
  • Performance Optimization: This new method significantly improves performance by avoiding redundant parsing and attributeChangedCallback calls when numerous attributes of a DisplayObject are updated at once. This is particularly beneficial in scenarios involving frequent changes to many objects, such as large-scale data visualizations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

这个 PR 为 DisplayObject 引入了一个新的 setAttributes 方法,用于批量更新属性,旨在通过绕过单独的 setAttribute 调用及其相关的开销(如事件派发)来提高性能。实现方式直接明了,符合其性能优化的目标。我的审查包含一个建议,以确保 API 在处理 undefined 值时与 setAttribute 保持一致性。

@wang1212
Copy link
Member

wang1212 commented Sep 8, 2025

setAttributessetAttribute 逻辑不一致,缺少 dirty() 后续的逻辑处理。

# Conflicts:
#	packages/g-lite/src/display-objects/DisplayObject.ts
@wang1212 wang1212 changed the base branch from master to release December 24, 2025 11:10
@wang1212
Copy link
Member

先作为实验性 api 添加,合并 pr,后续寻找更优方案,从根本上解决性能问题

@wang1212 wang1212 merged commit 8df267d into release Dec 24, 2025
2 checks passed
@wang1212 wang1212 deleted the perf/setAttributes branch December 24, 2025 11:39
@wang1212 wang1212 restored the perf/setAttributes branch December 24, 2025 11:53
wang1212 added a commit that referenced this pull request Dec 24, 2025
* chore: fix docs dependency version exception

* Remove rbush dependency (#2113)

* refactor: remove rbush dependency

Removes the `rbush` dependency and all related logic from the codebase.

This includes:
- Deleting the `RBushNode` component.
- Removing `rbush` logic from `CanvasRendererPlugin` and `SelectablePlugin`.
- Updating documentation to remove mentions of `rbush`.

* fix(lint): fix linting errors

Removes unused imports from `CanvasRendererPlugin.ts` and fixes a Prettier error.

* chore: fix lint error

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: wang1212 <mrwang1212@126.com>

* chore: add changeset

* perf: batch update attributes (#2001)

* perf: batch update attributes

* perf: batch update attributes

* 0.0.1-0

* chore: add changeset

* chore: revert change

---------

Co-authored-by: yunchen.wy <yunchen.wy@antgroup.com>
Co-authored-by: wang1212 <mrwang1212@126.com>

* chore(release): bump version (#2114)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: huiyu.zjt <Alexzjt@users.noreply.github.com>
Co-authored-by: yunchen.wy <yunchen.wy@antgroup.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@wang1212 wang1212 deleted the perf/setAttributes branch December 24, 2025 11:58
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.

3 participants