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

Change the return type of mulDiv to std::optional #4243

Merged
merged 1 commit into from
Jul 6, 2023

Conversation

ckeshava
Copy link
Collaborator

@ckeshava
Copy link
Collaborator Author

I had to use std::numeric_limits<std::uint64_t>::max() in a few places to return a default value (in case of an overflow). Do I need to store this value in a variable for better readability? Or is it okay to use this verbose expression?

src/ripple/basics/mulDiv.h Outdated Show resolved Hide resolved
src/ripple/basics/impl/mulDiv.cpp Outdated Show resolved Hide resolved
src/ripple/app/misc/impl/TxQ.cpp Outdated Show resolved Hide resolved
src/ripple/app/misc/impl/TxQ.cpp Outdated Show resolved Hide resolved
src/ripple/app/misc/TxQ.h Outdated Show resolved Hide resolved
src/ripple/app/misc/TxQ.h Outdated Show resolved Hide resolved
src/ripple/app/tx/impl/DeleteAccount.cpp Outdated Show resolved Hide resolved
src/ripple/ledger/ReadView.h Outdated Show resolved Hide resolved
src/ripple/app/misc/TxQ.h Outdated Show resolved Hide resolved
src/ripple/app/misc/impl/TxQ.cpp Outdated Show resolved Hide resolved
src/ripple/app/tx/impl/DeleteAccount.cpp Outdated Show resolved Hide resolved
@greg7mdp
Copy link
Contributor

Nice, thanks @chennakeshava1998 , but I don't think it is necessary to leave the old code commented out.

@ckeshava
Copy link
Collaborator Author

@greg7mdp Sorry my bad. I forgot to remove it. Completed now 👍

Copy link
Contributor

@greg7mdp greg7mdp left a comment

Choose a reason for hiding this comment

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

Almost there!

src/ripple/app/misc/TxQ.h Outdated Show resolved Hide resolved
@ckeshava
Copy link
Collaborator Author

Almost there!

Done :)
I have also removed extra import statements and refactored one more if-else condition into a value_or statement.

Do I need to clean up the commits? (I can do a force push with a single commit)

@greg7mdp
Copy link
Contributor

greg7mdp commented Jul 21, 2022

Nice! Looks very clean now.

Do I need to clean up the commits? (I can do a force push with a single commit)

Yes, I think it would be better.

Make sure to run the unittests again with the latest version, if you haven't already.

@ckeshava ckeshava force-pushed the changeMulDiv branch 2 times, most recently from 3c3c6df to ed9f30a Compare July 21, 2022 20:33
@ckeshava
Copy link
Collaborator Author

I have executed the unit tests, I didn't observe any failures. 👍

Copy link
Contributor

@greg7mdp greg7mdp left a comment

Choose a reason for hiding this comment

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

Looks great!

@greg7mdp
Copy link
Contributor

I approved the PR, but I don't have write access to the repo, so it doesn't count.

@ckeshava
Copy link
Collaborator Author

Okat, thanks for helping me out greg!

@intelliot
Copy link
Collaborator

@ckeshava could you merge develop into your branch, please?

@ckeshava
Copy link
Collaborator Author

Hello @intelliot ,
Sure, I'd love to merge it and clean up the commit. Is this a priority issue? I completed this code change as a part of my summer internship last year.

I do not have C++ toolchain required to install the dependencies and unit-test this change. Can I do this after 2-3 weeks?

@intelliot
Copy link
Collaborator

No hurry - thanks!

@intelliot intelliot requested a review from HowardHinnant April 3, 2023 05:43
@HowardHinnant
Copy link
Contributor

@ckeshava Could you rebase this against the current develop? I tried to do so and got multiple merge conflicts. And I don't want to try to guess the correct conflict resolutions.

I need it rebased because we've recently moved to conan for a build tool, and your PR won't build with Conan without a rebase.

@ckeshava
Copy link
Collaborator Author

ckeshava commented Apr 4, 2023

@HowardHinnant Will do 👍

Copy link
Contributor

@HowardHinnant HowardHinnant left a comment

Choose a reason for hiding this comment

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

Needs a rebase to develop (has merge conflicts).

@ckeshava
Copy link
Collaborator Author

@HowardHinnant I have merged the latest develop into this feature branch.

@intelliot
Copy link
Collaborator

intelliot commented Apr 21, 2023

you can download the clang-format patch from https://github.com/XRPLF/rippled/actions/runs/4759912341?pr=4243 (under Artifacts) and apply it with git apply <patch file name>

@ckeshava
Copy link
Collaborator Author

thanks a lot elliot, I was searching for exactly that :))

@ckeshava
Copy link
Collaborator Author

Hello,
I have completed the code changes for this PR. @HowardHinnant @greg7mdp

@intelliot intelliot requested a review from drlongle April 26, 2023 05:36
@HowardHinnant
Copy link
Contributor

I have requested changes to the git merge which place these commits at the top.

@ckeshava
Copy link
Collaborator Author

I'll work on your suggestions, sorry I forgot about it.

@ckeshava
Copy link
Collaborator Author

ckeshava commented Jun 5, 2023

Hello @HowardHinnant ,
Thank you so much for your detailed help with reorganizing the git commits, it was really helpful.
Sorry for the delay in updating this PR, I got occupied with another PR.

Is there a better alternative than force-pushing the changes? After rebasing, the remote and local branches were so different that I couldn't think of a better alternative.

@HowardHinnant
Copy link
Contributor

Hello @HowardHinnant , Thank you so much for your detailed help with reorganizing the git commits, it was really helpful. Sorry for the delay in updating this PR, I got occupied with another PR.

Is there a better alternative than force-pushing the changes? After rebasing, the remote and local branches were so different that I couldn't think of a better alternative.

I think force pushing is the only option here. Thanks for this work. It is on my to-do list to review.

@@ -2056,7 +2056,7 @@ NetworkOPsImp::pubServer()
f.em->openLedgerFeeLevel,
f.loadBaseServer,
f.em->referenceFeeLevel)
.second);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are cases like this guaranteed to have a value? Is that why the old code didn't check for success? If so, then I think the new code should just use operator*.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cases like this are not guaranteed to have a value. When the old behavior overflowed, it passed back an overflow flag and the value ripple::muldiv_max. The client level code (in this example) didn't check the overflow flag and just took the value. In this rewrite, the client code is forced to be explicit that it is using ripple::muldiv_max on overflow.

In any case the behavior is unchanged.

src/ripple/basics/mulDiv.h Outdated Show resolved Hide resolved
@thejohnfreeman
Copy link
Collaborator

CI caught a format issue. Instructions on how to fix it are in the job log.

@ckeshava
Copy link
Collaborator Author

okay, I've fixed that. let me know if I need to squash or clean-up the commit log

@thejohnfreeman
Copy link
Collaborator

It will be squashed when it is merged. Now that there are at least two approvals and all checks pass, you can add the "Passed" label if you think this PR is ready to merge.

@ckeshava ckeshava self-assigned this Jun 15, 2023
@ckeshava ckeshava added the Passed Passed code review & PR owner thinks it's ready to merge. Perf sign-off may still be required. label Jun 15, 2023
@ckeshava
Copy link
Collaborator Author

It will be squashed when it is merged. Now that there are at least two approvals and all checks pass, you can add the "Passed" label if you think this PR is ready to merge.

Okay, done. thanks

@intelliot intelliot removed the request for review from drlongle July 4, 2023 00:17
@intelliot
Copy link
Collaborator

Suggested commit message:

refactor: change the return type of mulDiv to std::optional (#4243)

- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix #3495

---------

Co-authored-by: John Freeman <[email protected]>

- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix XRPLF#3495

---------

Co-authored-by: John Freeman <[email protected]>
@intelliot
Copy link
Collaborator

For future reference: there is no need to force push. Instead, just confirm (or write a comment with) the desired commit message. A new commit message is set when the PR is Squash and merged.

Also, for future PRs, please sign your commits.

@intelliot intelliot merged commit c6fee28 into XRPLF:develop Jul 6, 2023
@ckeshava
Copy link
Collaborator Author

ckeshava commented Jul 6, 2023

okay, I'll keep that in mind 👍

@ckeshava ckeshava deleted the changeMulDiv branch July 6, 2023 15:44
@thejohnfreeman
Copy link
Collaborator

Following up on an old question: @HowardHinnant @ckeshava please never force-push in a PR. If you end up in a situation where you don't want to bother resolving conflicts, and you just want to keep your changes, then use this:

git merge upstream/develop --strategy ours

This creates a merge commit with two parents, but just takes your changes wherever there is a conflict.

You should not rebase either. That is what creates the conditions (lost history) that encourages force-push.

@ckeshava
Copy link
Collaborator Author

Okay, thanks for the tip, I'll keep it in mind.

ckeshava added a commit to ckeshava/rippled that referenced this pull request Jul 10, 2023
- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix XRPLF#3495

---------

Co-authored-by: John Freeman <[email protected]>
ckeshava added a commit to ckeshava/rippled that referenced this pull request Sep 22, 2023
- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix XRPLF#3495

---------

Co-authored-by: John Freeman <[email protected]>
ckeshava added a commit to ckeshava/rippled that referenced this pull request Sep 25, 2023
- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix XRPLF#3495

---------

Co-authored-by: John Freeman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Passed Passed code review & PR owner thinks it's ready to merge. Perf sign-off may still be required.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Change mulDiv() return from pair<bool, std::uint64_t> to optional<std::uint64_t>
5 participants