Skip to content

Conversation

@rodentrabies
Copy link
Contributor

@rodentrabies rodentrabies commented Jul 17, 2016

Changes for #8350

@fanquake
Copy link
Member

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

@rodentrabies
Copy link
Contributor Author

Yep, sorry. Done.

Copy link
Member

Choose a reason for hiding this comment

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

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

deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));

@dcousens
Copy link
Contributor

utACK 3debfbc

src/limitedmap.h Outdated
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

@dcousens dcousens Jul 28, 2016

Choose a reason for hiding this comment

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

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;
}

Copy link
Contributor

@dcousens dcousens Jul 28, 2016

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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.

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 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 (comment):

@@ -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
.

Copy link
Contributor

@dcousens dcousens Aug 3, 2016

Choose a reason for hiding this comment

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

@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.

@sipa
Copy link
Member

sipa commented Jul 30, 2016

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

@rodentrabies
Copy link
Contributor Author

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

@dcousens
Copy link
Contributor

dcousens commented Aug 9, 2016

utACK c784086

@sipa
Copy link
Member

sipa commented Aug 10, 2016

utACK. Did you see 5e187e7#r71238406 ?

EDIT: nevermind, i was confused by github.

@rodentrabies
Copy link
Contributor Author

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

@sipa
Copy link
Member

sipa commented Aug 10, 2016

utACK c784086

@laanwj
Copy link
Member

laanwj commented Aug 13, 2016

utACK c784086

@laanwj laanwj merged commit c784086 into bitcoin:master Aug 13, 2016
laanwj added a commit that referenced this pull request Aug 13, 2016
c784086 use std::map::emplace() instead of std::map::insert() (whythat)
5e187e7 use c++11 std::unique_ptr instead of boost::shared_ptr (whythat)
947913f use std::map::erase(const_iterator, const_iterator) to get non-constant iterator (whythat)
codablock pushed a commit to codablock/dash that referenced this pull request Jan 8, 2018
c784086 use std::map::emplace() instead of std::map::insert() (whythat)
5e187e7 use c++11 std::unique_ptr instead of boost::shared_ptr (whythat)
947913f use std::map::erase(const_iterator, const_iterator) to get non-constant iterator (whythat)
andvgal pushed a commit to energicryptocurrency/gen2-energi that referenced this pull request Jan 6, 2019
c784086 use std::map::emplace() instead of std::map::insert() (whythat)
5e187e7 use c++11 std::unique_ptr instead of boost::shared_ptr (whythat)
947913f use std::map::erase(const_iterator, const_iterator) to get non-constant iterator (whythat)
@rodentrabies rodentrabies deleted the cpp11 branch March 15, 2021 22:52
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants