-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize canvas renderer performance (#1810)
* perf: optimize canvas renderer performance * perf: avoid unnecessary transformation matrix calculations and canvas context matrix updates * perf: avoid frequent calls to `context.save()` and `context.restore()` * chore: fix eslint issue * perf: refactor hot code * fix: memory leaks * fix: memory leak caused by element destroy event not being triggered * docs: add perf demo to site * chore: add necessary comments * chore: remove comments
- Loading branch information
Showing
51 changed files
with
1,566 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
'@antv/g-plugin-canvas-path-generator': minor | ||
'@antv/g-plugin-canvaskit-renderer': minor | ||
'@antv/g-plugin-canvas-renderer': minor | ||
'@antv/g-plugin-device-renderer': minor | ||
'@antv/g-plugin-canvas-picker': minor | ||
'@antv/g-plugin-image-loader': minor | ||
'@antv/g-plugin-svg-renderer': minor | ||
'@antv/g-lite': minor | ||
--- | ||
|
||
perf: optimize canvas renderer performance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as lil from 'lil-gui'; | ||
import { Rect, Group, CanvasEvent } from '@antv/g'; | ||
import type { Canvas } from '@antv/g'; | ||
|
||
export async function attrUpdate(context: { canvas: Canvas; gui: lil.GUI }) { | ||
const { canvas, gui } = context; | ||
console.log(canvas); | ||
|
||
await canvas.ready; | ||
|
||
const { width, height } = canvas.getConfig(); | ||
const count = 2e4; | ||
const root = new Group(); | ||
const rects = []; | ||
|
||
const perfStore: { [k: string]: { count: number; time: number } } = { | ||
update: { count: 0, time: 0 }, | ||
setAttribute: { count: 0, time: 0 }, | ||
}; | ||
|
||
function updatePerf(key: string, time: number) { | ||
perfStore[key].count++; | ||
perfStore[key].time += time; | ||
console.log( | ||
`average ${key} time: `, | ||
perfStore[key].time / perfStore[key].count, | ||
); | ||
} | ||
|
||
function update() { | ||
// const startTime = performance.now(); | ||
// console.time('update'); | ||
|
||
const rectsToRemove = []; | ||
|
||
// const startTime0 = performance.now(); | ||
// console.time('setAttribute'); | ||
for (let i = 0; i < count; i++) { | ||
const rect = rects[i]; | ||
rect.x -= rect.speed; | ||
(rect.el as Rect).setAttribute('x', rect.x); | ||
if (rect.x + rect.size < 0) rectsToRemove.push(i); | ||
} | ||
// console.timeEnd('setAttribute'); | ||
// updatePerf('setAttribute', performance.now() - startTime0); | ||
|
||
rectsToRemove.forEach((i) => { | ||
rects[i].x = width + rects[i].size / 2; | ||
}); | ||
|
||
// console.timeEnd('update'); | ||
// updatePerf('update', performance.now() - startTime); | ||
} | ||
|
||
function render() { | ||
for (let i = 0; i < count; i++) { | ||
const x = Math.random() * width; | ||
const y = Math.random() * height; | ||
const size = 10 + Math.random() * 40; | ||
const speed = 1 + Math.random(); | ||
|
||
const rect = new Rect({ | ||
style: { | ||
x, | ||
y, | ||
width: size, | ||
height: size, | ||
fill: 'white', | ||
stroke: 'black', | ||
}, | ||
}); | ||
root.appendChild(rect); | ||
rects[i] = { x, y, size, speed, el: rect }; | ||
} | ||
} | ||
|
||
render(); | ||
canvas.addEventListener(CanvasEvent.BEFORE_RENDER, () => update()); | ||
|
||
canvas.appendChild(root); | ||
|
||
canvas.addEventListener( | ||
'rerender', | ||
() => { | ||
// console.timeEnd('render'); | ||
}, | ||
{ once: true }, | ||
); | ||
|
||
// GUI | ||
canvas.getConfig().renderer.getConfig().enableRenderingOptimization = true; | ||
|
||
gui | ||
.add( | ||
{ | ||
enableRenderingOptimization: canvas.getConfig().renderer.getConfig() | ||
.enableRenderingOptimization, | ||
}, | ||
'enableRenderingOptimization', | ||
) | ||
.onChange((result) => { | ||
canvas.getConfig().renderer.getConfig().enableRenderingOptimization = | ||
result; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { circles } from './circles'; | ||
export { rects } from './rect'; | ||
export { image } from './image'; | ||
export { attrUpdate } from './attr-update'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.