Skip to content

iterator cleanup in several places - #2164

Merged
UdjinM6 merged 4 commits into
dashpay:developfrom
nmarley:iterator-cleanup
Jul 12, 2018
Merged

iterator cleanup in several places#2164
UdjinM6 merged 4 commits into
dashpay:developfrom
nmarley:iterator-cleanup

Conversation

@nmarley

@nmarley nmarley commented Jul 4, 2018

Copy link
Copy Markdown

Please see individual commits. A few notes:

  • this should be all Dash-specific code, so none of these should conflict w/Bitcoin PRs
  • mostly uses auto and range-based for

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

Looks good in general but it feels a bit awkward to see iterators-like names it/itOutpointLock/etc inside loop blocks. Let's clean this up pls e.g. s/itOutpointLock/outpointLock/ etc.

@nmarley

nmarley commented Jul 4, 2018

Copy link
Copy Markdown
Author

You're probably right. What would you suggest I use for a std::map<int, CSporkMessage> entry though (from the 1st commit for example)? Just entry, or pair, or ... ?

Or are you only suggesting this for the longer it[A-Za-z]? names?

@UdjinM6

UdjinM6 commented Jul 4, 2018

Copy link
Copy Markdown

For simple cases like that (literally two rows of code) pair would do it I guess. For mapMasternodes it's mnpair all over the place already, for mMnbRecoveryGoodReplies (which is a bit more code) could be mnbreplypair to give some context or smth like that.

@nmarley nmarley changed the title iterator cleanup in several places [WIP] iterator cleanup in several places Jul 4, 2018
@nmarley
nmarley force-pushed the iterator-cleanup branch from 67ae044 to f998e14 Compare July 4, 2018 17:23
@nmarley nmarley changed the title [WIP] iterator cleanup in several places iterator cleanup in several places Jul 4, 2018
@nmarley

nmarley commented Jul 4, 2018

Copy link
Copy Markdown
Author

Ok, I believe I have addressed the naming issues. I have also cleaned up a few more places in the code where range-based for and auto can be used.

@UdjinM6

UdjinM6 commented Jul 4, 2018

Copy link
Copy Markdown

You can't do this

for (auto& pair : someMap) {
    auto key = pair.first;
    someMap.erase(key);
}

It's going to segfault.

@nmarley

nmarley commented Jul 5, 2018

Copy link
Copy Markdown
Author

@UdjinM6

UdjinM6 commented Jul 5, 2018

Copy link
Copy Markdown

Code: https://gitlab.com/nmarley/cpptest/blob/master/range-for-modify.cpp

The page could not be found or you don't have permission to view it.

@nmarley

nmarley commented Jul 5, 2018

Copy link
Copy Markdown
Author

@UdjinM6 Sorry, should be public now. And you're right, looks like this code was "working" because of some internal ordering of keys. Once I tried deleting element 13 instead, things got screwy.

I will go back and refactor (or revert) these. I was hoping to get rid of all the incrementing iterator else blocks this way. :/

@UdjinM6

UdjinM6 commented Jul 5, 2018

Copy link
Copy Markdown

Well, if that was the goal, you could probably try smth like:

auto it = someMap.begin();
while (it != someMap.end()) {
    auto curr = it++;
    if (condition) {
        // do smth using curr
        someMap.erase(curr);
    }
}

EDIT:

or if you REEEALLY want to use for, you could probably even do smth like:

for (auto it = someMap.begin(); it != someMap.end(); ) {
    auto curr = it++;
    if (condition) {
        // do smth using curr
        someMap.erase(curr);
    }
}

:)

@UdjinM6

UdjinM6 commented Jul 5, 2018

Copy link
Copy Markdown

The problem with the approach above is that this way you always create a copy, even if you don't need it, while with else ++it; you can avoid it (in some cases most of the time).

EDIT: however for small maps the overhead is probably negligible.

@nmarley

nmarley commented Jul 5, 2018

Copy link
Copy Markdown
Author

Well, my idea was actually to invert the if / else condition such that I can if (thing) { it++ ; continue; } and remove the else block that way. But might do that in a separate refactor PR.

@nmarley
nmarley force-pushed the iterator-cleanup branch from 50ba884 to 6b78375 Compare July 9, 2018 10:22
@nmarley

nmarley commented Jul 9, 2018

Copy link
Copy Markdown
Author

I've reverted changes where I made the false assuption that a container being iterated over could be modified using range-based for, which isn't the case. Also rebased onto the latest develop and squashed all since I reverted most of the changes that I'd originally made.

@UdjinM6

UdjinM6 commented Jul 9, 2018

Copy link
Copy Markdown

Pushed few suggestions to my repo, see individual commits:

  • could use const refs in few more places 401be7b
  • some names are still confusing a6084bb
  • use prefix ++ e4a4e59

Otherwise looks good 👍

@nmarley

nmarley commented Jul 10, 2018

Copy link
Copy Markdown
Author

Just had one question on the rationale for ++thing vs thing++, but looks good, thanks! 👍

edit: I fast-forwarded my branch tip to include your commits directly.

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

ACK

@UdjinM6
UdjinM6 merged commit fd70a1e into dashpay:develop Jul 12, 2018
@nmarley
nmarley deleted the iterator-cleanup branch July 23, 2018 17:20
CryptoCentric pushed a commit to absolute-community/absolute that referenced this pull request Apr 25, 2019
* iterator cleanup in Dash-specific code

* const

* *Pair/pair

* it++ -> ++it
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.

2 participants