Skip to content

Commit

Permalink
routing: use nodeWithDist instead of vertex to avoid map access
Browse files Browse the repository at this point in the history
The same nodeWithDist was fetched from the map for every channel it has.
This struct is not mutated so it can be fetched before and reused.
  • Loading branch information
champo committed Aug 28, 2019
1 parent 18fd12c commit 930a9b8
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions routing/pathfind.go
Expand Up @@ -406,7 +406,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
// processEdge is a helper closure that will be used to make sure edges
// satisfy our specific requirements.
processEdge := func(fromVertex route.Vertex, bandwidth lnwire.MilliSatoshi,
edge *channeldb.ChannelEdgePolicy, toNode route.Vertex) {
edge *channeldb.ChannelEdgePolicy, toNodeDist *nodeWithDist) {

// If this is not a local channel and it is disabled, we will
// skip it.
Expand All @@ -431,17 +431,16 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,

// Calculate amount that the candidate node would have to sent
// out.
toNodeDist := distance[toNode]
amountToSend := toNodeDist.amountToReceive

// Request the success probability for this edge.
edgeProbability := r.ProbabilitySource(
fromVertex, toNode, amountToSend,
fromVertex, toNodeDist.node, amountToSend,
)

if log.Level() <= btclog.LevelTrace {
log.Tracef("path finding probability: fromnode=%v, tonode=%v, "+
"probability=%v", fromVertex, toNode, edgeProbability)
"probability=%v", fromVertex, toNodeDist.node, edgeProbability)
}

// If the probability is zero, there is no point in trying.
Expand Down Expand Up @@ -588,6 +587,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
// from the heap.
partialPath := heap.Pop(&nodeHeap).(*nodeWithDist)
pivot := partialPath.node
pivotWithDist := distance[pivot]

// If we've reached our source (or we don't have any incoming
// edges), then we're done here and can exit the graph
Expand Down Expand Up @@ -636,7 +636,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,

// Check if this candidate node is better than what we
// already have.
processEdge(chanSource, edgeBandwidth, inEdge, pivot)
processEdge(chanSource, edgeBandwidth, inEdge, pivotWithDist)
return nil
}

Expand All @@ -656,7 +656,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
bandWidth := distance[pivot].amountToReceive
for _, reverseEdge := range additionalEdgesWithSrc[pivot] {
processEdge(reverseEdge.sourceNode, bandWidth,
reverseEdge.edge, pivot)
reverseEdge.edge, pivotWithDist)
}
}

Expand Down

0 comments on commit 930a9b8

Please sign in to comment.