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

Optimise the balance function #1771

Merged
merged 1 commit into from
Aug 11, 2020
Merged

Optimise the balance function #1771

merged 1 commit into from
Aug 11, 2020

Conversation

mrBliss
Copy link
Contributor

@mrBliss mrBliss commented Aug 11, 2020

I stumbled upon it by accident and couldn't help myself. This is probably just a
drop in the bucket.

Microbenchmarks with an UTxO with 1,000,000 entries:

Before:

time                 47.36 ms   (45.38 ms .. 48.76 ms)
                     0.997 R²   (0.994 R² .. 0.999 R²)
mean                 47.86 ms   (47.23 ms .. 48.89 ms)
std dev              1.582 ms   (1.131 ms .. 2.370 ms)

After:

time                 18.72 ms   (18.53 ms .. 18.91 ms)
                     0.999 R²   (0.998 R² .. 1.000 R²)
mean                 18.69 ms   (18.58 ms .. 18.86 ms)
std dev              305.1 μs   (233.6 μs .. 431.3 μs)

I stumbled upon it by accident and couldn't help myself. This is probably just a
drop in the bucket.

Microbenchmarks with an UTxO with 1,000,000 entries:

Before:

    time                 47.36 ms   (45.38 ms .. 48.76 ms)
                         0.997 R²   (0.994 R² .. 0.999 R²)
    mean                 47.86 ms   (47.23 ms .. 48.89 ms)
    std dev              1.582 ms   (1.131 ms .. 2.370 ms)

After:

    time                 18.72 ms   (18.53 ms .. 18.91 ms)
                         0.999 R²   (0.998 R² .. 1.000 R²)
    mean                 18.69 ms   (18.58 ms .. 18.86 ms)
    std dev              305.1 μs   (233.6 μs .. 431.3 μs)
balance :: Crypto crypto => UTxO crypto -> Coin
balance (UTxO utxo) = foldr addCoins 0 utxo
balance :: UTxO crypto -> Coin
balance (UTxO utxo) = fromIntegral $ Map.foldl' addCoins 0 utxo
Copy link
Contributor Author

@mrBliss mrBliss Aug 11, 2020

Choose a reason for hiding this comment

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

Note that I use Word64 to sum them and only convert to Coin at the end. Is this fine or do we expect an overflow?

Copy link
Contributor

Choose a reason for hiding this comment

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

a property of the system is that there are never more than 45 * 10 ^ 15 coins in this map, so 2 ^ 64 is more that enough. that said, if there ever were a bug, this could obfuscate the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

a property of the system is that there are never more than 45 * 10 ^ 15 coins in this map, so 2 ^ 64 is more that enough.

I see.

that said, if there ever were a bug, this could obfuscate the problem.

So what do you prefer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, when summing using Coin instead of Word64:

time                 36.17 ms   (35.04 ms .. 37.35 ms)
                     0.998 R²   (0.996 R² .. 0.999 R²)
mean                 36.61 ms   (36.15 ms .. 37.22 ms)
std dev              1.112 ms   (812.8 μs .. 1.637 ms)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a strong preference, I'm just making sure we all have the same understanding. Thank you for helping out with the performance @mrBliss !

@mrBliss mrBliss merged commit b82a339 into master Aug 11, 2020
@iohk-bors iohk-bors bot deleted the mrBliss/optimise-balance branch August 11, 2020 13:17
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