Skip to content

mon: optimize the get_rank function - #9096

Closed
songbaisen wants to merge 1 commit into
ceph:masterfrom
songbaisen:song25
Closed

songbaisen wants to merge 1 commit into
ceph:masterfrom
songbaisen:song25

Conversation

@songbaisen

Copy link
Copy Markdown

mon: optimize the get_rank function

Signed-off-by: songbaisen [email protected]

@jecluis

jecluis commented May 12, 2016

Copy link
Copy Markdown
Member

Behavior looks to be functionally the same and I can't think of any thing that would suffer from this.

Code is also simpler and easier on the eyes - plus it's more cpp-ish than before.

I don't think however this can be called an optimization. We're dealing with vectors with a really limited number of elements (we'd expect 1 to a less than a dozen).

The original code has a worst-case complexity of O(n) - we're looking at all the elements after all -, if we end up looking for the last element in the vector. The new code performs no only an std::find(), which has a complexity of O(n), but also an std::distance(), which also has a complexity of O(n). So we're not really optimizing the code.

Given the performance impact on this should be low anyway, regardless of whether we loop once or twice - again, expected to be a really small number of elements -, I'm okay with this just for getting rid of the loop and making things prettier. But the commit message should be adjusted to make it clear this is eye candy rather than an optimization.

@jecluis

jecluis commented May 12, 2016

Copy link
Copy Markdown
Member

Oh, I forgot to mention: some of your lines appear to be over 80 characters. Please make sure to hard wrap them at 80.

Comment thread src/mon/MonMap.h
for (unsigned i=0; i<rank_name.size(); i++)
if (rank_name[i] == n)
return i;
vector<string>::iterator iter = std::find(rank_name.begin(), rank_name.end(), n);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could mark this function const also.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i'd suggest use auto

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit, could use a better name than iter, for instance, name or found.

@tchaikov

Copy link
Copy Markdown
Contributor

agree with @jecluis . and i think the original code is also easy for mortal readers.

@songbaisen

Copy link
Copy Markdown
Author

@jecluis 😄 Thank you sir!That is true.And I will close this pr.Before this may i ask you two question.The first is that I seen the mon use the paxos like the fast paxos.But it is diff from that.Each proposal in ceph we need wait for all the quoro agree the proposal.If one quoro do not reply this in lease time , we will restart the election.But the origin fast paxos do not like this .Only more than half quoro agree the proposal then pass it.And do not need to new election.The second question is that after election the leader and quoro keep headbeat in lease time.If one quoro do not answer in the lease time.The leader will restart the election.It means that the front network for mon must be stable.If one quoro mon network do not stable such as constantly up and down or have a lot of packages loss this will result in the whole mon constantly election.

@songbaisen

Copy link
Copy Markdown
Author

@tchaikov 😄 Thank you for review!.And I will close this pr after the consult。

@songbaisen songbaisen closed this Nov 23, 2016
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.

3 participants