Skip to content

Commit

Permalink
Remove redundant appends to optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
snehalyelmati committed Jun 5, 2022
1 parent 3b0ad09 commit b64a43b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
7 changes: 2 additions & 5 deletions edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ func (e Edge) Encode() string {
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
}

s = append(s, "{")
s = append(s, strings.Join(p, ","))
s = append(s, "}")
s = append(s, "{", strings.Join(p, ","), "}")
}

s = append(s, "]->")
s = append(s, "(", e.Destination.Alias, ")")
s = append(s, "]->", "(", e.Destination.Alias, ")")

return strings.Join(s, "")
}
4 changes: 1 addition & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ func (n Node) Encode() string {
p = append(p, fmt.Sprintf("%s:%v", k, ToString(v)))
}

s = append(s, "{")
s = append(s, strings.Join(p, ","))
s = append(s, "}")
s = append(s, "{", strings.Join(p, ","), "}")
}

s = append(s, ")")
Expand Down
19 changes: 9 additions & 10 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func PathNew(nodes []interface{}, edges []interface{}) Path {
for i := 0; i < len(edges); i++ {
Edges[i] = edges[i].(*Edge)
}
return Path{
Edges : Edges,
Nodes : Nodes,

return Path{
Edges: Edges,
Nodes: Nodes,
}
}

Expand All @@ -38,7 +38,7 @@ func (p Path) GetNode(index int) *Node {
return p.Nodes[index]
}

func (p Path) GetEdge(index int) *Edge{
func (p Path) GetEdge(index int) *Edge {
return p.Edges[index]
}

Expand All @@ -63,16 +63,15 @@ func (p Path) String() string {
edgeCount := p.EdgeCount()
for i := 0; i < edgeCount; i++ {
var node = p.GetNode(i)
s = append(s, "(" , fmt.Sprintf("%v", node.ID) , ")")
s = append(s, "(", fmt.Sprintf("%v", node.ID), ")")
var edge = p.GetEdge(i)
if node.ID == edge.srcNodeID {
s = append(s, "-[" , fmt.Sprintf("%v", edge.ID) , "]->")
s = append(s, "-[", fmt.Sprintf("%v", edge.ID), "]->")
} else {
s= append(s, "<-[" , fmt.Sprintf("%v", edge.ID) , "]-")
s = append(s, "<-[", fmt.Sprintf("%v", edge.ID), "]-")
}
}
s = append(s, "(" , fmt.Sprintf("%v", p.GetNode(edgeCount).ID) , ")")
s = append(s, ">")
s = append(s, "(", fmt.Sprintf("%v", p.GetNode(edgeCount).ID), ")", ">")

return strings.Join(s, "")
}

0 comments on commit b64a43b

Please sign in to comment.