Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph/iterator: allocate reflect map iterator inline #1960

Merged
merged 1 commit into from May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions graph/iterator/lines_map_safe.go
Expand Up @@ -17,7 +17,7 @@ import (
// The iteration order of Lines is randomized.
type Lines struct {
lines reflect.Value
iter *reflect.MapIter
iter reflect.MapIter
pos int
curr graph.Line
}
Expand All @@ -30,7 +30,9 @@ type Lines struct {
// the call to NewLines.
func NewLines(lines map[int64]graph.Line) *Lines {
rv := reflect.ValueOf(lines)
return &Lines{lines: rv, iter: rv.MapRange()}
l := &Lines{lines: rv}
l.iter.Reset(rv)
return l
}

// Len returns the remaining number of lines to be iterated over.
Expand Down Expand Up @@ -61,7 +63,7 @@ func (l *Lines) Line() graph.Line {
func (l *Lines) Reset() {
l.curr = nil
l.pos = 0
l.iter = l.lines.MapRange()
l.iter.Reset(l.lines)
}

// LineSlice returns all the remaining lines in the iterator and advances
Expand All @@ -83,7 +85,7 @@ func (l *Lines) LineSlice() []graph.Line {
// The iteration order of WeightedLines is randomized.
type WeightedLines struct {
lines reflect.Value
iter *reflect.MapIter
iter reflect.MapIter
pos int
curr graph.WeightedLine
}
Expand All @@ -96,7 +98,9 @@ type WeightedLines struct {
// the call to NewWeightedLines.
func NewWeightedLines(lines map[int64]graph.WeightedLine) *WeightedLines {
rv := reflect.ValueOf(lines)
return &WeightedLines{lines: rv, iter: rv.MapRange()}
l := &WeightedLines{lines: rv}
l.iter.Reset(rv)
return l
}

// Len returns the remaining number of lines to be iterated over.
Expand Down Expand Up @@ -127,7 +131,7 @@ func (l *WeightedLines) WeightedLine() graph.WeightedLine {
func (l *WeightedLines) Reset() {
l.curr = nil
l.pos = 0
l.iter = l.lines.MapRange()
l.iter.Reset(l.lines)
}

// WeightedLineSlice returns all the remaining lines in the iterator and advances
Expand Down
26 changes: 18 additions & 8 deletions graph/iterator/nodes_map_safe.go
Expand Up @@ -17,7 +17,7 @@ import (
// The iteration order of Nodes is randomized.
type Nodes struct {
nodes reflect.Value
iter *reflect.MapIter
iter reflect.MapIter
pos int
curr graph.Node
}
Expand All @@ -30,7 +30,9 @@ type Nodes struct {
// the call to NewNodes.
func NewNodes(nodes map[int64]graph.Node) *Nodes {
rv := reflect.ValueOf(nodes)
return &Nodes{nodes: rv, iter: rv.MapRange()}
n := &Nodes{nodes: rv}
n.iter.Reset(rv)
return n
}

// Len returns the remaining number of nodes to be iterated over.
Expand Down Expand Up @@ -84,7 +86,7 @@ func (n *Nodes) NodeSlice() []graph.Node {
type NodesByEdge struct {
nodes map[int64]graph.Node
edges reflect.Value
iter *reflect.MapIter
iter reflect.MapIter
pos int
curr graph.Node
}
Expand All @@ -100,7 +102,9 @@ type NodesByEdge struct {
// is mutated after the call to NewNodes.
func NewNodesByEdge(nodes map[int64]graph.Node, edges map[int64]graph.Edge) *NodesByEdge {
rv := reflect.ValueOf(edges)
return &NodesByEdge{nodes: nodes, edges: rv, iter: rv.MapRange()}
n := &NodesByEdge{nodes: nodes, edges: rv}
n.iter.Reset(rv)
return n
}

// NewNodesByWeightedEdge returns a NodesByEdge initialized with the
Expand All @@ -114,7 +118,9 @@ func NewNodesByEdge(nodes map[int64]graph.Node, edges map[int64]graph.Edge) *Nod
// is mutated after the call to NewNodes.
func NewNodesByWeightedEdge(nodes map[int64]graph.Node, edges map[int64]graph.WeightedEdge) *NodesByEdge {
rv := reflect.ValueOf(edges)
return &NodesByEdge{nodes: nodes, edges: rv, iter: rv.MapRange()}
n := &NodesByEdge{nodes: nodes, edges: rv}
n.iter.Reset(rv)
return n
}

// NewNodesByLines returns a NodesByEdge initialized with the
Expand All @@ -128,7 +134,9 @@ func NewNodesByWeightedEdge(nodes map[int64]graph.Node, edges map[int64]graph.We
// is mutated after the call to NewNodes.
func NewNodesByLines(nodes map[int64]graph.Node, lines map[int64]map[int64]graph.Line) *NodesByEdge {
rv := reflect.ValueOf(lines)
return &NodesByEdge{nodes: nodes, edges: rv, iter: rv.MapRange()}
n := &NodesByEdge{nodes: nodes, edges: rv}
n.iter.Reset(rv)
return n
}

// NewNodesByWeightedLines returns a NodesByEdge initialized with the
Expand All @@ -142,7 +150,9 @@ func NewNodesByLines(nodes map[int64]graph.Node, lines map[int64]map[int64]graph
// is mutated after the call to NewNodes.
func NewNodesByWeightedLines(nodes map[int64]graph.Node, lines map[int64]map[int64]graph.WeightedLine) *NodesByEdge {
rv := reflect.ValueOf(lines)
return &NodesByEdge{nodes: nodes, edges: rv, iter: rv.MapRange()}
n := &NodesByEdge{nodes: nodes, edges: rv}
n.iter.Reset(rv)
return n
}

// Len returns the remaining number of nodes to be iterated over.
Expand Down Expand Up @@ -173,7 +183,7 @@ func (n *NodesByEdge) Node() graph.Node {
func (n *NodesByEdge) Reset() {
n.curr = nil
n.pos = 0
n.iter = n.edges.MapRange()
n.iter.Reset(n.edges)
}

// NodeSlice returns all the remaining nodes in the iterator and advances
Expand Down