Skip to content

Commit

Permalink
perf: optimize process performance (#5792)
Browse files Browse the repository at this point in the history
* perf: optimize data perf

* feat(utils): add mergeOptions util

* refactor(elements): use mergeOptions replace deepMix

* perf(runtime): optimize element perf
  • Loading branch information
Aarebecca committed May 31, 2024
1 parent d7a138f commit 59e8bcd
Show file tree
Hide file tree
Showing 24 changed files with 104 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/g6/__tests__/demos/element-node-triangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const elementNodeTriangle: TestCase = async (context) => {
type: 'triangle', // 👈🏻 Node shape type.
style: {
size: 40,
direction: (d: any) => (d.id === 'ports' ? 'left' : undefined),
direction: (d: any) => (d.id === 'ports' ? 'left' : 'up'),
labelText: (d) => d.id!,
iconSrc: 'https://gw.alipayobjects.com/zos/basement_prod/012bcf4f-423b-4922-8c24-32a89f8c41ce.svg',
ports: (d) => (d.id === 'ports' ? [{ placement: 'left' }, { placement: 'top' }, { placement: 'bottom' }] : []),
Expand Down
11 changes: 10 additions & 1 deletion packages/g6/__tests__/unit/utils/style.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computeElementCallbackStyle, zIndexOf } from '@/src/utils/style';
import { computeElementCallbackStyle, mergeOptions, zIndexOf } from '@/src/utils/style';

describe('style', () => {
it('computeElementCallbackStyle', () => {
Expand Down Expand Up @@ -45,4 +45,13 @@ describe('style', () => {
expect(zIndexOf({ id: 'node-1', style: { zIndex: 1 } })).toBe(1);
expect(zIndexOf({ id: 'node-1', style: { zIndex: -1 } })).toBe(-1);
});

it('mergeOptions', () => {
expect(
mergeOptions(
{ style: { a: 1, b: [1, 2], c: { d: 1 } }, id: '1' },
{ style: { a: 2, b: [2, 3], c: { f: 1 } }, id: '2' },
),
).toEqual({ style: { a: 2, b: [2, 3], c: { f: 1 } }, id: '2' });
});
});
5 changes: 3 additions & 2 deletions packages/g6/src/elements/combos/base-combo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AABB, BaseStyleProps, DisplayObject, DisplayObjectConfig, Group } from '@antv/g';
import { deepMix, isFunction } from '@antv/util';
import { isFunction } from '@antv/util';
import { COMBO_KEY } from '../../constants';
import type {
CollapsedMarkerStyleProps,
Expand All @@ -17,6 +17,7 @@ import { parsePadding } from '../../utils/padding';
import { getXYByPlacement, positionOf } from '../../utils/position';
import { subStyleProps } from '../../utils/prefix';
import { parseSize } from '../../utils/size';
import { mergeOptions } from '../../utils/style';
import { add, divide } from '../../utils/vector';
import type { BaseNodeStyleProps } from '../nodes';
import { BaseNode } from '../nodes';
Expand Down Expand Up @@ -99,7 +100,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
collapsedMarkerType: 'child-count',
};
constructor(options: DisplayObjectConfig<BaseComboStyleProps>) {
super(deepMix({}, { style: BaseCombo.defaultStyleProps }, options));
super(mergeOptions({ style: BaseCombo.defaultStyleProps }, options));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/g6/src/elements/edges/base-edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from '@antv/g';
import { Image, Path } from '@antv/g';
import type { PathArray } from '@antv/util';
import { deepMix, isEmpty, isFunction } from '@antv/util';
import { isEmpty, isFunction } from '@antv/util';
import type {
BaseElementStyleProps,
EdgeArrowStyleProps,
Expand All @@ -26,6 +26,7 @@ import { getCubicLoopPath, getLabelPositionStyle } from '../../utils/edge';
import { findPorts, getConnectionPoint, isSameNode } from '../../utils/element';
import { omitStyleProps, subStyleProps } from '../../utils/prefix';
import { parseSize } from '../../utils/size';
import { mergeOptions } from '../../utils/style';
import * as Symbol from '../../utils/symbol';
import { getWordWrapWidthByEnds } from '../../utils/text';
import { BaseElement } from '../base-element';
Expand Down Expand Up @@ -206,7 +207,7 @@ export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> {
};

constructor(options: DisplayObjectConfig<BaseEdgeStyleProps>) {
super(deepMix({}, { style: BaseEdge.defaultStyleProps }, options));
super(mergeOptions({ style: BaseEdge.defaultStyleProps }, options));
}

protected get sourceNode() {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/edges/cubic-horizontal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DisplayObjectConfig } from '@antv/g';
import { deepMix } from '@antv/util';
import type { Point } from '../../types';
import { mergeOptions } from '../../utils/style';
import type { BaseEdgeStyleProps } from './base-edge';
import { Cubic } from './cubic';

Expand Down Expand Up @@ -42,7 +42,7 @@ export class CubicHorizontal extends Cubic {
};

constructor(options: DisplayObjectConfig<CubicHorizontalStyleProps>) {
super(deepMix({}, { style: CubicHorizontal.defaultStyleProps }, options));
super(mergeOptions({ style: CubicHorizontal.defaultStyleProps }, options));
}

protected getControlPoints(
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/edges/cubic-vertical.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DisplayObjectConfig } from '@antv/g';
import { deepMix } from '@antv/util';
import type { Point } from '../../types';
import { mergeOptions } from '../../utils/style';
import type { BaseEdgeStyleProps } from './base-edge';
import { Cubic } from './cubic';

Expand Down Expand Up @@ -42,7 +42,7 @@ export class CubicVertical extends Cubic {
};

constructor(options: DisplayObjectConfig<CubicVerticalStyleProps>) {
super(deepMix({}, { style: CubicVertical.defaultStyleProps }, options));
super(mergeOptions({ style: CubicVertical.defaultStyleProps }, options));
}

protected getControlPoints(
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/edges/cubic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DisplayObjectConfig } from '@antv/g';
import type { PathArray } from '@antv/util';
import { deepMix } from '@antv/util';
import type { Point } from '../../types';
import { getCubicPath, getCurveControlPoint, parseCurveOffset, parseCurvePosition } from '../../utils/edge';
import { mergeOptions } from '../../utils/style';
import type { BaseEdgeStyleProps } from './base-edge';
import { BaseEdge } from './base-edge';

Expand Down Expand Up @@ -48,7 +48,7 @@ export class Cubic extends BaseEdge {
};

constructor(options: DisplayObjectConfig<CubicStyleProps>) {
super(deepMix({}, { style: Cubic.defaultStyleProps }, options));
super(mergeOptions({ style: Cubic.defaultStyleProps }, options));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/edges/line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DisplayObjectConfig } from '@antv/g';
import type { PathArray } from '@antv/util';
import { deepMix } from '@antv/util';
import { mergeOptions } from '../../utils/style';
import type { BaseEdgeStyleProps } from './base-edge';
import { BaseEdge } from './base-edge';

Expand All @@ -24,7 +24,7 @@ export class Line extends BaseEdge {
static defaultStyleProps: Partial<LineStyleProps> = {};

constructor(options: DisplayObjectConfig<LineStyleProps>) {
super(deepMix({}, { style: Line.defaultStyleProps }, options));
super(mergeOptions({ style: Line.defaultStyleProps }, options));
}

protected getKeyPath(attributes: ParsedLineStyleProps): PathArray {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/edges/polyline.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { DisplayObjectConfig } from '@antv/g';
import type { PathArray } from '@antv/util';
import { deepMix } from '@antv/util';
import type { LoopStyleProps, Padding, Point, Port } from '../../types';
import { getBBoxHeight, getBBoxWidth, getNodeBBox } from '../../utils/bbox';
import { getPolylineLoopPath, getPolylinePath } from '../../utils/edge';
import { findPorts, getConnectionPoint, getPortPosition } from '../../utils/element';
import { subStyleProps } from '../../utils/prefix';
import { orth } from '../../utils/router/orth';
import { mergeOptions } from '../../utils/style';
import type { BaseEdgeStyleProps } from './base-edge';
import { BaseEdge } from './base-edge';

Expand Down Expand Up @@ -70,7 +70,7 @@ export class Polyline extends BaseEdge {
};

constructor(options: DisplayObjectConfig<PolylineStyleProps>) {
super(deepMix({}, { style: Polyline.defaultStyleProps }, options));
super(mergeOptions({ style: Polyline.defaultStyleProps }, options));
}

protected getKeyPath(attributes: ParsedPolylineStyleProps): PathArray {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/edges/quadratic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DisplayObjectConfig } from '@antv/g';
import type { PathArray } from '@antv/util';
import { deepMix } from '@antv/util';
import type { Point } from '../../types';
import { getCurveControlPoint, getQuadraticPath } from '../../utils/edge';
import { mergeOptions } from '../../utils/style';
import type { BaseEdgeStyleProps } from './base-edge';
import { BaseEdge } from './base-edge';

Expand Down Expand Up @@ -48,7 +48,7 @@ export class Quadratic extends BaseEdge {
};

constructor(options: DisplayObjectConfig<QuadraticStyleProps>) {
super(deepMix({}, { style: Quadratic.defaultStyleProps }, options));
super(mergeOptions({ style: Quadratic.defaultStyleProps }, options));
}

protected getKeyPath(attributes: ParsedQuadraticStyleProps): PathArray {
Expand Down
5 changes: 3 additions & 2 deletions packages/g6/src/elements/nodes/base-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseStyleProps, DisplayObject, DisplayObjectConfig, Group } from '@antv/g';
import { Circle as GCircle } from '@antv/g';
import { deepMix, isEmpty } from '@antv/util';
import { isEmpty } from '@antv/util';
import type { CategoricalPalette } from '../../palettes/types';
import type { NodeData } from '../../spec';
import type {
Expand All @@ -22,6 +22,7 @@ import { getRectIntersectPoint } from '../../utils/point';
import { getXYByPlacement } from '../../utils/position';
import { omitStyleProps, subObject, subStyleProps } from '../../utils/prefix';
import { parseSize } from '../../utils/size';
import { mergeOptions } from '../../utils/style';
import { getWordWrapWidthByBox } from '../../utils/text';
import { replaceTranslateInTransform } from '../../utils/transform';
import { BaseElement } from '../base-element';
Expand Down Expand Up @@ -218,7 +219,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
};

constructor(options: DisplayObjectConfig<S>) {
super(deepMix({}, { style: BaseNode.defaultStyleProps }, options));
super(mergeOptions({ style: BaseNode.defaultStyleProps }, options));
}

protected getSize(attributes = this.attributes) {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/nodes/circle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DisplayObjectConfig, CircleStyleProps as GCircleStyleProps, Group } from '@antv/g';
import { Circle as GCircle } from '@antv/g';
import { deepMix } from '@antv/util';
import { ICON_SIZE_RATIO } from '../../constants/element';
import type { Point } from '../../types';
import { getEllipseIntersectPoint } from '../../utils/point';
import { mergeOptions } from '../../utils/style';
import type { IconStyleProps } from '../shapes';
import type { BaseNodeStyleProps } from './base-node';
import { BaseNode } from './base-node';
Expand All @@ -26,7 +26,7 @@ export class Circle extends BaseNode {
};

constructor(options: DisplayObjectConfig<CircleStyleProps>) {
super(deepMix({}, { style: Circle.defaultStyleProps }, options));
super(mergeOptions({ style: Circle.defaultStyleProps }, options));
}

protected drawKeyShape(attributes: Required<CircleStyleProps>, container: Group) {
Expand Down
5 changes: 3 additions & 2 deletions packages/g6/src/elements/nodes/donut.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Path } from '@antv/g';
import { deepMix, isNumber, isString } from '@antv/util';
import { isNumber, isString } from '@antv/util';
import { getPaletteColors } from '../../utils/palette';
import { subStyleProps } from '../../utils/prefix';
import { parseSize } from '../../utils/size';
Expand All @@ -8,6 +8,7 @@ import { Circle } from './circle';
import type { BaseStyleProps, DisplayObjectConfig, Group } from '@antv/g';
import type { CategoricalPalette } from '../../palettes/types';
import type { DonutRound, Prefix } from '../../types';
import { mergeOptions } from '../../utils/style';
import type { CircleStyleProps } from './circle';

/**
Expand Down Expand Up @@ -51,7 +52,7 @@ export class Donut extends Circle {
};

constructor(options: DisplayObjectConfig<DonutStyleProps>) {
super(deepMix({}, { style: Donut.defaultStyleProps }, options));
super(mergeOptions({ style: Donut.defaultStyleProps }, options));
}

private parseOuterR() {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/nodes/ellipse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DisplayObjectConfig, EllipseStyleProps as GEllipseStyleProps, Group } from '@antv/g';
import { Ellipse as GEllipse } from '@antv/g';
import { deepMix } from '@antv/util';
import { ICON_SIZE_RATIO } from '../../constants/element';
import type { Point } from '../../types';
import { getEllipseIntersectPoint } from '../../utils/point';
import { mergeOptions } from '../../utils/style';
import type { IconStyleProps } from '../shapes';
import type { BaseNodeStyleProps } from './base-node';
import { BaseNode } from './base-node';
Expand All @@ -26,7 +26,7 @@ export class Ellipse extends BaseNode {
};

constructor(options: DisplayObjectConfig<EllipseStyleProps>) {
super(deepMix({}, { style: Ellipse.defaultStyleProps }, options));
super(mergeOptions({ style: Ellipse.defaultStyleProps }, options));
}

protected drawKeyShape(attributes: Required<EllipseStyleProps>, container: Group) {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/nodes/image.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DisplayObjectConfig, RectStyleProps as GRectStyleProps, Group } from '@antv/g';
import { Image as GImage, ImageStyleProps as GImageStyleProps, Rect as GRect } from '@antv/g';
import { deepMix } from '@antv/util';
import { ICON_SIZE_RATIO } from '../../constants/element';
import { subStyleProps } from '../../utils/prefix';
import { mergeOptions } from '../../utils/style';
import { add } from '../../utils/vector';
import type { IconStyleProps } from '../shapes';
import type { BaseNodeStyleProps } from './base-node';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class Image extends BaseNode<ImageStyleProps> {
};

constructor(options: DisplayObjectConfig<ImageStyleProps>) {
super(deepMix({}, { style: Image.defaultStyleProps }, options));
super(mergeOptions({ style: Image.defaultStyleProps }, options));
}

protected getKeyStyle(attributes: Required<ImageStyleProps>): GImageStyleProps {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/nodes/rect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DisplayObjectConfig, RectStyleProps as GRectStyleProps, Group } from '@antv/g';
import { Rect as GRect } from '@antv/g';
import { deepMix } from '@antv/util';
import { ICON_SIZE_RATIO } from '../../constants/element';
import { mergeOptions } from '../../utils/style';
import type { IconStyleProps } from '../shapes';
import type { BaseNodeStyleProps } from './base-node';
import { BaseNode } from './base-node';
Expand All @@ -25,7 +25,7 @@ export class Rect extends BaseNode<RectStyleProps> {
};

constructor(options: DisplayObjectConfig<RectStyleProps>) {
super(deepMix({}, { style: Rect.defaultStyleProps }, options));
super(mergeOptions({ style: Rect.defaultStyleProps }, options));
}

protected getKeyStyle(attributes: ParsedRectStyleProps): GRectStyleProps {
Expand Down
5 changes: 3 additions & 2 deletions packages/g6/src/elements/nodes/triangle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { DisplayObjectConfig } from '@antv/g';
import { deepMix, isEmpty } from '@antv/util';
import { isEmpty } from '@antv/util';
import { ICON_SIZE_RATIO } from '../../constants/element';
import type { NodePortStyleProps, Point, TriangleDirection, TrianglePortPlacement } from '../../types';
import { getIncircleRadius, getTriangleCenter } from '../../utils/bbox';
import { getPortXYByPlacement, getTrianglePoints, getTrianglePorts } from '../../utils/element';
import { subStyleProps } from '../../utils/prefix';
import { mergeOptions } from '../../utils/style';
import type { PolygonStyleProps } from '../shapes';
import { IconStyleProps } from '../shapes';
import { Polygon } from '../shapes/polygon';
Expand Down Expand Up @@ -36,7 +37,7 @@ export class Triangle extends Polygon<TriangleStyleProps> {
};

constructor(options: DisplayObjectConfig<TriangleStyleProps>) {
super(deepMix({}, { style: Triangle.defaultStyleProps }, options));
super(mergeOptions({ style: Triangle.defaultStyleProps }, options));
}

protected getPoints(attributes: Required<TriangleStyleProps>): Point[] {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/shapes/badge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DisplayObjectConfig, Group } from '@antv/g';
import { deepMix } from '@antv/util';
import { mergeOptions } from '../../utils/style';
import { BaseShape } from './base-shape';
import type { LabelStyleProps } from './label';
import { Label } from './label';
Expand All @@ -18,7 +18,7 @@ export class Badge extends BaseShape<BadgeStyleProps> {
};

constructor(options: BadgeOptions) {
super(deepMix({}, { style: Badge.defaultStyleProps }, options));
super(mergeOptions({ style: Badge.defaultStyleProps }, options));
}

protected getBadgeStyle(attributes: ParsedBadgeStyleProps) {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/shapes/contour.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DisplayObjectConfig, Group, PathStyleProps } from '@antv/g';
import { Path } from '@antv/g';
import { deepMix } from '@antv/util';
import type { CardinalPlacement, Prefix } from '../../types';
import { getPolygonTextStyleByPlacement } from '../../utils/polygon';
import { subStyleProps } from '../../utils/prefix';
import { mergeOptions } from '../../utils/style';
import { getWordWrapWidthByBox } from '../../utils/text';
import type { LabelStyleProps } from '../shapes';
import { BaseShape } from './base-shape';
Expand Down Expand Up @@ -76,7 +76,7 @@ export class Contour extends BaseShape<ContourStyleProps> {
};

constructor(options: ContourOptions) {
super(deepMix({}, { style: Contour.defaultStyleProps }, options));
super(mergeOptions({ style: Contour.defaultStyleProps }, options));
}

protected getLabelStyle(attributes: ParsedContourStyleProps): LabelStyleProps | false {
Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/elements/shapes/label.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DisplayObjectConfig, Group, Rect, RectStyleProps, Text, TextStyleProps } from '@antv/g';
import { deepMix } from '@antv/util';
import type { Padding } from '../../types/padding';
import type { Prefix } from '../../types/prefix';
import { parsePadding } from '../../utils/padding';
import { omitStyleProps, startsWith, subStyleProps } from '../../utils/prefix';
import { mergeOptions } from '../../utils/style';
import { BaseShape } from './base-shape';

export interface LabelStyleProps extends TextStyleProps, Prefix<'background', RectStyleProps> {
Expand Down Expand Up @@ -39,7 +39,7 @@ export class Label extends BaseShape<LabelStyleProps> {
};

constructor(options: LabelOptions) {
super(deepMix({}, { style: Label.defaultStyleProps }, options));
super(mergeOptions({ style: Label.defaultStyleProps }, options));
}

protected isTextStyle(key: string) {
Expand Down
Loading

0 comments on commit 59e8bcd

Please sign in to comment.