Increase tree statistic performance

Fixes #44
This commit is contained in:
Thomas Nordquist
2019-01-22 14:04:49 +01:00
parent c2345a17ba
commit bcd5a32483
3 changed files with 23 additions and 14 deletions

View File

@@ -32,7 +32,7 @@ class TreeNodeSubnodes extends React.Component<Props, State> {
private sortedNodes(): q.TreeNode[] { private sortedNodes(): q.TreeNode[] {
const { topicOrder, treeNode } = this.props const { topicOrder, treeNode } = this.props
let edges = Object.values(treeNode.edges) let edges = treeNode.edgeArray
if (topicOrder === TopicOrder.abc) { if (topicOrder === TopicOrder.abc) {
edges = edges.sort((a, b) => a.name.localeCompare(b.name)) edges = edges.sort((a, b) => a.name.localeCompare(b.name))
} }
@@ -59,7 +59,7 @@ class TreeNodeSubnodes extends React.Component<Props, State> {
} }
public render() { public render() {
const edges = Object.values(this.props.treeNode.edges) const edges = this.props.treeNode.edgeArray
if (edges.length === 0 || this.props.collapsed) { if (edges.length === 0 || this.props.collapsed) {
return null return null
} }

View File

@@ -13,7 +13,7 @@ export class Edge implements Hashable {
} }
public edges() { public edges() {
return this.target ? Object.values(this.target.edges) : [] return this.target ? this.target.edgeArray : []
} }
public hash(): string { public hash(): string {

View File

@@ -7,6 +7,7 @@ export class TreeNode {
public mqttMessage?: MqttMessage public mqttMessage?: MqttMessage
public messageHistory: RingBuffer<Message> = new RingBuffer<Message>(3000, 100) public messageHistory: RingBuffer<Message> = new RingBuffer<Message>(3000, 100)
public edges: {[s: string]: Edge} = {} public edges: {[s: string]: Edge} = {}
public edgeArray: Edge[] = []
public collapsed = false public collapsed = false
public messages: number = 0 public messages: number = 0
public lastUpdate: number = Date.now() public lastUpdate: number = Date.now()
@@ -14,8 +15,10 @@ export class TreeNode {
public onEdgesChange = new EventDispatcher<void, TreeNode>(this) public onEdgesChange = new EventDispatcher<void, TreeNode>(this)
public onMessage = new EventDispatcher<Message, TreeNode>(this) public onMessage = new EventDispatcher<Message, TreeNode>(this)
public isTree = false public isTree = false
private cachedLeafes?: TreeNode[] private cachedLeafes?: TreeNode[]
private cachedLeafMessageCount?: number private cachedLeafMessageCount?: number
private cachedLeafCount?: number
public unconnectedClone() { public unconnectedClone() {
const node = new TreeNode() const node = new TreeNode()
@@ -37,6 +40,7 @@ export class TreeNode {
message && this.setMessage(message) message && this.setMessage(message)
this.onMerge.subscribe(() => { this.onMerge.subscribe(() => {
this.cachedLeafes = undefined this.cachedLeafes = undefined
this.cachedLeafCount = undefined
this.cachedLeafMessageCount = undefined this.cachedLeafMessageCount = undefined
this.lastUpdate = Date.now() this.lastUpdate = Date.now()
}) })
@@ -75,6 +79,7 @@ export class TreeNode {
public addEdge(edge: Edge, emitUpdate: boolean = false) { public addEdge(edge: Edge, emitUpdate: boolean = false) {
this.edges[edge.name] = edge this.edges[edge.name] = edge
this.edgeArray.push(edge)
edge.source = this edge.source = this
if (emitUpdate) { if (emitUpdate) {
@@ -102,33 +107,37 @@ export class TreeNode {
this.onMerge.dispatch() this.onMerge.dispatch()
} }
public leafMessageCount() { public leafMessageCount(): number {
if (this.cachedLeafMessageCount === undefined) { if (this.cachedLeafMessageCount === undefined) {
this.cachedLeafMessageCount = this.leafes() this.cachedLeafMessageCount = this.edgeArray
.map(leaf => leaf.messages) .map(edge => edge.target.leafMessageCount())
.reduce((a, b) => a + b) .reduce((a, b) => a + b, 0) + this.messages
} }
return this.cachedLeafMessageCount return this.cachedLeafMessageCount
} }
public leafCount(): number { 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 { public edgeCount(): number {
return Object.values(this.edges).length return this.edgeArray.length
} }
public leafes(): TreeNode[] { public leafes(): TreeNode[] {
if (this.cachedLeafes === undefined) { if (this.cachedLeafes === undefined) {
if (Object.values(this.edges).length === 0) { const initialValue = this.message && this.message.value ? [this] : []
return [this]
}
this.cachedLeafes = Object.values(this.edges) this.cachedLeafes = this.edgeArray
.map(e => e.target.leafes()) .map(e => e.target.leafes())
.reduce((a, b) => a.concat(b), []) .reduce((a, b) => a.concat(b), initialValue)
} }
return this.cachedLeafes return this.cachedLeafes