From f8fdb6de3f9bf2b2664ca845954094224b63369a Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Wed, 5 Jan 2022 11:28:47 +0000 Subject: [PATCH] do not use pointer addr strings as map keys in set When creating a Set of BasicEdges, the Hashcode function is used to determine map keys for the underlying set data structure. The string hex representation of the two vertices' pointers is unsafe to use as a map key, since these addresses may change between the time they are added to the set and the time the set is operated on. Instead we modify the Hashcode function to maintain the references to the underlying vertices so they cannot be garbage collected during the lifetime of the Set. --- internal/dag/edge.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/internal/dag/edge.go b/internal/dag/edge.go index f0d99ee3a..8c78924bb 100644 --- a/internal/dag/edge.go +++ b/internal/dag/edge.go @@ -1,9 +1,5 @@ package dag -import ( - "fmt" -) - // Edge represents an edge in the graph, with a source and target vertex. type Edge interface { Source() Vertex @@ -25,7 +21,7 @@ type basicEdge struct { } func (e *basicEdge) Hashcode() interface{} { - return fmt.Sprintf("%p-%p", e.S, e.T) + return [...]interface{}{e.S, e.T} } func (e *basicEdge) Source() Vertex {