From e8e4638a2197fb78d42bfbf6f28dbb1314c8a230 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Tue, 23 Jul 2019 18:12:43 +0930 Subject: [PATCH] graph/multi: don't allocate line maps before they are needed --- graph/multi/directed.go | 25 ++++++++++++++++--------- graph/multi/undirected.go | 24 ++++++++++++++++-------- graph/multi/weighted_directed.go | 25 ++++++++++++++++--------- graph/multi/weighted_undirected.go | 24 ++++++++++++++++-------- 4 files changed, 64 insertions(+), 34 deletions(-) diff --git a/graph/multi/directed.go b/graph/multi/directed.go index b5caf048..9f9f00b2 100644 --- a/graph/multi/directed.go +++ b/graph/multi/directed.go @@ -53,8 +53,6 @@ func (g *DirectedGraph) AddNode(n graph.Node) { panic(fmt.Sprintf("simple: node ID collision: %d", n.ID())) } g.nodes[n.ID()] = n - g.from[n.ID()] = make(map[int64]map[int64]graph.Line) - g.to[n.ID()] = make(map[int64]map[int64]graph.Line) g.nodeIDs.Use(n.ID()) } @@ -242,20 +240,29 @@ func (g *DirectedGraph) SetLine(l graph.Line) { } else { g.nodes[fid] = from } - if g.from[fid][tid] == nil { - g.from[fid][tid] = make(map[int64]graph.Line) - } if _, ok := g.nodes[tid]; !ok { g.AddNode(to) } else { g.nodes[tid] = to } - if g.to[tid][fid] == nil { - g.to[tid][fid] = make(map[int64]graph.Line) + + switch { + case g.from[fid] == nil: + g.from[fid] = map[int64]map[int64]graph.Line{tid: map[int64]graph.Line{lid: l}} + case g.from[fid][tid] == nil: + g.from[fid][tid] = map[int64]graph.Line{lid: l} + default: + g.from[fid][tid][lid] = l + } + switch { + case g.to[tid] == nil: + g.to[tid] = map[int64]map[int64]graph.Line{fid: map[int64]graph.Line{lid: l}} + case g.to[tid][fid] == nil: + g.to[tid][fid] = map[int64]graph.Line{lid: l} + default: + g.to[tid][fid][lid] = l } - g.from[fid][tid][lid] = l - g.to[tid][fid][lid] = l g.lineIDs.Use(lid) } diff --git a/graph/multi/undirected.go b/graph/multi/undirected.go index fac8462f..b735c106 100644 --- a/graph/multi/undirected.go +++ b/graph/multi/undirected.go @@ -51,7 +51,6 @@ func (g *UndirectedGraph) AddNode(n graph.Node) { panic(fmt.Sprintf("simple: node ID collision: %d", n.ID())) } g.nodes[n.ID()] = n - g.lines[n.ID()] = make(map[int64]map[int64]graph.Line) g.nodeIDs.Use(n.ID()) } @@ -242,19 +241,28 @@ func (g *UndirectedGraph) SetLine(l graph.Line) { } else { g.nodes[fid] = from } - if g.lines[fid][tid] == nil { - g.lines[fid][tid] = make(map[int64]graph.Line) - } if _, ok := g.nodes[tid]; !ok { g.AddNode(to) } else { g.nodes[tid] = to } - if g.lines[tid][fid] == nil { - g.lines[tid][fid] = make(map[int64]graph.Line) + + switch { + case g.lines[fid] == nil: + g.lines[fid] = map[int64]map[int64]graph.Line{tid: map[int64]graph.Line{lid: l}} + case g.lines[fid][tid] == nil: + g.lines[fid][tid] = map[int64]graph.Line{lid: l} + default: + g.lines[fid][tid][lid] = l + } + switch { + case g.lines[tid] == nil: + g.lines[tid] = map[int64]map[int64]graph.Line{fid: map[int64]graph.Line{lid: l}} + case g.lines[tid][fid] == nil: + g.lines[tid][fid] = map[int64]graph.Line{lid: l} + default: + g.lines[tid][fid][lid] = l } - g.lines[fid][tid][lid] = l - g.lines[tid][fid][lid] = l g.lineIDs.Use(lid) } diff --git a/graph/multi/weighted_directed.go b/graph/multi/weighted_directed.go index 2cbec87b..7b4ad30e 100644 --- a/graph/multi/weighted_directed.go +++ b/graph/multi/weighted_directed.go @@ -62,8 +62,6 @@ func (g *WeightedDirectedGraph) AddNode(n graph.Node) { panic(fmt.Sprintf("simple: node ID collision: %d", n.ID())) } g.nodes[n.ID()] = n - g.from[n.ID()] = make(map[int64]map[int64]graph.WeightedLine) - g.to[n.ID()] = make(map[int64]map[int64]graph.WeightedLine) g.nodeIDs.Use(n.ID()) } @@ -251,20 +249,29 @@ func (g *WeightedDirectedGraph) SetWeightedLine(l graph.WeightedLine) { } else { g.nodes[fid] = from } - if g.from[fid][tid] == nil { - g.from[fid][tid] = make(map[int64]graph.WeightedLine) - } if _, ok := g.nodes[tid]; !ok { g.AddNode(to) } else { g.nodes[tid] = to } - if g.to[tid][fid] == nil { - g.to[tid][fid] = make(map[int64]graph.WeightedLine) + + switch { + case g.from[fid] == nil: + g.from[fid] = map[int64]map[int64]graph.WeightedLine{tid: map[int64]graph.WeightedLine{lid: l}} + case g.from[fid][tid] == nil: + g.from[fid][tid] = map[int64]graph.WeightedLine{lid: l} + default: + g.from[fid][tid][lid] = l + } + switch { + case g.to[tid] == nil: + g.to[tid] = map[int64]map[int64]graph.WeightedLine{fid: map[int64]graph.WeightedLine{lid: l}} + case g.to[tid][fid] == nil: + g.to[tid][fid] = map[int64]graph.WeightedLine{lid: l} + default: + g.to[tid][fid][lid] = l } - g.from[fid][tid][lid] = l - g.to[tid][fid][lid] = l g.lineIDs.Use(l.ID()) } diff --git a/graph/multi/weighted_undirected.go b/graph/multi/weighted_undirected.go index bce322f6..32178143 100644 --- a/graph/multi/weighted_undirected.go +++ b/graph/multi/weighted_undirected.go @@ -60,7 +60,6 @@ func (g *WeightedUndirectedGraph) AddNode(n graph.Node) { panic(fmt.Sprintf("simple: node ID collision: %d", n.ID())) } g.nodes[n.ID()] = n - g.lines[n.ID()] = make(map[int64]map[int64]graph.WeightedLine) g.nodeIDs.Use(n.ID()) } @@ -252,20 +251,29 @@ func (g *WeightedUndirectedGraph) SetWeightedLine(l graph.WeightedLine) { } else { g.nodes[fid] = from } - if g.lines[fid][tid] == nil { - g.lines[fid][tid] = make(map[int64]graph.WeightedLine) - } if _, ok := g.nodes[tid]; !ok { g.AddNode(to) } else { g.nodes[tid] = to } - if g.lines[tid][fid] == nil { - g.lines[tid][fid] = make(map[int64]graph.WeightedLine) + + switch { + case g.lines[fid] == nil: + g.lines[fid] = map[int64]map[int64]graph.WeightedLine{tid: map[int64]graph.WeightedLine{lid: l}} + case g.lines[fid][tid] == nil: + g.lines[fid][tid] = map[int64]graph.WeightedLine{lid: l} + default: + g.lines[fid][tid][lid] = l + } + switch { + case g.lines[tid] == nil: + g.lines[tid] = map[int64]map[int64]graph.WeightedLine{fid: map[int64]graph.WeightedLine{lid: l}} + case g.lines[tid][fid] == nil: + g.lines[tid][fid] = map[int64]graph.WeightedLine{lid: l} + default: + g.lines[tid][fid][lid] = l } - g.lines[fid][tid][lid] = l - g.lines[tid][fid][lid] = l g.lineIDs.Use(lid) }