Skip to content

Commit

Permalink
Improve performance of path finding implementation (#3818)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms authored Aug 12, 2021
1 parent b55b7d1 commit 65cd406
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
26 changes: 13 additions & 13 deletions exp/orderbook/dfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func dfs(
ctx context.Context,
state searchState,
maxPathLength int,
visited map[string]bool,
visitedList []xdr.Asset,
visited []xdr.Asset,
remainingTerminalNodes int,
currentAssetString string,
currentAsset xdr.Asset,
currentAssetAmount xdr.Int64,
Expand All @@ -76,24 +76,24 @@ func dfs(
if currentAssetAmount <= 0 {
return nil
}
if visited[currentAssetString] {
return nil
}
if len(visitedList) > maxPathLength {
return nil
for _, asset := range visited {
if asset.Equals(currentAsset) {
return nil
}
}
visited[currentAssetString] = true
defer func() {
visited[currentAssetString] = false
}()

updatedVisitedList := append(visitedList, currentAsset)
updatedVisitedList := append(visited, currentAsset)
if state.isTerminalNode(currentAssetString, currentAssetAmount) {
state.appendToPaths(
updatedVisitedList,
currentAssetString,
currentAssetAmount,
)
remainingTerminalNodes--
}
// abort search if we've visited all destination nodes or if we've exceeded maxPathLength
if remainingTerminalNodes == 0 || len(updatedVisitedList) > maxPathLength {
return nil
}

for nextAssetString, offers := range state.edges(currentAssetString) {
Expand All @@ -113,8 +113,8 @@ func dfs(
ctx,
state,
maxPathLength,
visited,
updatedVisitedList,
remainingTerminalNodes,
nextAssetString,
nextAsset,
nextAssetAmount,
Expand Down
4 changes: 2 additions & 2 deletions exp/orderbook/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ func (graph *OrderBookGraph) FindPaths(
ctx,
searchState,
maxPathLength,
map[string]bool{},
[]xdr.Asset{},
len(sourceAssets),
destinationAssetString,
destinationAsset,
destinationAmount,
Expand Down Expand Up @@ -379,8 +379,8 @@ func (graph *OrderBookGraph) FindFixedPaths(
ctx,
searchState,
maxPathLength,
map[string]bool{},
[]xdr.Asset{},
len(destinationAssets),
sourceAsset.String(),
sourceAsset,
amountToSpend,
Expand Down

0 comments on commit 65cd406

Please sign in to comment.