-
Notifications
You must be signed in to change notification settings - Fork 499
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
xdr and exp/orderbook: Reduce path search allocations #4105
xdr and exp/orderbook: Reduce path search allocations #4105
Conversation
bffe014
to
f352677
Compare
exp/orderbook/graph.go
Outdated
@@ -348,10 +352,10 @@ func (graph *OrderBookGraph) FindPaths( | |||
maxAssetsPerPath int, | |||
includePools bool, | |||
) ([]Path, uint32, error) { | |||
destinationAssetString := destinationAsset.String() | |||
destinationAssetString := destinationAsset.StringWithEncoder(graph.strkeyEncoder) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is graph.strkeyEncoder
thread safe? FindPaths()
can be called concurrently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ops, no, it is not. I will make it thread-safe and see if it still performs well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a small critical section in the Find*Paths()
functions
PTAL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@2opremio is there still a performance improvement with the critical section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth it because of the loop.
15d2725
to
18b9aef
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm interested in strkey performance improvements because github.com/stellar/starlight uses strkey.Encode heavily and just looked at them, but I didn't review the other changes. I left a suggestion (💡) where I suggest an alternative optimization that benefits more folks. @2opremio told me this PR is urgent, so none of these comments are blocking if this is urgent, and I defer to Horizon folks for the PR as a whole.
3145ec2
to
fdfc521
Compare
I removed |
@tamirms PTAL |
Replace bytes.Buffer with working with a byte array directly. Also, contains the crc16 improvements by @2opremio that were in #4105. Reduce the allocations in strkey.Encode from 6 to 1, and reduce the heap use from 244 B/op to 64 B/op. In the past I have seen impact to performance of Starlight caused by the strkey.Encode functions. @2opremio also highlighted in #4105 there is an impact there. The use of a bytes.Buffer in this code is rather unnecessary given we know the size of everything, and given that the size of strkeys are small encouraging their data onto the stack is fine. It is worth noting that there is a functional impact by this change. Attempts to strkey.Encode a data value greater than the max payload size as defined by SEP-23 will now error. We've been wanting to add length checks (#1769) for sometime, so I think this is fine, and we can continue to improve those length checks in the future. Before: BenchmarkDecode_accountID-8 2576450 414.2 ns/op 130 B/op 3 allocs/op BenchmarkEncode_accountID-8 3657649 319.6 ns/op 244 B/op 6 allocs/op After: BenchmarkDecode_accountID-8 3563737 334.9 ns/op 128 B/op 2 allocs/op BenchmarkEncode_accountID-8 7365306 165.4 ns/op 64 B/op 1 allocs/op Co-authored-by: Alfonso Acosta <2362916+2opremio@users.noreply.github.com>
Replace bytes.Buffer with working with a byte array directly. Also, contains the crc16 improvements by @2opremio that were in #4105. Reduce the allocations in strkey.Encode from 6 to 1, and reduce the heap use from 244 B/op to 64 B/op. In the past I have seen impact to performance of Starlight caused by the strkey.Encode functions. @2opremio also highlighted in #4105 there is an impact there. The use of a bytes.Buffer in this code is rather unnecessary given we know the size of everything, and given that the size of strkeys are small encouraging their data onto the stack is fine. It is worth noting that there is a functional impact by this change. Attempts to strkey.Encode a data value greater than the max payload size as defined by SEP-23 will now error. We've been wanting to add length checks (#1769) for sometime, so I think this is fine, and we can continue to improve those length checks in the future. Before: BenchmarkDecode_accountID-8 2576450 414.2 ns/op 130 B/op 3 allocs/op BenchmarkEncode_accountID-8 3657649 319.6 ns/op 244 B/op 6 allocs/op After: BenchmarkDecode_accountID-8 3563737 334.9 ns/op 128 B/op 2 allocs/op BenchmarkEncode_accountID-8 7365306 165.4 ns/op 64 B/op 1 allocs/op Co-authored-by: Alfonso Acosta <2362916+2opremio@users.noreply.github.com>
Leftover from stellar#4105
This considerably reduces the allocations of path search by:
pathNode
s through a slab (which piggyback's onappend()
s exponential allocation).Optimizing strkey encoding. This is done in two ways:This will be done at strkey: reduce allocs in Encode #4107 insteada. By creating
strkey.EncodingBuffer
which reuses the internal buffers between calls (much like Create xdr.EncodingBuffer, which reduces buffer allocations #4056 ).b. By optimizing
strkey
encoding in general (there were a few things which were slow or caused uncessary allocartions, like usingfmt.Sprintf()
and using an array to obtain thecrc16
).Before:
After:
This is an ~2.49x allocation reduction and a 20% speedup.
Most of the remaining allocations are removed at #4104
Depends on #4102