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: Speed up path finding by changing edgeSet data structure #3965

Merged
merged 5 commits into from
Sep 29, 2021

Conversation

tamirms
Copy link
Contributor

@tamirms tamirms commented Sep 28, 2021

I did some profiling on path finding and found that iterating through entries in the edgeSet map was particularly expensive. We never use edgeSet to do map lookups in the dfs. We only use the data structure to iterate through the key value pairs. So I changed the representation to a pair of lists and that resulted in a significant improvement in the path finding performance.

Before applying the changes in this commit:

goos: darwin
goarch: amd64
pkg: github.com/stellar/go/exp/orderbook
cpu: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
BenchmarkMultipleDestinationAssets
BenchmarkMultipleDestinationAssets-12    	       3	 417991332 ns/op
PASS

After applying the changes in this commit:

goos: darwin
goarch: amd64
pkg: github.com/stellar/go/exp/orderbook
cpu: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
BenchmarkMultipleDestinationAssets
BenchmarkMultipleDestinationAssets-12    	       4	 300770334 ns/op
PASS

@tamirms tamirms requested a review from a team September 28, 2021 21:42
Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never use edgeSet to do map lookups in the dfs.

I think I passively observed this during the latest changeset, but was too deep in a refactor to give it proper consideration. Super exciting to see a follow-up on it! 🎉

A few minor DRY-related comments and other questions below ⬇️

exp/orderbook/edges.go Outdated Show resolved Hide resolved
exp/orderbook/graph.go Outdated Show resolved Hide resolved
exp/orderbook/edges.go Outdated Show resolved Hide resolved

offers := e.values[i].offers
updatedOffers := offers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we modify offers directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to modify offers while iterating through it in the for i, offer := range offers loop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point, I see. It won't keep iterating after it's modified since it breaks immediately, but I can see why you would do this to avoid confusion when reading the code.

exp/orderbook/graph.go Show resolved Hide resolved
exp/orderbook/graph_test.go Outdated Show resolved Hide resolved
@tamirms tamirms requested a review from Shaptic September 29, 2021 12:10
Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I'm curious if you saw a boost after going for the []edge approach?


offers := e.values[i].offers
updatedOffers := offers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point, I see. It won't keep iterating after it's modified since it breaks immediately, but I can see why you would do this to avoid confusion when reading the code.

exp/orderbook/graph.go Show resolved Hide resolved

// addOffer will insert the given offer into the edge set
func (e edgeSet) addOffer(key string, offer xdr.OfferEntry) {
func (e edgeSet) addOffer(key string, offer xdr.OfferEntry) edgeSet {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever, great idea 👍

@tamirms
Copy link
Contributor Author

tamirms commented Sep 29, 2021

I'm curious if you saw a boost after going for the []edge approach?

There wasn't a noticeable difference but the code was cleaner using an edge struct so I kept it.

@tamirms tamirms merged commit 10446da into stellar:release-horizon-v2.9.0 Sep 29, 2021
@tamirms tamirms deleted the edge-set branch September 29, 2021 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants