Improve FormatMoney #3061

pull lano1106 wants to merge 1 commits into bitcoin:master from lano1106:formatmoney_imprv changing 1 files +26 −12
  1. lano1106 commented at 4:26 AM on October 7, 2013: contributor

    3 possible cases for n: > 0, < 0 and == 0

    Skip everything if == 0

    reserve string memory for every valid Money values

    Add sign first to avoid memory copy that inserting it at the end incur.

    Remove a superfluous counter: nTrim

    Eliminate 2 logical expression evaluations.

    Signed-off-by: Olivier Langlois olivier@olivierlanglois.net

  2. Improve FormatMoney
    3 possible cases for n: > 0, < 0 and == 0
    
    Skip everything if == 0
    
    reserve string memory for every valid Money values
    
    Add sign first to avoid memory copy that inserting it at the end incur.
    
    Remove a superfluous counter: nTrim
    
    Eliminate 2 logical expression evaluations.
    
    Signed-off-by: Olivier Langlois <olivier@olivierlanglois.net>
    f38e39df7f
  3. BitcoinPullTester commented at 5:20 AM on October 7, 2013: none

    Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/f38e39df7f009d9d229b14447a2644db65c3d183 for binaries and test log. This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/ Contact BlueMatt on freenode if something looks broken.

  4. in src/util.cpp:None in f38e39df7f
     388 | @@ -389,22 +389,36 @@ string FormatMoney(int64 n, bool fPlus)
     389 |  {
     390 |      // Note: not using straight sprintf here because we do NOT want
     391 |      // localized number formatting.
     392 | -    int64 n_abs = (n > 0 ? n : -n);
     393 | +    if (!n)
    


    laanwj commented at 8:30 AM on October 7, 2013:

    Is this special case needed? Or can it simply be handled by the if (n>=0) case?


    lano1106 commented at 3:33 AM on October 8, 2013:

    not needed. It could be removed if desired and and this case be treated generically by the rest of the code. That being said, it must be discriminated anyway when handling the sign. Cannot prefix '+' or '-' to it. So why not leverage this to skip the whole processing.


    laanwj commented at 10:51 AM on October 8, 2013:

    Yes, the sign must be discriminated anyway, but for readability and flexibility it's best to have as little special cases as possible.

  5. laanwj commented at 8:30 AM on October 7, 2013: member

    My main gripe here is that you're making the code longer instead of shorter. Have you benchmarked the improvement in any way?

  6. Diapolo commented at 8:41 AM on October 7, 2013: none

    That is really the sort of code I don't like to read, before AND after ^^.

  7. lano1106 commented at 3:45 AM on October 8, 2013: contributor

    laanwj,

    what metric are you using to make the statement that the code is longer?

    Line code count may be misleading. Out of the 26 new lines, 9 are empty lines or lines only having brackets. Also consider that previous statement:

    int64 n_abs = (n > 0 ? n : -n);

    is in fact a packed if else block.

    fair enough. I'll produce a small benchmark program and report back the result here.

  8. gavinandresen commented at 4:01 AM on October 8, 2013: contributor

    I find the old code easier to read, and ease of reading is more important than performance. Especially for a routine like this, which I strongly doubt is on any critical code paths.

  9. lano1106 commented at 4:42 AM on October 8, 2013: contributor

    Ok. it is not a good pull request. Hell is paved with good intentions.

    I have benchmarked my modifs and the results are almost identical between old code and proposal.

    Any memcpy avoided by new code is offseted possibly by the fact it has now 2 string object. local str and strprintf return value whereas the old code benefit from RVO (Return Value Optimization).

    Thanks for your time guys. I'm learning and improving myself. I'll eventually come up with a good pull request!

  10. lano1106 closed this on Oct 8, 2013

  11. laanwj commented at 10:51 AM on October 8, 2013: member

    @lano1106 thanks for trying to help anyway!

  12. Bushstar referenced this in commit 386de78bcb on Apr 8, 2020
  13. Bushstar referenced this in commit c74f2cd8b2 on Apr 8, 2020
  14. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-02 21:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me