-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize element performance (#5800)
* perf: remove util isType usage * fix(plugins): fix background plugin * fix: fix autoFit not effect after draw * test: add test case to draw 20000 elements * feat(utils): add createGeometry util * refactor(3d): optimize massive shape performance * test: add massive 3d elements case
- Loading branch information
Showing
25 changed files
with
286 additions
and
51 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
63 changes: 63 additions & 0 deletions
63
packages/g6-extension-3d/__tests__/demos/massive-elements.ts
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,63 @@ | ||
import { CameraSetting, ExtensionCategory, Graph, register } from '@antv/g6'; | ||
import { Light, Line3D, ObserveCanvas3D, Sphere, ZoomCanvas3D, renderer } from '../../src'; | ||
|
||
export const massiveElements: TestCase = async (context) => { | ||
register(ExtensionCategory.PLUGIN, '3d-light', Light); | ||
register(ExtensionCategory.NODE, 'sphere', Sphere); | ||
register(ExtensionCategory.EDGE, 'line3d', Line3D); | ||
register(ExtensionCategory.PLUGIN, 'camera-setting', CameraSetting); | ||
register(ExtensionCategory.BEHAVIOR, 'zoom-canvas-3d', ZoomCanvas3D); | ||
register(ExtensionCategory.BEHAVIOR, 'observe-canvas-3d', ObserveCanvas3D); | ||
|
||
const data = await fetch('https://assets.antv.antgroup.com/g6/eva-3d-data.json').then((res) => res.json()); | ||
|
||
const graph = new Graph({ | ||
...context, | ||
animation: false, | ||
renderer, | ||
data, | ||
node: { | ||
type: 'sphere', | ||
style: { | ||
materialType: 'phong', | ||
size: 50, | ||
x: (d) => d.data!.x, | ||
y: (d) => d.data!.y, | ||
z: (d) => d.data!.z, | ||
}, | ||
palette: { | ||
color: 'tableau', | ||
type: 'group', | ||
field: 'cluster', | ||
}, | ||
}, | ||
edge: { | ||
type: 'line3d', | ||
}, | ||
behaviors: ['observe-canvas-3d', 'zoom-canvas-3d'], | ||
plugins: [ | ||
{ | ||
type: 'camera-setting', | ||
projectionMode: 'orthographic', | ||
near: 1, | ||
far: 10000, | ||
fov: 45, | ||
aspect: 1, | ||
}, | ||
{ | ||
type: '3d-light', | ||
directional: { | ||
direction: [0, 0, 1], | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
console.time('time'); | ||
|
||
await graph.draw(); | ||
|
||
console.timeEnd('time'); | ||
|
||
return graph; | ||
}; |
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
14 changes: 14 additions & 0 deletions
14
packages/g6-extension-3d/__tests__/unit/utils/geometry.spec.ts
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,14 @@ | ||
import { CubeGeometry } from '@antv/g-plugin-3d'; | ||
import { createGeometry } from '../../../src/utils/geometry'; | ||
|
||
describe('geometry', () => { | ||
it('createGeometry', () => { | ||
const device: any = {}; | ||
const geometry1 = createGeometry('cube', device, CubeGeometry, { width: 1, height: 1, depth: 1 }); | ||
const geometry2 = createGeometry('cube', device, CubeGeometry, { depth: 1, height: 1, width: 1 }); | ||
const geometry3 = createGeometry('cube', device, CubeGeometry, { width: 2, height: 2, depth: 2 }); | ||
|
||
expect(geometry1).toBe(geometry2); | ||
expect(geometry1).not.toBe(geometry3); | ||
}); | ||
}); |
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
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,39 @@ | ||
import type { Device } from '@antv/g-device-api'; | ||
import type { ProceduralGeometry } from '@antv/g-plugin-3d'; | ||
|
||
const GEOMETRY_CACHE = new Map<string, unknown>(); | ||
|
||
/** | ||
* <zh/> 创建几何体 | ||
* | ||
* <en/> Create geometry | ||
* @param type - <zh/> 几何体类型 <en/> geometry type | ||
* @param device - <zh/> 设备对象 <en/> device object | ||
* @param Ctor - <zh/> 几何体构造函数 <en/> geometry constructor | ||
* @param style - <zh/> 几何体样式 <en/> geometry style | ||
* @returns <zh/> 几何体对象 <en/> geometry object | ||
*/ | ||
export function createGeometry<T extends ProceduralGeometry<any>>( | ||
type: string, | ||
device: Device, | ||
Ctor: new (...args: any[]) => T, | ||
style: Record<string, unknown>, | ||
) { | ||
const cacheKey = | ||
type + | ||
'|' + | ||
Object.entries(style) | ||
.sort(([a], [b]) => a.localeCompare(b)) | ||
.map(([k, v]) => `${k}:${v}`) | ||
.join(','); | ||
|
||
if (GEOMETRY_CACHE.has(cacheKey)) { | ||
return GEOMETRY_CACHE.get(cacheKey) as T; | ||
} | ||
|
||
const geometry = new Ctor(device, style); | ||
|
||
GEOMETRY_CACHE.set(cacheKey, geometry); | ||
|
||
return geometry; | ||
} |
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,86 @@ | ||
import type { GraphOptions } from '@/src'; | ||
import { Graph } from '@/src'; | ||
|
||
export const perf20000Elements: TestCase = async (context) => { | ||
const data = await fetch('https://assets.antv.antgroup.com/g6/20000.json').then((res) => res.json()); | ||
|
||
const graph = new Graph({ | ||
...context, | ||
animation: false, | ||
data, | ||
node: { | ||
style: { | ||
size: 8, | ||
}, | ||
palette: { | ||
type: 'group', | ||
field: 'cluster', | ||
}, | ||
}, | ||
behaviors: ['zoom-canvas', 'drag-canvas', 'drag-element'], | ||
autoFit: 'view', | ||
plugins: [{ type: 'background', background: '#fff' }], | ||
}); | ||
|
||
console.time('time'); | ||
await graph.draw(); | ||
console.timeEnd('time'); | ||
|
||
perf20000Elements.form = (gui) => { | ||
const themes: Record<string, GraphOptions> = { | ||
'🌞 Light': { | ||
theme: 'light', | ||
node: { | ||
palette: { | ||
type: 'group', | ||
field: 'cluster', | ||
}, | ||
}, | ||
plugins: [{ type: 'background', background: '#fff' }], | ||
}, | ||
'🌚 Dark': { | ||
theme: 'dark', | ||
node: { | ||
palette: { | ||
type: 'group', | ||
field: 'cluster', | ||
}, | ||
}, | ||
plugins: [{ type: 'background', background: '#000' }], | ||
}, | ||
'🌎 Blue': { | ||
theme: 'light', | ||
node: { | ||
palette: { | ||
type: 'group', | ||
field: 'cluster', | ||
color: 'blues', | ||
invert: true, | ||
}, | ||
}, | ||
plugins: [{ type: 'background', background: '#f3faff' }], | ||
}, | ||
'🌕 Yellow': { | ||
background: '#fcf9f1', | ||
theme: 'light', | ||
node: { | ||
palette: { | ||
type: 'group', | ||
field: 'cluster', | ||
color: ['#ffe7ba', '#ffd591', '#ffc069', '#ffa940', '#fa8c16', '#d46b08', '#ad4e00', '#873800', '#612500'], | ||
}, | ||
}, | ||
plugins: [{ type: 'background', background: '#fcf9f1' }], | ||
}, | ||
}; | ||
|
||
return [ | ||
gui.add({ theme: '🌞 Light' }, 'theme', Object.keys(themes)).onChange((theme: string) => { | ||
graph.setOptions(themes[theme]); | ||
graph.draw(); | ||
}), | ||
]; | ||
}; | ||
|
||
return graph; | ||
}; |
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.