From 47b94bb760d46104547b6e567fbc8460c2eb714a Mon Sep 17 00:00:00 2001 From: Yin Date: Thu, 19 Dec 2024 12:50:24 +0800 Subject: [PATCH 1/3] Improve performance by avoiding repeated recalculateRenderedStyle #3309 --- src/collection/dimensions/bounds.js | 15 ++------------- .../base/coord-ele-math/rendered-style.js | 4 ++++ src/util/bounds.js | 13 +++++++++++++ src/util/index.js | 1 + 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/util/bounds.js diff --git a/src/collection/dimensions/bounds.js b/src/collection/dimensions/bounds.js index e562798a12..0c8a9c69d9 100644 --- a/src/collection/dimensions/bounds.js +++ b/src/collection/dimensions/bounds.js @@ -1,6 +1,6 @@ import * as is from '../../is'; import { assignBoundingBox, expandBoundingBoxSides, clearBoundingBox, expandBoundingBox, makeBoundingBox, copyBoundingBox, shiftBoundingBox, updateBoundingBox } from '../../math'; -import { defaults, getPrefixedProperty, hashIntsArray } from '../../util'; +import { defaults, getPrefixedProperty, getBoundingBoxPosKey } from '../../util'; let fn, elesfn; @@ -800,18 +800,6 @@ let getKey = function( opts ){ return key; }; -let getBoundingBoxPosKey = ele => { - if( ele.isEdge() ){ - let p1 = ele.source().position(); - let p2 = ele.target().position(); - let r = x => Math.round(x); - - return hashIntsArray([ r(p1.x), r(p1.y), r(p2.x), r(p2.y) ]); - } else { - return 0; - } -}; - let cachedBoundingBoxImpl = function( ele, opts ){ let _p = ele._private; let bb; @@ -926,6 +914,7 @@ elesfn.boundingBox = function( options ){ let isPosKeySame = _p.bbCachePosKey === currPosKey; let useCache = opts.useCache && isPosKeySame && !_p.styleDirty; + _p.bbCachePosKey = currPosKey; ele.recalculateRenderedStyle( useCache ); } } diff --git a/src/extensions/renderer/base/coord-ele-math/rendered-style.js b/src/extensions/renderer/base/coord-ele-math/rendered-style.js index b260fdadbb..a74fab9d34 100644 --- a/src/extensions/renderer/base/coord-ele-math/rendered-style.js +++ b/src/extensions/renderer/base/coord-ele-math/rendered-style.js @@ -1,3 +1,5 @@ +import { getBoundingBoxPosKey } from '../../../../util'; + let BRp = {}; BRp.registerCalculationListeners = function(){ @@ -50,6 +52,8 @@ BRp.registerCalculationListeners = function(){ enqueue( ele.connectedEdges() ); rstyle.cleanConnected = true; + } else if (ele.isEdge()) { + ele._private.bbCachePosKey = getBoundingBoxPosKey( ele ); } } diff --git a/src/util/bounds.js b/src/util/bounds.js new file mode 100644 index 0000000000..ff7eb6daa6 --- /dev/null +++ b/src/util/bounds.js @@ -0,0 +1,13 @@ +import { hashIntsArray } from '.'; + +export const getBoundingBoxPosKey = (ele) => { + if (ele.isEdge()) { + let p1 = ele.source().position(); + let p2 = ele.target().position(); + let r = (x) => Math.round(x); + + return hashIntsArray([r(p1.x), r(p1.y), r(p2.x), r(p2.y)]); + } else { + return 0; + } +}; diff --git a/src/util/index.js b/src/util/index.js index 52eb099a69..89026bf1ec 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -12,6 +12,7 @@ export * from './maps'; export * from './strings'; export * from './timing'; export * from './hash'; +export * from './bounds'; export { strings, extend, extend as assign, memoize, regex, sort }; From 2add561911a6a64636871c71464932ad4cb93ea8 Mon Sep 17 00:00:00 2001 From: Yin Date: Wed, 8 Jan 2025 17:13:30 +0800 Subject: [PATCH 2/3] Fix bezier curve render problem --- .../coord-ele-math/edge-control-points.js | 25 ++----------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/extensions/renderer/base/coord-ele-math/edge-control-points.js b/src/extensions/renderer/base/coord-ele-math/edge-control-points.js index 205c175cac..3d14866008 100644 --- a/src/extensions/renderer/base/coord-ele-math/edge-control-points.js +++ b/src/extensions/renderer/base/coord-ele-math/edge-control-points.js @@ -706,28 +706,7 @@ BRp.findEdgeControlPoints = function( edges ){ let cy = r.cy; let hasCompounds = cy.hasCompoundNodes(); - let hashTable = { - map: new Map(), - get: function(pairId){ - let map2 = this.map.get(pairId[0]); - - if( map2 != null ){ - return map2.get(pairId[1]); - } else { - return null; - } - }, - set: function(pairId, val){ - let map2 = this.map.get(pairId[0]); - - if( map2 == null ){ - map2 = new Map(); - this.map.set(pairId[0], map2); - } - - map2.set(pairId[1], val); - } - }; + let hashTable = new Map(); let pairIds = []; let haystackEdges = []; @@ -756,7 +735,7 @@ BRp.findEdgeControlPoints = function( edges ){ let srcIndex = src.poolIndex(); let tgtIndex = tgt.poolIndex(); - let pairId = [ srcIndex, tgtIndex ].sort(); + let pairId = `${srcIndex}_${tgtIndex}_${edgeIsUnbundled}`; let tableEntry = hashTable.get( pairId ); From 0482afa803bad3569c0822f58dc9db5348e7949d Mon Sep 17 00:00:00 2001 From: Yin Date: Thu, 9 Jan 2025 19:50:22 +0800 Subject: [PATCH 3/3] Sort index before assign pairId --- .../renderer/base/coord-ele-math/edge-control-points.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/extensions/renderer/base/coord-ele-math/edge-control-points.js b/src/extensions/renderer/base/coord-ele-math/edge-control-points.js index 3d14866008..2e80b8bb12 100644 --- a/src/extensions/renderer/base/coord-ele-math/edge-control-points.js +++ b/src/extensions/renderer/base/coord-ele-math/edge-control-points.js @@ -735,7 +735,8 @@ BRp.findEdgeControlPoints = function( edges ){ let srcIndex = src.poolIndex(); let tgtIndex = tgt.poolIndex(); - let pairId = `${srcIndex}_${tgtIndex}_${edgeIsUnbundled}`; + let sortIndex = [ srcIndex, tgtIndex ].sort(); + let pairId = `${sortIndex[0]}_${sortIndex[1]}_${edgeIsUnbundled}`; let tableEntry = hashTable.get( pairId );