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: optimize canvas renderer performance #1810

Merged
merged 10 commits into from
Nov 4, 2024
Merged

perf: optimize canvas renderer performance #1810

merged 10 commits into from
Nov 4, 2024

Conversation

wang1212
Copy link
Member

@wang1212 wang1212 commented Oct 31, 2024

🤔 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

see #1712

💡 Background and solution

Currently, there is not much optimization in the core process of G's canvas renderer, resulting in poor performance under large data volumes. The following strategies can be introduced to optimize performance:

  • The forEach recursive call loop in the core code is refactored into a while loop to avoid stack overflow errors
  • Avoid repeated transformation matrix calculations and canvas context matrix updates during rendering
  • Avoid frequent save() and restore() calls to the canvas context state during the rendering process
  • Support batch rendering

Since changes in the core program flow may cause unexpected results, the performance optimization mode can be enabled through a flag in the early stage to avoid affecting existing products.

const canvasRenderer = new CanvasRenderer({
   enableRenderingOptimization: true,
});

📝 Changelog

Language Changelog
🇺🇸 English perf: optimize canvas renderer performance
🇨🇳 Chinese perf: 优化 canvas 渲染器性能

☑️ 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

Base automatically changed from release to next November 1, 2024 05:12
__tests__/demos/perf/attr-update.ts Dismissed Show dismissed Hide dismissed
@wang1212 wang1212 force-pushed the perf/canvas branch 4 times, most recently from 06af76e to 7167936 Compare November 1, 2024 07:03
@wang1212 wang1212 marked this pull request as ready for review November 4, 2024 03:47
@hustcc
Copy link
Member

hustcc commented Nov 4, 2024

这个开启后有多少的优化效果?

@wang1212
Copy link
Member Author

wang1212 commented Nov 4, 2024

这个开启后有多少的优化效果?

在我的设备上测试:

  • case1:1w 随机移动的矩形 fps 60 -> 95
  • case2:2w 随机移动的矩形 fps 30 -> 45

发个版本后可以先验证下 s2 和 g6 的效果。


目前从性能数据来看,每一帧的耗时 30% 分布在属性解析和事件分发,70% 分布在元素渲染流程上。目前主要优化的是渲染流程,开启优化策略后该阶段耗时可以降低 50%~70%

@@ -220,52 +223,62 @@ export class RenderingService {
canvasConfig: Partial<CanvasConfig>,
renderingContext: RenderingContext,
) {
const self = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

这个目的是?

Copy link
Member Author

Choose a reason for hiding this comment

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

内部方法需要

// console.log('canvas renderer fcp...', renderingContext.root.childNodes);
renderByZIndex(renderingContext.root, context);
// console.time('renderByZIndex');
if (enableRenderingOptimization) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这里啥情况

Copy link
Member Author

@wang1212 wang1212 Nov 4, 2024

Choose a reason for hiding this comment

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

优化不是默认开启的

if (enableRenderingOptimization) {
  context.save();
  renderByZIndex(renderingContext.root, context);
  context.restore();
} else {
  renderByZIndex(renderingContext.root, context);
}

@wang1212 wang1212 merged commit a3e07c1 into next Nov 4, 2024
5 checks passed
@wang1212 wang1212 deleted the perf/canvas branch November 4, 2024 09:15
wang1212 added a commit that referenced this pull request Nov 4, 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.

3 participants