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

perf: Speedup Dec.Sqrt() #16141

Merged
merged 3 commits into from
May 15, 2023
Merged

perf: Speedup Dec.Sqrt() #16141

merged 3 commits into from
May 15, 2023

Conversation

ValarDragon
Copy link
Contributor

@ValarDragon ValarDragon commented May 13, 2023

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:

BenchmarkLegacySqrtOnMersennePrime-16               23086             51408 ns/op           26730 B/op	     726 allocs/op

New:

BenchmarkLegacySqrtOnMersennePrime-16    	   39177	     28938 ns/op	   11864 B/op	     214 allocs/op

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...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

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...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@ValarDragon ValarDragon added the T: Performance Performance improvements label May 13, 2023

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++ {
Copy link
Contributor Author

@ValarDragon ValarDragon May 13, 2023

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.

@ValarDragon ValarDragon marked this pull request as ready for review May 13, 2023 09:35
@ValarDragon ValarDragon requested a review from a team as a code owner May 13, 2023 09:35
@ValarDragon ValarDragon added the backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release label May 13, 2023
Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

LGTM!

@tac0turtle tac0turtle added this pull request to the merge queue May 15, 2023
Merged via the queue into main with commit fb8ff07 May 15, 2023
@tac0turtle tac0turtle deleted the dev/speedup_sqrt branch May 15, 2023 10:36
mergify bot pushed a commit that referenced this pull request May 15, 2023
(cherry picked from commit fb8ff07)

# Conflicts:
#	math/CHANGELOG.md
@julienrbrt
Copy link
Member

julienrbrt commented May 15, 2023

Do we tag math v1.0.1 with this?

@tac0turtle
Copy link
Member

lets do it

@julienrbrt julienrbrt mentioned this pull request May 15, 2023
19 tasks
odeke-em added a commit that referenced this pull request Sep 19, 2023
…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
odeke-em added a commit that referenced this pull request Oct 1, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release T: Performance Performance improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants