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

replace map count/insert w/emplace in instantx.cpp #2165

Merged
merged 2 commits into from
Jul 12, 2018

Conversation

nmarley
Copy link

@nmarley nmarley commented Jul 4, 2018

This feels like an optimization, something about:

if map.count(key)
     return false
else
     insert(pair)
     return true

... just feels icky. Plus, vote.GetMasternodeOutpoint() is only called
once. The previous version might be slightly more readable, however.

This feels like an optimization, something about:

```
if map.count(key)
     return false
else
     insert(pair)
     return true
```

... just feels icky. Plus, `vote.GetMasternodeOutpoint()` is only called
once. The previous version might be slightly more readable, however.
src/instantx.cpp Outdated
return false;
mapMasternodeVotes.insert(std::make_pair(vote.GetMasternodeOutpoint(), vote));
return true;
return mapMasternodeVotes.emplace(std::make_pair(vote.GetMasternodeOutpoint(), vote)).second;
Copy link
Member

Choose a reason for hiding this comment

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

what does the .second do in this instance?

Copy link
Author

Choose a reason for hiding this comment

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

emplace is a method defined on std::map, so I will point to the documentation there (in case you're interested):

https://en.cppreference.com/w/cpp/container/map/emplace

Return value
Returns a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place. True for Insertion, False for No Insertion.

:)

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, that makes more sense. I guess I missed the first part of the return doc when I read it a few minutes ago. Thanks

Copy link

Choose a reason for hiding this comment

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

No need for make_pair here with emplace.

@UdjinM6 UdjinM6 added this to the 12.4 milestone Jul 4, 2018
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

Good idea, can be simplified a bit more though, see inline comment ;)

Also, maybe apply a similar fix to AcceptLockRequest/RejectLockRequest while at it?

src/instantx.cpp Outdated
return false;
mapMasternodeVotes.insert(std::make_pair(vote.GetMasternodeOutpoint(), vote));
return true;
return mapMasternodeVotes.emplace(std::make_pair(vote.GetMasternodeOutpoint(), vote)).second;
Copy link

Choose a reason for hiding this comment

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

No need for make_pair here with emplace.

@nmarley
Copy link
Author

nmarley commented Jul 4, 2018

You're right, good catch.

Also, maybe apply a similar fix to AcceptLockRequest/RejectLockRequest while at it?

These functions just call insert, so it wouldn't replace as much code as in this case, but is syntactically cleaner though. Is that what you're going for?

Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

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

Actually, ignore that part :) That should probably be in some another, more general PR.

utACK

@nmarley
Copy link
Author

nmarley commented Jul 4, 2018

@UdjinM6 I fully agree: nmarley@03af44a

;-)

Would like to get that larger PR in after iterator cleanup if possible, so easier to merge w/o conflicts.

Copy link

@codablock codablock left a comment

Choose a reason for hiding this comment

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

utACK

@UdjinM6 UdjinM6 merged commit 42c193d into dashpay:develop Jul 12, 2018
@nmarley nmarley deleted the ix-addvote-emplace branch August 21, 2018 22:04
CryptoCentric pushed a commit to absolute-community/absolute that referenced this pull request Apr 25, 2019
* replace map count/insert w/emplace in instantx.cpp

This feels like an optimization, something about:

```
if map.count(key)
     return false
else
     insert(pair)
     return true
```

... just feels icky. Plus, `vote.GetMasternodeOutpoint()` is only called
once. The previous version might be slightly more readable, however.

* use std::pair template constructor shortcut
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.

4 participants