Trivial: tiny c++11 refactors #8353

pull rodentrabies wants to merge 3 commits into bitcoin:master from rodentrabies:cpp11 changing 2 files +10 −6
  1. rodentrabies commented at 10:21 PM on July 17, 2016: contributor

    Changes for #8350

  2. laanwj added the label Refactoring on Jul 18, 2016
  3. fanquake commented at 9:17 AM on July 18, 2016: member

    Can you please improve the commit message to be more explicit about what is actually being changed.

  4. rodentrabies force-pushed on Jul 18, 2016
  5. rodentrabies commented at 9:28 AM on July 18, 2016: contributor

    Yep, sorry. Done.

  6. in src/rpc/server.cpp:None in 7f34f12dd0 outdated
     490 | @@ -490,7 +491,7 @@ void RPCRunLater(const std::string& name, boost::function<void(void)> func, int6
     491 |          throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC");
     492 |      deadlineTimers.erase(name);
     493 |      LogPrint("rpc", "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name());
     494 | -    deadlineTimers.insert(std::make_pair(name, boost::shared_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))));
     495 | +    deadlineTimers.insert(std::make_pair(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))));
    


    theuni commented at 9:57 PM on July 18, 2016:

    May as well use emplace here while you're at it:

    deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));
    
  7. dcousens commented at 2:06 AM on July 22, 2016: contributor

    utACK 3debfbc

  8. in src/limitedmap.h:None in 3debfbca0d outdated
      65 | @@ -66,8 +66,7 @@ class limitedmap
      66 |      }
      67 |      void update(const_iterator itIn, const mapped_type& v)
      68 |      {
      69 | -        // TODO: When we switch to C++11, use map.erase(itIn, itIn) to get the non-const iterator.
      70 | -        iterator itTarget = map.find(itIn->first);
      71 | +        iterator itTarget = map.erase(itIn, itIn);
    


    theuni commented at 8:29 PM on July 27, 2016:

    This doesn't look right. Since erase returns the next iterator, I'm not sure what good it would do here. It's also erasing without re-adding.

    As-is, this appears to be modifying a random element.


    dcousens commented at 1:13 AM on July 28, 2016:

    Actually, it doens't erase anything AFAIK. Note, erase works on [first, last).

    It is totally confusing though, but I assumed for that comment to exist, this had already been bikeshed as somehow sensible.

    Example:

    #include <iostream>
    #include <map>
    
    int main ()
    {
        std::map<char,int> mymap;
    
        // insert some values
        mymap['a'] = 10;
        mymap['b'] = 20;
        mymap['c'] = 30;
        mymap['d'] = 40;
        mymap['e'] = 50;
        mymap['f'] = 60;
    
        std::cout << mymap.size() << std::endl; // 6
        const auto x = mymap.find('d');
        mymap.erase(x, x);
        std::cout << mymap.size() << std::endl; // 6
    
        // show all
        for (auto y : mymap)
            std::cout << y.second << '\n'; // 10, 20, 30, 40, 50, 60
    
        return 0;
    }
    

    dcousens commented at 1:43 AM on July 28, 2016:

    ping @TheBlueMatt (this was after all, your TODO)


    theuni commented at 2:41 AM on July 28, 2016:

    Huh, indeed. Neat trick! Thanks for explaining. Is the returned iterator guaranteed to be the same as the inputs? Everything I can find references the return relative to the last item erased, which obviously doesn't apply here.

    Assuming it's all well defined, fine by me, though it's much less clear what's happening.


    laanwj commented at 9:03 AM on August 3, 2016:

    I strongly prefer non-tricky code to tricky code, even if documented, to be honest. It increases the scope of making mistakes (due to misunderstanding the code) in later maintenance. So unless this makes a clear performance difference in practical usage I prefer keeping it as-is.


    rodentrabies commented at 5:18 PM on August 3, 2016:

    I currently don't have access to my laptop, so will bench and fix it accordingly in a couple of days.

    On Aug 3, 2016 7:25 PM, "Wladimir J. van der Laan" notifications@github.com wrote:

    In src/limitedmap.h #8353 (review):

    @@ -66,8 +66,7 @@ class limitedmap } void update(const_iterator itIn, const mapped_type& v) {

    •    // TODO: When we switch to C++11, use map.erase(itIn, itIn) to get the non-const iterator.
    •    iterator itTarget = map.find(itIn->first);
    •    iterator itTarget = map.erase(itIn, itIn);

    I strongly prefer non-tricky code to tricky code, even if documented, to be honest. It increases the scope of making mistakes (due to misunderstanding the code) in later maintenance. So unless this makes a clear performance difference in practical usage I prefer keeping it as-is.

    — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bitcoin/bitcoin/pull/8353/files/3debfbca0d85d7c17f67c5a15df0938584445026#r73304022, or mute the thread https://github.com/notifications/unsubscribe-auth/AHSu6XP5fp8pV6uoGcv3MUlHEtEF-t1Nks5qcFmmgaJpZM4JOU7Z .


    dcousens commented at 9:29 PM on August 3, 2016:

    @laanwj this trick is explained here: https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator, and appears to be pretty common.

    The access is constant time versus the lookup, so it is an improvement (assuming this is actually a performance bottleneck anyhow). I think a SO link and explanation would be more than fine to appropriate its existence.

  9. sipa commented at 1:59 AM on July 30, 2016: member

    Concept ACK, but the erase trick needs a comment to explain the behaviour.

  10. use std::map::erase(const_iterator, const_iterator) to get non-constant iterator 947913fc54
  11. use c++11 std::unique_ptr instead of boost::shared_ptr 5e187e7001
  12. use std::map::emplace() instead of std::map::insert() c784086075
  13. rodentrabies force-pushed on Aug 9, 2016
  14. rodentrabies commented at 12:16 AM on August 9, 2016: contributor

    Added an explanation for the map::erase() usage with an SO link, mentioned above.

  15. dcousens commented at 12:29 AM on August 9, 2016: contributor

    utACK c784086

  16. sipa commented at 5:25 AM on August 10, 2016: member
  17. rodentrabies commented at 7:16 AM on August 10, 2016: contributor

    @sipa note about using emplace()? Yes, the last commit in this PR does that.

  18. sipa commented at 8:10 AM on August 10, 2016: member

    utACK c78408607536bf030947f69f6dc6e09e07873c84

  19. laanwj commented at 1:55 PM on August 13, 2016: member

    utACK c784086

  20. laanwj merged this on Aug 13, 2016
  21. laanwj closed this on Aug 13, 2016

  22. laanwj referenced this in commit 3859072963 on Aug 13, 2016
  23. codablock referenced this in commit 5cb5b1654d on Sep 19, 2017
  24. codablock referenced this in commit cc3138f48a on Dec 29, 2017
  25. codablock referenced this in commit 6ba688b1c6 on Jan 8, 2018
  26. andvgal referenced this in commit dbefa33c13 on Jan 6, 2019
  27. rodentrabies deleted the branch on Mar 15, 2021
  28. DrahtBot locked this on Aug 18, 2022

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-04-14 21:15 UTC

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