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

exp/orderbook: Improve performance of path finding implementation #3818

Merged
merged 1 commit into from
Aug 12, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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