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

[move] add standard math library #3172

Merged
merged 2 commits into from Sep 12, 2022
Merged

Conversation

0xhawk
Copy link
Contributor

@0xhawk 0xhawk commented Aug 19, 2022

Description

Adds a math standard library. There are currently four functions; max, min, average, and pow. These will be helpful, especially for DeFi builders on Aptos Network to calculate something.

Test Plan

In the file math.move.


This change is Reviewable

@davidiw davidiw requested review from movekevin and wrwg August 21, 2022 03:25

/// Return the average of two.
public fun average(a: u64, b: u64): u64 {
(a & b) + (a ^ b) / 2
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we really need the gas optimization here vs just doing (a + b) / 2

Copy link
Contributor

Choose a reason for hiding this comment

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

Especially as a ^ b can overflow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got your point

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a + b has the overflow risk other than the original solution.
let's do (a & b) + (a ^ b) >> 1 using bit ops for the whole expression.

module aptos_std::math {

/// Return the largest of two numbers.
public fun max(a: u64, b: u64): u64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about u128? And even if you don't introduce now, perhaps call these here max_u64 et. al?

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, we can also put this into the module name, as in math64::max et. al

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 love the way to split the file into the two

@0xhawk 0xhawk requested a review from davidiw as a code owner August 23, 2022 05:15
Comment on lines +27 to +33
p = p * p;
if (e % 2 == 1) {
p = p * n;
p
} else {
p
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
p = p * p;
if (e % 2 == 1) {
p = p * n;
p
} else {
p
}
if (e % 2 == 1) {
p * p * n
} else {
p * p
}

I wonder which saves more gas?

Copy link
Contributor

@lightmark lightmark left a comment

Choose a reason for hiding this comment

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

Synced with @movekevin please fix the average function. Sorry for the inconvenience.

@lightmark
Copy link
Contributor

gonna land it for now. I'll fix it later.

@github-actions
Copy link
Contributor

Forge is running with 120975c00e726bf57e12661cb34271444f9d487a

❌ Forge test failure on 120975c00e726bf57e12661cb34271444f9d487a

Forge test runner terminated

Forge is running with 120975c00e726bf57e12661cb34271444f9d487a

❌ Forge test failure on 120975c00e726bf57e12661cb34271444f9d487a

Forge test runner terminated

Forge is running suite land_blocking on c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c

Forge is running suite compat on testnet ==> c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c

✅ Forge suite land_blocking success on c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c

performance benchmark with full nodes : 7684 TPS, 3858 ms latency, 5400 ms p99 latency,no expired txns
Test Ok

✅ Forge suite compat success on testnet ==> c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c

Compatibility test results for testnet ==> c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c (PR)
1. Check liveness of validators at old version: testnet
compatibility::simple-validator-upgrade::liveness-check : 7511 TPS, 3947 ms latency, 6500 ms p99 latency,no expired txns
2. Upgrading first Validator to new version: c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c
compatibility::simple-validator-upgrade::single-validator-upgrade : 5924 TPS, 4571 ms latency, 6500 ms p99 latency,no expired txns
3. Upgrading rest of first batch to new version: c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c
compatibility::simple-validator-upgrade::half-validator-upgrade : 5819 TPS, 4776 ms latency, 6200 ms p99 latency,no expired txns
4. upgrading second batch to new version: c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c
compatibility::simple-validator-upgrade::rest-validator-upgrade : 5951 TPS, 4108 ms latency, 7700 ms p99 latency,no expired txns
5. check swarm health
Compatibility test for testnet ==> c5fb4f8f760bde1c93bc0c3938ecfa146e95cb8c passed
Test Ok

@lightmark lightmark merged commit cd38549 into aptos-labs:main Sep 12, 2022
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.

None yet

4 participants