-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
perf: Speedup Dec.Sqrt() #16141
perf: Speedup Dec.Sqrt() #16141
Conversation
|
||
for iter := 0; delta.Abs().GT(LegacySmallestDec()) && iter < maxApproxRootIterations; iter++ { | ||
prev := guess.Power(root - 1) | ||
for iter := 0; delta.AbsMut().GT(smallestDec) && iter < maxApproxRootIterations; iter++ { |
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.
Importantly for square roots, we now have 0 allocations in the hot loop! So the speedup scales with bigger sqrts being taken.
EDIT: 0 allocations outside what the big
package does. QuoMut
does allocations, which is part of why the right shift is a speedup.
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.
LGTM!
(cherry picked from commit fb8ff07) # Conflicts: # math/CHANGELOG.md
Do we tag math v1.0.1 with this? |
lets do it |
…ions This fuzzer performs a roundtrip comparison for square roots comparing the value from: * squaring LegacyDec.ApproxRoot(2) against itself then checking if the value is very close to the original whole value * comparing against the Go standard library's math/big.Float.Sqrt and panicking if we find a deviation of greater than 5% It's great to note that the results from this library so far from fuzzing show a higher precision/accuracy than math/big.Float.Sqrt for example with: LegacyDec.ApproxRoot(100000000000000000.000000000000000000, 2) Stdlib sqrt: 316227766.000000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 316227766.016837933199889354 Python3.7.6: math.sqrt(100000000000000000): 316227766.01683795 LegacyDec.ApproxRoot(10000700000000000.000000000000000000, 2) Stdlib sqrt: 100003499.900000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 100003499.938752143656215533 Python3.7.6: math.sqrt(10000700000000000): 100003499.93875214 Updates PR #16141 Updates PR #17725
…ions This fuzzer performs a roundtrip comparison for square roots comparing the value from: * squaring LegacyDec.ApproxRoot(2) against itself then checking if the value is very close to the original whole value * comparing against the Go standard library's math/big.Float.Sqrt and panicking if we find a deviation of greater than 5% It's great to note that the results from this library so far from fuzzing show a higher precision/accuracy than math/big.Float.Sqrt for example with: LegacyDec.ApproxRoot(100000000000000000.000000000000000000, 2) Stdlib sqrt: 316227766.000000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 316227766.016837933199889354 Python3.7.6: math.sqrt(100000000000000000): 316227766.01683795 LegacyDec.ApproxRoot(10000700000000000.000000000000000000, 2) Stdlib sqrt: 100003499.900000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 100003499.938752143656215533 Python3.7.6: math.sqrt(10000700000000000): 100003499.93875214 Updates PR #16141 Updates PR #17725
Description
Speeds up the Dec.Sqrt() routine which is in a hot loop for Osmosis AMM code.
This is done by lowering heap allocations, through better use of constant re-use, or mutative methods.
Old:
New:
More important than the pure raw time, is the number of allocations, as those induce system overheads in Garbage Collection at scale, that don't get represented in the micro-bench here.
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
to the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
I have...
!
in the type prefix if API or client breaking change