Skip to content

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

Merged
UdjinM6 merged 2 commits into
dashpay:developfrom
nmarley:ix-addvote-emplace
Jul 12, 2018
Merged

replace map count/insert w/emplace in instantx.cpp#2165
UdjinM6 merged 2 commits into
dashpay:developfrom
nmarley:ix-addvote-emplace

Conversation

@nmarley

@nmarley nmarley commented Jul 4, 2018

Copy link
Copy Markdown

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.
Comment thread 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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

@UdjinM6 UdjinM6 left a comment

Copy link
Copy Markdown

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?

Comment thread 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
Copy Markdown

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

nmarley commented Jul 4, 2018

Copy link
Copy Markdown
Author

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?

@UdjinM6 UdjinM6 left a comment

Copy link
Copy Markdown

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

nmarley commented Jul 4, 2018

Copy link
Copy Markdown
Author

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

@codablock codablock left a comment

Copy link
Copy Markdown

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