From bcd5a32483ed650407242c5a5e39ea5a92928c18 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Tue, 22 Jan 2019 14:04:49 +0100 Subject: [PATCH] Increase tree statistic performance Fixes #44 --- app/src/components/Tree/TreeNodeSubnodes.tsx | 4 +-- backend/src/Model/Edge.ts | 2 +- backend/src/Model/TreeNode.ts | 31 +++++++++++++------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/src/components/Tree/TreeNodeSubnodes.tsx b/app/src/components/Tree/TreeNodeSubnodes.tsx index 946f512..8caee35 100644 --- a/app/src/components/Tree/TreeNodeSubnodes.tsx +++ b/app/src/components/Tree/TreeNodeSubnodes.tsx @@ -32,7 +32,7 @@ class TreeNodeSubnodes extends React.Component { private sortedNodes(): q.TreeNode[] { const { topicOrder, treeNode } = this.props - let edges = Object.values(treeNode.edges) + let edges = treeNode.edgeArray if (topicOrder === TopicOrder.abc) { edges = edges.sort((a, b) => a.name.localeCompare(b.name)) } @@ -59,7 +59,7 @@ class TreeNodeSubnodes extends React.Component { } public render() { - const edges = Object.values(this.props.treeNode.edges) + const edges = this.props.treeNode.edgeArray if (edges.length === 0 || this.props.collapsed) { return null } diff --git a/backend/src/Model/Edge.ts b/backend/src/Model/Edge.ts index 0d61326..90007c4 100644 --- a/backend/src/Model/Edge.ts +++ b/backend/src/Model/Edge.ts @@ -13,7 +13,7 @@ export class Edge implements Hashable { } public edges() { - return this.target ? Object.values(this.target.edges) : [] + return this.target ? this.target.edgeArray : [] } public hash(): string { diff --git a/backend/src/Model/TreeNode.ts b/backend/src/Model/TreeNode.ts index bde538e..9b9e240 100644 --- a/backend/src/Model/TreeNode.ts +++ b/backend/src/Model/TreeNode.ts @@ -7,6 +7,7 @@ export class TreeNode { public mqttMessage?: MqttMessage public messageHistory: RingBuffer = new RingBuffer(3000, 100) public edges: {[s: string]: Edge} = {} + public edgeArray: Edge[] = [] public collapsed = false public messages: number = 0 public lastUpdate: number = Date.now() @@ -14,8 +15,10 @@ export class TreeNode { public onEdgesChange = new EventDispatcher(this) public onMessage = new EventDispatcher(this) public isTree = false + private cachedLeafes?: TreeNode[] private cachedLeafMessageCount?: number + private cachedLeafCount?: number public unconnectedClone() { const node = new TreeNode() @@ -37,6 +40,7 @@ export class TreeNode { message && this.setMessage(message) this.onMerge.subscribe(() => { this.cachedLeafes = undefined + this.cachedLeafCount = undefined this.cachedLeafMessageCount = undefined this.lastUpdate = Date.now() }) @@ -75,6 +79,7 @@ export class TreeNode { public addEdge(edge: Edge, emitUpdate: boolean = false) { this.edges[edge.name] = edge + this.edgeArray.push(edge) edge.source = this if (emitUpdate) { @@ -102,33 +107,37 @@ export class TreeNode { this.onMerge.dispatch() } - public leafMessageCount() { + public leafMessageCount(): number { if (this.cachedLeafMessageCount === undefined) { - this.cachedLeafMessageCount = this.leafes() - .map(leaf => leaf.messages) - .reduce((a, b) => a + b) + this.cachedLeafMessageCount = this.edgeArray + .map(edge => edge.target.leafMessageCount()) + .reduce((a, b) => a + b, 0) + this.messages } return this.cachedLeafMessageCount } public leafCount(): number { - return this.leafes().length + if (this.cachedLeafCount === undefined) { + this.cachedLeafCount = this.edgeArray + .map(e => e.target.leafCount()) + .reduce((a, b) => a + b, this.edgeArray.length === 0 ? 1 : 0) + } + + return this.cachedLeafCount } public edgeCount(): number { - return Object.values(this.edges).length + return this.edgeArray.length } public leafes(): TreeNode[] { if (this.cachedLeafes === undefined) { - if (Object.values(this.edges).length === 0) { - return [this] - } + const initialValue = this.message && this.message.value ? [this] : [] - this.cachedLeafes = Object.values(this.edges) + this.cachedLeafes = this.edgeArray .map(e => e.target.leafes()) - .reduce((a, b) => a.concat(b), []) + .reduce((a, b) => a.concat(b), initialValue) } return this.cachedLeafes